Please compute the average of each row and the sum of each column. For the avera
ID: 3750160 • Letter: P
Question
Please compute the average of each row and the sum of each column. For the average, please use integer division.
Specifics:
Write a function that will take in a 20x5 matrix. The function signature should look:
void tabulate(int matrix[20][5]);
Use the main function template below that will get the values from standard input (scanf). This will allow input values from a file from the command line via an input.txt:
#include <stdio.h>
void tabulate(int matrix[20][5])
{
// code here
}
int main()
{
int matrix[3][3];
int i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &matrix[i][j]);
}
}
// code here.
return 0;
}
Please have the output in the form of each number separated by a space. No punctuation or any other commentary. The order should be: RowAve_1 RowAve_2 ... RowAve_20 ColSum_1 ColSum_2 ... ColSum_5.
If you were to use this 3x3 matrix, the output should look like:
2 4 9
4 5 3
6 8 7
C:UsersDirectory>gcc YourFile.c
C:UsersDirectory>a < input.txt
5 4 7 12 17 19
Explanation / Answer
//when using 3*3 matrix tabulate , argument list of function should be changed from matrix[20][5]
#include <stdio.h>
void tabulate(int matrix[3][3])
{
// code here
int i,j,k;
int row_sum[3]= {0},col_sum[3] ={0};
//output the matrix
printf("Given matrix: ");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d ",matrix[i][j]);
}
printf(" ");
}
//calculate each row sum to calculate avg for each row.
k = 0;
for(i= 0; i < 3; i++)
{
for(j =0; j < 3; j++)
{
row_sum[k]+=matrix[i][j];
}
k++;
}
//now calculate each column sum to calcualte avg of each column
k = 0;
for(i= 0; i < 3; i++)
{
for(j =0; j < 3; j++)
{
col_sum[k]+=matrix[j][i];
}
k++;
}
//now display row avarage
printf("Row avarage: ");
for(i = 0; i < 3; i++)
{
printf("%.2f ",(float)row_sum[i]/3);
}
//now display column avarage
printf(" Column avarage: ");
for(i = 0; i < 3; i++)
{
printf("%.2f ",(float)col_sum[i]/3);
}
}
int main()
{
int matrix[3][3];
int i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &matrix[i][j]);
//printf("%d ",matrix[i][j]);
}
//printf(" ");
}
// code here.
//call the function tabulate
tabulate(matrix);
return 0;
}
--------------------------------------------
//output
Given matrix:
2 4 9
4 5 3
6 8 7
Row avarage:
5.00 4.00 7.00
Column avarage:
4.00 5.67 6.33
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.