I need to create and initialize a 2d array in C not C++ that takes 2 intergers r
ID: 3693416 • Letter: I
Question
I need to create and initialize a 2d array in C not C++ that takes 2 intergers rows and cols, then a list of num_rows x num_cols (char, int) and print it in table format.
Ex.
3 4
(f,4)( ,0)( ,0)( ,0)( ,0)(x,7)( ,0)( ,0)( ,0)( ,0)( ,0)(d,9)
The table should then read the input file or user input and update the table entries and print it in both table format and list format.
When it prints the list it should only print ones that aren't the default ( , 0) and each line should be formatted like "(i,j) : (d,m)"
Here's what I have:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char c;
int n;
} entry_t;
typedef entry_t* row_t;
typedef row_t* table_t;
table_t t;
int main(int argc, char *argv[])
{
int num_rows;
int num_cols;
free()
return 0;
}
Explanation / Answer
#include <stdio.h> // to use printf, FILE, fopen, fgets, fclose, sscanf
#include <stdlib.h> // 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;
int i, j;
// create and initialize 2-D table
// create rows
t = (row_t*) malloc(sizeof(row_t) * num_rows);
// create cells / colums for rows
for (i=0; i<num_rows; i++){
t[i]=(entry_t*) malloc(sizeof(entry_t) * num_columns);
for (j=0; j<num_columns; j++){
t[i][j].n = 0;
t[i][j].c = 0;
}
}
// Print the values of each cell in table t
for (i=0; i<num_rows; i++){
for (j=0; j<num_columns; j++)
printf("(%d, %u) ", t[i][j].c, t[i][j].n);
printf(" ");
}
printf(" ");
// Open and store file in an array
char filename[] = "data.txt";
FILE *file = fopen ( filename, "r" );
char row;
char column;
char cValue;
char nValue;
char conversionVal = '0';
if (file != NULL) {
char line [1000];
while(fgets(line,sizeof line,file) != NULL) /* read a line from a file */ {
fprintf(stdout,"%s ",line);
// print tests for wanted values in each row
printf("line[1]: %c ", line[1]);
row = line[1];
printf("row: %c ", row);
printf("line[3]: %c ", line[3]);
column = line[3];
printf("column: %c ", column);
printf("line[7]: %c ", line[7]);
cValue = line[7];
printf("cValue: %c ", cValue);
printf("line[9]: %c ", line[9]);
nValue = line[9];
printf("nValue: %c ", nValue);
i = row - conversionVal;
j = column - conversionVal;
if ((i < 0) || (j < 0))
break;
printf("i: %d ", i);
printf("j: %d ", j);
t[i][j].c = cValue;
t[i][j].n = nValue;
printf("t[%d][%d].c: %c ", i, j, t[i][j].c);
printf("t[%d][%d].n: %c ", i, j, t[i][j].n);
printf(" ");
i = 0;
j = 0;
}
fclose(file);
}
else {
perror(filename); //print the error message on stderr.
}
printf(" ");
// print updated table
for (i=0; i<num_rows; i++){
for (j=0; j<num_columns; j++) {
if (t[i][j].n == 0)
printf("(%d, %d) ", t[i][j].c, t[i][j].n);
else
printf("(%c, %c) ", t[i][j].c, t[i][j].n);
}
printf(" ");
}
printf(" ");
// free the dynammically allocated space before terminating
for (i=0; i<num_rows; i++){
free(t[i]);
}
free(t);
return 0;
}
data.txt
(0,0):(f,4)
(1,1):(x,7)
(2,3):(d,9)
sample output
(0, 0) (0, 0) (0, 0) (0, 0)
(0, 0) (0, 0) (0, 0) (0, 0)
(0, 0) (0, 0) (0, 0) (0, 0)
(0,0):(f,4)
line[1]: 0
row: 0
line[3]: 0
column: 0
line[7]: f
cValue: f
line[9]: 4
nValue: 4
i: 0
j: 0
t[0][0].c: f
t[0][0].n: 4
(1,1):(x,7)
line[1]: 1
row: 1
line[3]: 1
column: 1
line[7]: x
cValue: x
line[9]: 7
nValue: 7
i: 1
j: 1
t[1][1].c: x
t[1][1].n: 7
(2,3):(d,9)
line[1]: 2
row: 2
line[3]: 3
column: 3
line[7]: d
cValue: d
line[9]: 9
nValue: 9
i: 2
j: 3
t[2][3].c: d
t[2][3].n: 9
line[1]:
row:
line[3]: 3
column: 3
line[7]: d
cValue: d
line[9]: 9
nValue: 9
(f, 4) (0, 0) (0, 0) (0, 0)
(0, 0) (x, 7) (0, 0) (0, 0)
(0, 0) (0, 0) (0, 0) (d, 9)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.