Find Sum of each Row and each Column using Functions and PointersWrite a C program to find the sum of elements of each row and sum of elements of each column of a two-dimensional array using pointers.Note: Write the functions read1(), display(), sumOfEachRow() and sumOfEachColumn() in Program1009a.c.Sample Test CasesTest Case 1:Expected Output:Enter·row·and·column·sizes·:·2 2Enter·4·elements·:·1 2 3 4The·given·matrix·is1·2·3·4·Sum·of·row·-·0·elements·=·3Sum·of·row·-·1·elements·=·7Sum·of·column·-·0·elements·=·4Sum·of·column·-·1·elements·=·6Test Case 2:Expected Output:Enter·row·and·column·sizes·:·3 3Enter·9·elements·:·1 2 3 4 5 6 7 8 9The·given·matrix·is1·2·3·4·5·6·7·8·9·Sum·of·row·-·0·elements·=·6Sum·of·row·-·1·elements·=·15Sum·of·row·-·2·elements·=·24Sum·of·column·-·0·elements·=·12Sum·of·column·-·1·elements·=·15Sum·of·column·-·2·elements·=·18Test Case 3:Expected Output:Enter·row·and·column·sizes·:·3 2Enter·6·elements·:·11 22 33 44 55 66The·given·matrix·is11·22·33·44·55·66·Sum·of·row·-·0·elements·=·33Sum·of·row·-·1·elements·=·77Sum·of·row·-·2·elements·=·121Sum·of·column·-·0·elements·=·99Sum·of·column·-·1·elements·=·132
Question
Find Sum of each Row and each Column using Functions and PointersWrite a C program to find the sum of elements of each row and sum of elements of each column of a two-dimensional array using pointers.Note: Write the functions read1(), display(), sumOfEachRow() and sumOfEachColumn() in Program1009a.c.Sample Test CasesTest Case 1:Expected Output:Enter·row·and·column·sizes·:·2 2Enter·4·elements·:·1 2 3 4The·given·matrix·is1·2·3·4·Sum·of·row·-·0·elements·=·3Sum·of·row·-·1·elements·=·7Sum·of·column·-·0·elements·=·4Sum·of·column·-·1·elements·=·6Test Case 2:Expected Output:Enter·row·and·column·sizes·:·3 3Enter·9·elements·:·1 2 3 4 5 6 7 8 9The·given·matrix·is1·2·3·4·5·6·7·8·9·Sum·of·row·-·0·elements·=·6Sum·of·row·-·1·elements·=·15Sum·of·row·-·2·elements·=·24Sum·of·column·-·0·elements·=·12Sum·of·column·-·1·elements·=·15Sum·of·column·-·2·elements·=·18Test Case 3:Expected Output:Enter·row·and·column·sizes·:·3 2Enter·6·elements·:·11 22 33 44 55 66The·given·matrix·is11·22·33·44·55·66·Sum·of·row·-·0·elements·=·33Sum·of·row·-·1·elements·=·77Sum·of·row·-·2·elements·=·121Sum·of·column·-·0·elements·=·99Sum·of·column·-·1·elements·=·132
Solution
Here is a C program that accomplishes the task:
#include <stdio.h>
void read1(int *a, int m, int n) {
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
scanf("%d", (a + i*n + j));
}
}
}
void display(int *a, int m, int n) {
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
printf("%d ", *(a + i*n + j));
}
printf("\n");
}
}
void sumOfEachRow(int *a, int m, int n) {
for(int i=0; i<m; i++) {
int sum = 0;
for(int j=0; j<n; j++) {
sum += *(a + i*n + j);
}
printf("Sum of row - %d elements = %d\n", i, sum);
}
}
void sumOfEachColumn(int *a, int m, int n) {
for(int i=0; i<n; i++) {
int sum = 0;
for(int j=0; j<m; j++) {
sum += *(a + j*n + i);
}
printf("Sum of column - %d elements = %d\n", i, sum);
}
}
int main() {
int m, n;
printf("Enter row and column sizes : ");
scanf("%d %d", &m, &n);
int a[m][n];
printf("Enter %d elements : ", m*n);
read1((int *)a, m, n);
printf("The given matrix is\n");
display((int *)a, m, n);
sumOfEachRow((int *)a, m, n);
sumOfEachColumn((int *)a, m, n);
return 0;
}
This program first reads the number of rows and columns from the user. Then it reads the elements of the matrix. It displays the matrix and calculates the sum of each row and each column using pointers.
Similar Questions
Write a program to obtain a matrix and find the sum of each row and each column.Input format :The first line of the input consists of the value of the number of rows and the number of columns.The second line of the input consists of a matrix.Output format :The output prints the sum of each row and each column.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :4 41 2 3 45 6 7 89 10 11 1213 14 15 16Output 1 :Sum of the row 0 = 10Sum of the row 1 = 26Sum of the row 2 = 42Sum of the row 3 = 58Sum of the column 0 = 28Sum of the column 1 = 32Sum of the column 2 = 36Sum of the column 3 = 40Input 2 :3 398 87 6545 32 2845 56 58Output 2 :Sum of the row 0 = 250Sum of the row 1 = 105Sum of the row 2 = 159Sum of the column 0 = 188Sum of the column 1 = 175Sum of the column 2 = 151
int calculateMatrixSum(int rows, int columns, int **matrix) { int i, j, sum=0; if((rows>0)&&(columns>0)) { for(i=0;i<rows;i++) { sum =0; for(j=0;j<columns;j++) { if(i==j) { if(matrix[i][j]/2!=0) sum += matrix[i][i]; } } } return sum; } else return sum; }
Write C Program to Find the Sum of Each Row and Column of a 2x2 Matrix - 15 marksSample InputEnter the co-efficients of the matrix
Write a C Program to Compute the Sum of Diagonals of a square matrix. Print the sum of both diagonals separately. Get the number of rows and columns from the user.
Write a C program that calculates the sum of both diagonals in a square matrix. Implement a function that takes a square matrix and its size as input and returns the sum of the main diagonal and anti-diagonal elements.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.