prompt for the number of rows and columns in a 2D matrix and then create a matri
ID: 3656474 • Letter: P
Question
prompt for the number of rows and columns in a 2D matrix and
then create a matrix containing integers 1, 2, 3, : : : up to the number of matrix elements.
Print the matrix and then print the sum of the elements of each column in the matrix.
For example, specifying 4 rows and 5 columns produces the following:
Enter number of rows and number of columns: 4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
Column sum:
34 38 42 46 50
--Declare global constant variables storing the maximum number of rows and maxi-
mum number of columns. Both of these should be set to 50.
--Prompt for the number of rows and columns
--In main, print the line " here are the column sums:". Call the function print_column_sum
to print the sums.
--make set_matrix() a function which sets the value of the elements of the matrix.
The (i; j)'th element of the matrix should have value i*num_cols+j+1. (Note that the
rows and columns are numbered starting at 0.)
Use three parameters, the matrix, the number of rows in the matrix and the number of columns in the matrix.
The function modifies the matrix but does not return any value.
--Make print_matrix() and Use setw from the iomanip to align the columns. You may assume that each matrix entry has at most 4 digits. three parameters, the matrix, the number of rows in the matrix and the number of columns in the matrix.
The function does not modify the matrix or return any value
--Make sum_column a function which computes and returns the sum of the elements of
a matrix column. The function should take four parameters, the column number, the matrix, the number of rows in the matrix and the number of columns in the matrix.
The function returns the sum of the elements of a matrix column.
--Make a function called print_column_sum which calls sum_column to compute the sum of
each column and prints the sum of each column, aligned with its column.
Assume that each sum has at most 5 digits. There are three parameters: the matrix, the number of rows in the
matrix and the number of columns in the matrix.
The function does not modify the matrix or return any value
Explanation / Answer
#include const int MAXROWS=50; const int MAXCOLUMNS=50; void set_matrix(int arr[][MAXCOLUMNS],int row,int columns); void print_matrix(int arr[][MAXCOLUMNS],int row,int columns); int sum_column(int colNum,int arr[][MAXCOLUMNS],int row,int columns); void print_column_sum(int arr[][MAXCOLUMNS],int row,int columns); int main(){ int DataArray[MAXROWS][MAXCOLUMNS]; int rows,columns; do{ printf("Enter number of rows and number of columns: "); scanf("%d %d",&rows,&columns); if((rowsMAXROWS)||( columnsMAXCOLUMNS)){ printf("The number of rows and the number of columns must be positive. "); } }while((rowsMAXROWS)||( columnsMAXCOLUMNS)); set_matrix(DataArray,rows,columns); print_matrix(DataArray,rows,columns); printf(" here are the column sums: "); print_column_sum(DataArray,rows,columns); getchar();getchar(); return 0; } void set_matrix(int arr[][MAXCOLUMNS],int row,int columns){ int i,j; for(i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.