-
Notifications
You must be signed in to change notification settings - Fork 0
/
FAC SIMILE ESAME.c
80 lines (72 loc) · 1.38 KB
/
FAC SIMILE ESAME.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdlib.h>
#define FILE_MATRICE "Matrice.txt"
#define FILE_PARAMS "Parametri.txt"
#define FILE_SUMMARY "Sommario.txt"
void leggiParams(int *num_r, int *num_c){
FILE *fi = fopen(FILE_PARAMS, "r");
fscanf(fi, "%d%d", num_r, num_c);
fclose(fi);
}
void leggiMatrix(int r, int c, int **mat){
int i,j;
FILE *fi = fopen(FILE_MATRICE, "r");
if(fi!=NULL){
for(i=0; i<r; i++){
for(j=0; j<c; j++){
int a;
fscanf(fi,"%d", &a);
mat[i][j]=a;
}
}
fclose(fi);
}else{
printf("ERRORE LETTURA MATRICE");
}
}
void maxRow(int r, int c, int **mat, int *vettore){
int i,j;
for(i=0; i<r; i++){
vettore[i]=mat[i][0];
for(j=0; j<c; j++){
if(mat[i][j]>vettore[i])
vettore[i]=mat[i][j];
}
}
}
void writeFile(int *arr, int l){
int i;
FILE *fi = fopen(FILE_SUMMARY, "w");
for(i=0; i<l; i++){
fprintf(fi, "Valore max di riga %d: %d\n", i+1, arr[i]);
}
fclose(fi);
}
int** createMatrix(int r, int c){
int ** a;
int i;
a= (int **) malloc (sizeof(int *) * r);
for(i=0; i<r; i++){
*(a+i) = malloc(sizeof(int) * c);
}
return a;
}
int main(){
int r,c,i;
int *dati;
int **m;
leggiParams(&r,&c);
//mem alloc for array
dati = (int *) malloc(r*sizeof(int));
//mem alloc for matrix
m = createMatrix(r,c);
leggiMatrix(r,c,m);
maxRow(r,c,m,dati);
writeFile(dati, r);
//libero la memoria
for(i=0; i<r; i++){
free(m[i]);
}
free(m);
free(dati);
}