write a program that accepts an MxN matrix then do the following:
Find the sum of the main and opposite diagonal elements of an MxN matrix and adds the main diagonal of the matrix as well as the opposite diagonal of the matrix
#include <stdio.h>
#define SIZE 3
int main()
{
int Arr[SIZE][SIZE];
int r, c;
int sum = 0;
printf("Enter elements in matrix of size %dx%d: \n",SIZE,SIZE);
for(r=0; r<SIZE; r++)
{
for(c=0; c<SIZE; c++)
{
scanf("%d", &Arr[r][c]);
}
}
for(r=0; r<SIZE; r++)
{
for(c=0; c<SIZE; c++)
{
if(r+c == ((SIZE+1)-2))
{
sum += Arr[r][c];
}
}
}
printf("\nSum of minor diagonal elements = %d", sum);
return 0;
}
Comments
Leave a comment