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.
Question
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.
Solution
Sure, here is a simple C program that calculates the sum of both diagonals in a square matrix:
#include <stdio.h>
// Function to calculate sum of diagonals
int diagonalSum(int matrix[10][10], int size) {
int mainDiagonalSum = 0, antiDiagonalSum = 0;
for (int i = 0; i < size; i++) {
mainDiagonalSum += matrix[i][i];
antiDiagonalSum += matrix[i][size - i - 1];
}
return mainDiagonalSum + antiDiagonalSum;
}
int main() {
int matrix[10][10] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int size = 3;
printf("Sum of Diagonal elements = %d", diagonalSum(matrix, size));
return 0;
}
In this program, the diagonalSum function calculates the sum of the main diagonal and anti-diagonal elements of the matrix. The main diagonal elements are those where the row index and column index are the same (i.e., matrix[i][i]), and the anti-diagonal elements are those where the row index and column index sum to size - 1 (i.e., matrix[i][size - i - 1]). The function then returns the sum of these two sums.
The main function initializes a 3x3 matrix and its size, and then calls the diagonalSum function to calculate and print the sum of the diagonal elements.
Similar Questions
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 program to obtain a matrix and find the sum of its diagonal elements.Note: Only square matrix.Input format :The input consists of the number of rows and columns separated by a space.The second line of the input is matrix elements.Output format :The output prints the sum of diagonal elements.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 31 2 34 5 67 8 9Output 1 :15Input 2 :4 412 23 45 5678 89 98 8765 54 32 2114 25 36 58Output 2 :191
Write a C Program to print lower diagonal of a matrix.
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 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.