Question: The program below uses a 1d array and a 2d jagged array. The numbers in the 1d array store the number of columns to be

The program below uses a 1d array and a 2d jagged array. The numbers in the 1d array store the number of columns to be used for each row in the 2d array. For example, if the 1d array consists of numbers 4, 3, 5, then the first row in the 2d array has 4 elements, the second row in the 2d array has 3 elements, and the third row in the 2d array has 5 elements.

All that is left is to replace the comments with code:

#include

#include

void assign(int** matrix, int *cols, int row);

// provide a prototype for myprint function

int main(void) {

int row, column, i;

int *cols; // a pointer for a 1D array to store the array of

ints that indicate the number of columns for each 2d row

int **matrix;

printf("Enter the number of rows for your matrix: ");

scanf("%d", &row);

cols = // dynamically allocate a 1d int array of size row

matrix = malloc(sizeof(int *) * row);

if (cols != NULL and matrix != NULL) {

for (i = 0; i < row; i++) {

printf("Enter the number of columns for row %d: ", i);

scanf("%d", &column);

cols[i] = column;

matrix[i] = // allocate a 1d int array of size column

that inializes the array to zeros

// print 2D array before filling it with data

myprint(matrix, cols, row);

// assign the 2D array

assign(matrix, cols, row);

// print 2D array after filling it with data

myprint(matrix, cols, row);

}

return 0;

}

// write the definition for myprint that prints matrix contents

void assign(int** matrix, int *cols, int row) {

int i, j;

for (i = 0; i < row; i++)

for (j = 0; j < cols[i]; j++)

if ((i + j) % 2)

matrix[i][j] = i + j;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!