i need to create a 2d table array print it them i need to read in a file then print 5188600
I need to create a 2D table/array print it them I need to read in a file then print the new table with the file contents in C(not C++):
You will create and initialize a 2-D table of num_rows rows and num_columns columns containing entries of type entry_t (defined in hw1.c). The c and n fields of each entry should be initialized to ' ' (blank space) and 0, respectively.
You will then print the table. By adding parentheses, commas, and blank spaces as needed and using formatted output, your output should be:
( ,0) ( ,0) ( ,0) ( ,0)
( ,0) ( ,0) ( ,0) ( ,0)
( ,0) ( ,0) ( ,0) ( ,0)
Next, you will open the (provided) file data.txt, read it line by line and update table entries based on the content of each line as follows: if the line contains (i,j):(d,m) (where i, j, and m are integers and d is a character) you will update the table entry in row i and column j so field c is 'd' and field n has value m.
When the end of the file data.txt is reached, close the file and print the updated table. You should obtain:
(f,4) ( ,0) ( ,0) ( ,0)
( ,0) (x,7) ( ,0) ( ,0)
( ,0) ( ,0) ( ,0) (d,9)
Finally, free the memory space allocated for the table.
This is what I have so far, if you see any mistakes please let me know:
#include // to use printf, FILE, fopen, fgets, fclose, sscanf
#include // to use malloc, free
typedef struct { // define new struct type for storing
char c; // a char and an int
int n;
} entry_t;
typedef entry_t* row_t; // define row type
typedef row_t* table_t; // define table type
table_t t;
int main()
{
int num_rows = 3;
int num_columns = 4;
// TODO: create and initialize 2-D table
int i,j;
t = (row_t*) malloc(sizepf(row_t) *num_rows);
for (i=0; i t[i]=(entry_t*) malloc(sizeof(entry_t) *num_columns);
for(j=0;j t[i][j] = 0;
}
}
// TODO: print initial table
for (i=0;i for (j=0;j printf(“%d”, t[i][j]);
printf(“n”);
// TO DO: open file data.txt, read it line by line, and
// update table entries; close file when done.
FILE *fp = fopen(“data.txt”,”r”);
for(i = 0; i
for(j = 0; j
fscanf(fp, “%d”, &a[i][j]);
fclose(fp);
// TO DO: print updated table
// TO DO: free memory space allocated to table
return 0;
}