Question
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
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
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 // TODO: print initial table for (i=0;i // 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 < num_rows; i++) for(j = 0; j < num_columns; j++) fscanf(fp, "%d", &a[i][j]); fclose(fp); // TO DO: print updated table // TO DO: free memory space allocated to table return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started