Write C Program to Find the Sum of Each Row and Column of a 2x2 Matrix - 15 marksSample InputEnter the co-efficients of the matrix23 4580 97Sample OutputSum of the 0 row is = 68Sum of the 1 row is = 177Sum of the 0 column is = 103Sum of the 1 column is = 142
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 matrix23 4580 97Sample OutputSum of the 0 row is = 68Sum of the 1 row is = 177Sum of the 0 column is = 103Sum of the 1 column is = 142
Solution
To solve this problem, we need to write a C program that calculates the sum of each row and column of a 2x2 matrix. Here are the steps to do that:
-
Start by including the necessary header files:
#include <stdio.h> -
Define a function to calculate the sum of each row and column:
void calculateSum(int matrix[2][2]) { int rowSum[2] = {0}; // Array to store the sum of each row int colSum[2] = {0}; // Array to store the sum of each column // Calculate the sum of each row and column for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { rowSum[i] += matrix[i][j]; colSum[j] += matrix[i][j]; } } // Print the sum of each row for (int i = 0; i < 2; i++) { printf("Sum of the %d row is = %d\n", i, rowSum[i]); } // Print the sum of each column for (int j = 0; j < 2; j++) { printf("Sum of the %d column is = %d\n", j, colSum[j]); } } -
In the main function, declare a 2x2 matrix and prompt the user to enter the coefficients:
int main() { int matrix[2][2]; printf("Enter the coefficients of the matrix:\n"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { scanf("%d", &matrix[i][j]); } } calculateSum(matrix); return 0; } -
Compile and run the program. Enter the coefficients of the matrix when prompted. The program will calculate and display the sum of each row and column.
Sample Input:
Enter the coefficients of the matrix:
2 3
4 5
Sample Output:
Sum of the 0 row is = 5
Sum of the 1 row is = 9
Sum of the 0 column is = 6
Sum of the 1 column is = 8
This program calculates the sum of each row and column of a 2x2 matrix and displays the results.
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
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 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
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 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.
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.