...
Last active
October 30, 2017 13:27
-
-
Save aziis98/ec2c9609275cb639d152d389f951b543 to your computer and use it in GitHub Desktop.
Esercizi del 25 Ottobre 2017 - http://pages.di.unipi.it/bodei/CORSO_FP_17/FP/Lezioni/Lab3.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
char c; | |
while (c != '$') { | |
scanf("%c", &c); | |
if (c >= '0' && c <= '9') { | |
for (int i = 0; i < c - '0'; i++) | |
printf("*"); | |
} | |
else { | |
printf("%c", c); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define SIZE 10 | |
void swap(int vet[], int i1, int i2) { | |
int tmp = vet[i1]; | |
vet[i1] = vet[i2]; | |
vet[i2] = tmp; | |
} | |
int main() { | |
int vet[SIZE]; | |
for (int i = 0; i < SIZE; i++) { | |
scanf("%d", &vet[i]); | |
if (vet[i] != 0 && vet[i] != 1) { | |
return 1; | |
} | |
} | |
int i = 0, j = SIZE - 1; | |
while (i < j) { | |
int front = vet[i]; | |
int back = vet[j]; | |
if (front != back) { | |
if (front == 1) { | |
swap(vet, i, j); | |
} | |
i++; | |
j--; | |
} | |
else if (front == 0) { | |
i++; | |
} | |
else if (front == 1) { | |
j--; | |
} | |
} | |
for (int i = 0; i < SIZE; ++i) | |
{ | |
printf("%d ", vet[i]); | |
} | |
printf("\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
int n = -1; | |
int count = 0; | |
while (n != 0) { | |
scanf("%d", &n); | |
if (n != 0 && n % 5 == 0 && n % 7 != 0) { | |
count++; | |
} | |
} | |
printf("%d\n", count); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define SIZE 10 | |
int main() { | |
int vet[SIZE]; | |
for (int i = 0; i < SIZE; i++) { | |
scanf("%d", &vet[i]); | |
} | |
int sum = 0; | |
for (int i = 1; i < SIZE; i += 2) { | |
sum += vet[i]; | |
} | |
printf("%d\n", sum); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define SIZE 10 | |
int main() { | |
int vet[SIZE]; | |
for (int i = 0; i < SIZE; i++) { | |
scanf("%d", &vet[i]); | |
} | |
int i; | |
for (i = 0; i < SIZE - 1 && vet[i] < vet[i + 1]; i++) { ; } | |
printf("%d\n", i == SIZE - 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define SIZE 5 | |
void swap(int vet[], int i1, int i2) { | |
int tmp = vet[i1]; | |
vet[i1] = vet[i2]; | |
vet[i2] = tmp; | |
} | |
int main() { | |
int vet[SIZE]; | |
for (int i = 0; i < SIZE; i++) { | |
scanf("%d", &vet[i]); | |
} | |
for (int i = 0; i < SIZE / 2; i++) { | |
swap(vet, i, SIZE - i - 1); | |
} | |
for (int i = 0; i < SIZE; i++) { | |
printf("%d ", vet[i]); | |
} | |
printf("\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Versione con array di dimensione richiesta in input | |
*/ | |
#include <stdio.h> | |
#define SIZE 5 | |
void swap(int vet[], int i1, int i2) { | |
int tmp = vet[i1]; | |
vet[i1] = vet[i2]; | |
vet[i2] = tmp; | |
} | |
int main() { | |
int n; | |
printf("n = "); | |
scanf("%d", &n); | |
int vet[n]; | |
for (int i = 0; i < n; i++) { | |
scanf("%d", &vet[i]); | |
} | |
for (int i = 0; i < n / 2; i++) { | |
swap(vet, i, n - i - 1); | |
} | |
for (int i = 0; i < n; i++) { | |
printf("%d ", vet[i]); | |
} | |
printf("\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
int n, m; | |
printf("Inserici gli elementi dell'insieme A:\n"); | |
printf("cardinalità = "); | |
scanf("%d", &n); | |
int vet1[n]; | |
for (int i = 0; i < n; i++) { | |
scanf("%d", &vet1[i]); | |
} | |
printf("Inserici gli elementi dell'insieme B, per vedere se è un sottoinsieme di A:\n"); | |
printf("cardinalità = "); | |
scanf("%d", &m); | |
int vet2[m]; | |
for (int i = 0; i < m; i++) { | |
scanf("%d", &vet2[i]); | |
} | |
int sottoinsieme = 1; | |
for (int j = 0; sottoinsieme && j < m; j++) { | |
int contenuto = 0; | |
for (int i = 0; !contenuto && i < n; i++) { | |
if (vet1[i] == vet2[j]) { | |
contenuto = 1; | |
} | |
} | |
if (contenuto == 0) { | |
sottoinsieme = 0; | |
} | |
} | |
if (sottoinsieme) { | |
printf("B è un sottoinsieme di A\n"); | |
} | |
else { | |
printf("B non è un sottoinsieme di A\n"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
int h, l; | |
scanf(" %d", &h); | |
scanf(" %d", &l); | |
for (int i = 0; i < h; i++) { | |
if (i == 0 || i == h - 1) { | |
for(int i = 0; i < l; i++) { | |
printf("*"); | |
} | |
printf("\n"); | |
} | |
else { | |
printf("*"); | |
for (int j = 0; j < l - 2; j++) { | |
printf(" "); | |
} | |
printf("*\n"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define SIZE 7 | |
void swap(int vet[], int i1, int i2) { | |
int tmp = vet[i1]; | |
vet[i1] = vet[i2]; | |
vet[i2] = tmp; | |
} | |
int main() { | |
int vet[SIZE], count[10] = { 0 }; | |
printf("Inserisci una sequenza di %d numeri:\n", SIZE); | |
for (int i = 0; i < SIZE; i++) { | |
scanf("%d", &vet[i]); | |
} | |
for (int i = 0; i < SIZE; i++) { | |
count[vet[i]]++; | |
} | |
// test | |
for (int i = 0; i < 10; i++) { | |
printf("%d ", count[i]); | |
} | |
printf("\n"); | |
int i = 0; | |
for (int j = 0; j < 10; j++) { | |
while (count[j]-- > 0) { | |
vet[i++] = j; | |
} | |
} | |
// test | |
for (int i = 0; i < SIZE; i++) { | |
printf("%d ", vet[i]); | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment