Write a program which prompts for the number of rows and columns in a 2D matrix
ID: 3655957 • Letter: W
Question
Write a program which prompts for the number of rows and columns in a 2D matrix and then creates 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 sums: 34 38 42 46 50 The output should be formatted so that the columns and column sums line up. 1. All functions should be written AFTER the main procedure. 12. A function prototype should be written for each function and placed BEFORE the main procedure. 3. Each function should have a comment explaining what it does. 4. Each function parameter should have a comment explaining the parameter. 5. Declare GLOBAL constant variables storing the maximum number of rows and maxi- mum number of columns. Both of these should be set to 50. 6. Prompt and read the number of matrix rows and number of matrix columns. Note that the matrix should be allocated to have size (max num rows) by (max num columns). Only a subset of those rows and columns are used. 7. Write a function set_matrix() 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.) The function should take three parameters, the matrix, the number of rows in the matrix and the number of columns in the matrix. Note that the matrix argument has size (max num rows) by (max num columns). The function modies the matrix but does not return any value. 8. Write a function print_matrix() which prints the elements of the matrix, properly formatted in rows and columns. Use setw from the iomanip library to align the columns. You may assume that each matrix entry has at most 3 digits. The function should take 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. 9. In the main program, call the functions set_matrix and then print_matrix to set and print the matrix. 10. Write a function sum_column 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. Note that matrix columns are numbered starting at 0. The function returns the sum of the elements of a matrix column. 11. Write a function 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. You may assume that each sum has at most 4 digits. You may have to add space between matrix columns in print_matrix() to get the appropriate alignment with the column sums. 2The function should take 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. 12. In the main program, print the line Column sums:".Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#define rowSIZE 5
#define colSIZE 5
int myMatrix[rowSIZE][colSIZE];
int myFunction(void)
{
int row = 0,
col = 0;
for (row = 0; row < rowSIZE; row++)
for (col = 0; col < colSIZE; col++)
printf("Please enter the data for our %d by %d array: ", rowSIZE, colSIZE);
scanf("%i", &myMatrix[row][col]);
}
int printArray(int myMatrix[rowSIZE][colSIZE])
{
int row, col;
for ( row = 0; row < rowSIZE ; row++ )
for ( col = 0; col < colSIZE ; col++ )
printf("%d ", myMatrix[ row ][ col ]);
printf(" ");
}
int sumArray(int myMatrix[rowSIZE][colSIZE])
{
int row, col, sum = 0;
for ( row = 0; row < rowSIZE ; row++ ) {
for ( col = 0; col < colSIZE ; col++ )
sum += myMatrix[row][col];
return sum ;
}
int main (void)
{
int sum = 0;
int row, col;
int arrayToSum[rowSIZE][colSize];
for ( row = 0; row < ROWSIZE ; row++ ) {
for ( col = 0; col < COLSIZE ; col++ )
{
arrayToSum[row][col] = myFunction(myMatrix[row][col]);
}
for ( row = 0; row < ROWSIZE ; row++ ) {
for ( col = 0; col < COLSIZE ; col++ )
{
arrayToSum[row][col] = printArray(myMAtrix[row][col])
}
for ( row = 0; row < ROWSIZE ; row++ ) {
for ( col = 0; col < COLSIZE ; col++ )
{
arrayToSum[row][col] = sumArray(int myMatrix[row][col])
}
printf("This is the sum of our matrix: %d ", sum);
return sum;
o The C++ programming . 2D arrays :
#include <stdio.h>
#include <stdlib.h>
#define rowSIZE 5
#define colSIZE 5
int myMatrix[rowSIZE][colSIZE];
int myFunction(void)
{
int row = 0,
col = 0;
for (row = 0; row < rowSIZE; row++)
for (col = 0; col < colSIZE; col++)
printf("Please enter the data for our %d by %d array: ", rowSIZE, colSIZE);
scanf("%i", &myMatrix[row][col]);
}
int printArray(int myMatrix[rowSIZE][colSIZE])
{
int row, col;
for ( row = 0; row < rowSIZE ; row++ )
for ( col = 0; col < colSIZE ; col++ )
printf("%d ", myMatrix[ row ][ col ]);
printf(" ");
}
int sumArray(int myMatrix[rowSIZE][colSIZE])
{
int row, col, sum = 0;
for ( row = 0; row < rowSIZE ; row++ ) {
for ( col = 0; col < colSIZE ; col++ )
sum += myMatrix[row][col];
return sum ;
}
int main (void)
{
int sum = 0;
int row, col;
int arrayToSum[rowSIZE][colSize];
for ( row = 0; row < ROWSIZE ; row++ ) {
for ( col = 0; col < COLSIZE ; col++ )
{
arrayToSum[row][col] = myFunction(myMatrix[row][col]);
}
for ( row = 0; row < ROWSIZE ; row++ ) {
for ( col = 0; col < COLSIZE ; col++ )
{
arrayToSum[row][col] = printArray(myMAtrix[row][col])
}
for ( row = 0; row < ROWSIZE ; row++ ) {
for ( col = 0; col < COLSIZE ; col++ )
{
arrayToSum[row][col] = sumArray(int myMatrix[row][col])
}
printf("This is the sum of our matrix: %d ", sum);
return sum;
}
rgds
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.