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
Question
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
Solution
Sure, here is a step-by-step solution to find the sum of each row and column of a 2x2 matrix in C:
- Start by including the necessary header files:
#include <stdio.h>
- Define a constant for the size of the matrix:
#define SIZE 2
- Declare a function to calculate the sum of each row and column:
void calculateSum(int matrix[SIZE][SIZE]) {
int rowSum[SIZE] = {0};
int colSum[SIZE] = {0};
// Calculate the sum of each row and column
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
rowSum[i] += matrix[i][j];
colSum[j] += matrix[i][j];
}
}
// Print the sum of each row
printf("Sum of each row: ");
for (int i = 0; i < SIZE; i++) {
printf("%d ", rowSum[i]);
}
// Print the sum of each column
printf("\nSum of each column: ");
for (int j = 0; j < SIZE; j++) {
printf("%d ", colSum[j]);
}
}
- In the main function, declare a 2x2 matrix and prompt the user to enter the coefficients:
int main() {
int matrix[SIZE][SIZE];
printf("Enter the coefficients of the matrix:\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Call the calculateSum function
calculateSum(matrix);
return 0;
}
- Compile and run the program. Enter the coefficients of the matrix when prompted, and the program will calculate and display the sum of each row and column.
Note: This solution assumes that the user will enter valid integer coefficients for the matrix. You may want to add error handling code to handle invalid input.
I hope this helps! Let me know if you have any further questions.
Similar Questions
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.Input3 31 2 3 4 5 6 7 8 9OutputSum of the main diagonal elements: 15Sum of the anti-diagonal elements: 15
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
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.
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
Write a program to obtain a matrix and find the sum of the elements in the lower triangular matrix.Note: Only square matrixInput format :The first line of the input consists of the number of rows and columns.The second line of the input consists of the matrix element.Output format :The output prints the sum of the lower triangular matrix.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 312 23 4556 78 8995 51 20Output 1 :312
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.