I'm suppose to implement in the function make2Darray to make the elements in the 2D array correspond with the row number.
The other parts of the code that prints and frees was already given, so no need to worry about that. I'm only suppose to touch the make2Darray function. However, in that function, the allocating part was also given. So the only code I am to alter is the part where I change the elements in the 2D array.
int** make2Darray(int width, int height) {
int **a;
int i = 0;
int j = 0;
/*allocate memory to store pointers for each row*/
a = (int **)calloc(height, sizeof(int *));
if(a != NULL) {
/* allocate memory to store data for each row*/
for(i = 0; i < height; i++) {
a[i] = (int *)calloc(width, sizeof(int));
if(a[i] == NULL) {
/* clean up */
free2Darray(a, height);
return NULL; /*aborting here*/
}
}
}
/* from this point down is the part I implemented, all code above was
given*/
if (height < 0 && width < 0) {
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
a[i][j] = j;
}
}
}
return a;
}
The elements in the 2D array is suppose to correspond to the row number If height = 4 and width = 3
0 0 0
1 1 1
2 2 2
3 3 3
However, I always get 0s which was the default setting when I got the code
0 0 0
0 0 0
0 0 0
0 0 0