Fondamenti di Programmazione a.a. 2011-2012
Soluzioni prova intermedia - 18 novembre 2011
#include <stdio.h>
int main() {
printf("Digitare due caratteri (separati da uno spazio): ");
char c1, c2;
scanf("%c %c", &c1, &c2);
printf("Digitare un intero positivo: ");
int n;
scanf("%d", &n);
for (int i = 0, k = 2*n - 1 ; i < n ; i++, k -= 2) {
for (int j = 0 ; j < 2*n + 1 + i ; j++) {
if (j < i) printf(" ");
else if (j == i + k) printf("%c", c2);
else printf("%c", c1);
}
printf("\n");
}
return 0;
}
int present(int V[], int k, int x) {
for (int i = 0 ; i < k ; i++)
if (V[i] == x) return 1;
return 0;
}
int valcom(int A[], int B[], int n) {
int count = 0;
for (int i = 0 ; i < n ; i++)
if (!present(A, i, A[i]) && present(B, n, A[i]))
count++;
return count;
}
#include <ctype.h>
int wcount(char *text) {
int i = -1, count = 0;
char first = '#';
do {
i++;
if (isalpha(text[i])) {
if (first == '#')
first = tolower(text[i]);
} else if (first != '#') {
if (tolower(text[i - 1]) == first)
count++;
first = '#';
}
} while (text[i] != '\0');
return count;
}
#include <stdlib.h>
int *chareplace(char **strA, int n, char c1, char c2) {
int *count = malloc(n * sizeof (int));
for (int i = 0; i < n; i++) {
count[i] = 0;
for (int j = 0; strA[i][j] != '\0'; j++) {
if (strA[i][j] == c1) {
strA[i][j] = c2;
count[i]++;
}
}
}
return count;
}
int submatrix(int n, char M[n][n], char x, int k, int *pr, int *pc) {
for (int r = 0 ; r <= n - k ; r++) {
for (int c = 0 ; c <= n - k ; c++) {
int ok = 1;
for (int i = 0 ; i < k && ok ; i++)
for (int j = 0 ; j < k && ok ; j++)
if (M[r + i][c +j] != x)
ok = 0;
if (ok) {
*pr = r;
*pc = c;
return 1;
}
}
}
return 0;
}