Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • 20041679/prog-1-lab-assigments
1 result
Show changes
#include <stdio.h>
#define ROWS(a) (sizeof(a) / sizeof(a[0]))
#define COLS(a) (sizeof(a[0]) / sizeof(a[0][0]))
void input(int rows, int cols, int matrix[][cols]) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("[%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
}
int count_eq(int rows, int cols, int matrix[][cols], int key) {
int ret = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (matrix[i][j] == key) {
++ret;
}
}
}
return ret;
}
int sum(
int rows1, int cols1, int a1[][cols1],
int rows2, int cols2, int a2[][cols2],
int rows3, int cols3, int a3[][cols3])
{
int ret;
if (rows1 == rows2 && rows2 == rows3 && cols1 == cols2 && cols2 == cols3) {
int rows = rows1;
int cols = cols1;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
a3[i][j] = a1[i][j] + a2[i][j];
}
}
ret = 0;
} else {
ret = -1;
}
return ret;
}
void print(int rows, int cols, int matrix[][cols]) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%3d ", matrix[i][j]);
}
puts("");
}
}
int multiply(
int rows1, int cols1, int a1[][cols1],
int rows2, int cols2, int a2[][cols2],
int rows3, int cols3, int a3[][cols3])
{
int ret;
if (cols1 == rows2 && rows1 == rows3 && cols2 == cols3) {
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
int sum = 0;
for (int k = 0; k < rows2; ++k) {
sum += a1[i][k] * a2[k][j];
}
a3[i][j] = sum;
}
}
ret = 1;
} else {
ret = -1;
}
return ret;
}
int main(void) {
int a1[2][3];
int a2[3][2];
puts("inserimento A");
input(ROWS(a1), COLS(a1), a1);
puts("inserimento B");
input(ROWS(a2), COLS(a2), a2);
int x;
printf("inserire x: ");
scanf("%d", &x);
printf("instanze di %d in A: %d\n", x, count_eq(ROWS(a1), COLS(a1), a1, x));
int y;
printf("inserire y: ");
scanf("%d", &y);
printf("instanze di %d in B: %d\n", y, count_eq(ROWS(a2), COLS(a2), a2, y));
int s[ROWS(a1)][COLS(a1)];
if (sum(ROWS(a1), COLS(a1), a1, ROWS(a2), COLS(a2), a2, ROWS(s), COLS(s), s) < 0) {
fputs("somma impossibile\n", stderr);
} else {
print(ROWS(s), COLS(s), s);
}
int m[2][2];
if (multiply(ROWS(a1), COLS(a1), a1, ROWS(a2), COLS(a2), a2, ROWS(m), COLS(m), m) < 0) {
fputs("prodotto impossibile\n", stderr);
} else {
print(ROWS(m), COLS(m), m);
}
}
aaaaaa
aaabca
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define err(s) fprintf(stderr, s "%s\n", strerror(errno))
#define F1_STR "1.txt"
#define F2_STR "2.txt"
int main(void) {
int ret = 0;
FILE *f1;
if ((f1 = fopen(F1_STR, "r"))) {
FILE *f2;
if ((f2 = fopen(F2_STR, "r"))) {
char c1;
char c2;
int diff = 0;
while (fscanf(f1, "%c", &c1) != EOF &&
fscanf(f2, "%c", &c2) != EOF &&
!diff) {
if (c1 != c2) {
diff = 1;
printf("file 1: %c, file2: %c\n", c1, c2);
}
}
fclose(f2);
} else {
ret = 1;
err(F2_STR ": ");
}
fclose(f1);
} else {
ret = 1;
err(F1_STR ": ");
}
return ret;
}
abcdefghijk
abbdefgjklk
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define err(s) fprintf(stderr, s "%s\n", strerror(errno))
#define F1_STR "1.txt"
#define F2_STR "2.txt"
#define FOUT_STR "out.txt"
int main(void) {
int ret = 0;
FILE *f1;
if ((f1 = fopen(F1_STR, "r"))) {
FILE *f2;
if ((f2 = fopen(F2_STR, "r"))) {
FILE *fout;
if ((fout = fopen(FOUT_STR, "w"))) {
char c1;
char c2;
while (fscanf(f1, "%c", &c1) != EOF &&
fscanf(f2, "%c", &c2) != EOF) {
if (c1 != c2) {
printf("file 1: %c, file2: %c\n", c1, c2);
fprintf(fout, "%c%c\n", c1, c2);
}
}
fclose(fout);
} else {
ret = 1;
err(FOUT_STR ": ");
}
fclose(f2);
} else {
ret = 1;
err(F2_STR ": ");
}
fclose(f1);
} else {
ret = 1;
err(F1_STR ": ");
}
return ret;
}
cb
hj
ik
jl
aaa bbb ccc ddd eee
aaa b ccc dd eeee
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define err(s) fprintf(stderr, s "%s\n", strerror(errno))
#define F1_STR "1.txt"
#define F2_STR "2.txt"
int main(void) {
int ret = 0;
FILE *f1;
if ((f1 = fopen(F1_STR, "r"))) {
FILE *f2;
if ((f2 = fopen(F2_STR, "r"))) {
char s1[128];
char s2[128];
while (fscanf(f1, "%s", s1) != EOF &&
fscanf(f2, "%s", s2) != EOF) {
if (strcmp(s1, s2) != 0) {
puts(s2);
}
}
fclose(f2);
} else {
ret = 1;
err(F2_STR ": ");
}
fclose(f1);
} else {
ret = 1;
err(F1_STR ": ");
}
return ret;
}
1
2
3
4
5
6
7
8
9
-1
1
2
3
3
3
9
8
7
6
-1
#include <stdio.h>
#define ARR_SIZE(arr) (sizeof(arr) / sizeof(*arr))
#define MARK -1
int write_to_file(char *filename, int arr[]) {
int ret = 0;
FILE *file;
if ((file = fopen(filename, "w")) == NULL) {
ret = -1;
} else {
int i = 0;
while (arr[i] != MARK && arr[i + 1] != MARK) {
fprintf(file, "%d-", arr[i]);
++i;
}
fprintf(file, "%d", arr[i]);
fclose(file);
}
return ret;
}
int read_from_file(char *filename, int arr[], int size) {
int ret;
FILE *file;
if ((file = fopen(filename, "r")) == NULL) {
ret = -1;
} else {
int i = 0;
int end = 0;
int buf;
while (i < size && !end) {
fscanf(file, "%d", &buf);
if (buf == MARK) {
end = 1;
} else {
arr[i] = buf;
++i;
}
}
if (end) {
arr[i] = MARK;
ret = i;
} else {
arr[0] = MARK;
ret = -1;
}
fclose(file);
}
return ret;
}
void modify(int arr1[], int arr2[]) {
int i = 0;
while (arr1[i] != MARK && arr2[i] != MARK) {
int a = arr1[i];
int b = arr2[i];
if (a == b) {
arr1[i] = 0;
} else if (a < b) {
arr1[i] = b;
} else {
++arr1[i];
}
++i;
}
}
int main(void) {
int ret = 0;
int arr1[10];
if (read_from_file("in1.txt", arr1, ARR_SIZE(arr1)) < 0) {
ret = 1;
} else {
int arr2[10];
if (read_from_file("in2.txt", arr2, ARR_SIZE(arr2)) < 0) {
ret = 1;
} else {
modify(arr2, arr1);
write_to_file("out.txt", arr2);
}
}
return ret;
}
0-0-0-4-5-10-9-8-9
\ No newline at end of file