-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
69 lines (63 loc) · 1.54 KB
/
util.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
#include "util.h"
int **alloc_2d_matrix(int r, int c)
{
int ** a;
int i;
a = (int **)malloc(sizeof(int *) * r);
if (a == NULL) {
perror("memory allocation failure");
exit(0);
}
for (i = 0; i < r; ++i) {
a[i] = (int *)malloc(sizeof(int) * c);
if (a[i] == NULL) {
perror("memory allocation failure");
exit(EXIT_FAILURE);
}
}
return a;
}
void dealloc_2d_matrix(int **a, int r, int c)
{
int i;
for (i = 0; i < r; ++i)
free(a[i]);
free(a);
}
void print_2d_matrix(int **a, int r, int c){
for(int i = 0; i < r; i++) {
for(int j = 0; j < c;j++ ){
printf("%d ",a[i][j] );
}
printf("\n\n");
}
}
int** read_pgm_file(char * file_name, int *num_rows, int *num_columns)
{
FILE *inputFile = fopen(file_name, "r");
if(inputFile){
int success;
success = fscanf(inputFile, "%d", &*num_rows);
if(!success){
printf("Bad File format!\n");
return(NULL);
}
success = fscanf(inputFile, "%d", &*num_columns);
if(!success){
printf("Bad File format!\n");
return(NULL);
}
int i,j, int_tmp;
int** data=alloc_2d_matrix(*num_rows,*num_columns);
for (i = 0; i < (*num_rows); i++)
{
for (j = 0; j < (*num_columns); j++) {
fscanf(inputFile,"%d", &int_tmp);
data[i][j] = int_tmp;
}
}
fclose(inputFile);
return data;
}
return(NULL);
}