C Program to find Sum of Opposite Diagonal Elements of a Matrix

How to write a C Program to find Sum of Opposite Diagonal Elements of a Matrix?. Or How to write a C program to find Sum of Opposite Diagonal Elements of a Multi-Dimensional Array with example.

C Program to find Sum of Opposite Diagonal Elements of a Matrix 1

C Program to find Sum of Opposite Diagonal Elements of a Matrix Example 1

This program allows the user to enter the number of rows and columns of a Matrix. Next, we are going to calculate the sum of opposite diagonal elements in this matrix using For Loop.

/* C Program to find Sum of Opposite Diagonal Elements of a Matrix */

#include<stdio.h>
 
int main()
{
 	int i, j, rows, columns, a[10][10], Sum = 0;
  
 	printf("\n Please Enter Number of rows and columns  :  ");
 	scanf("%d %d", &i, &j);
 
 	printf("\n Please Enter the Matrix Elements \n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0;columns < j;columns++)
    	{
      		scanf("%d", &a[rows][columns]);
    	}
  	}
   	  
 	for(rows = 0; rows < i; rows++)
  	{
   		Sum = Sum + a[rows][i - rows  - 1];
  	}
 
 	printf("\n The Sum of Opposite Diagonal Elements of a Matrix =  %d", Sum );

 	return 0;
}
C Program to find Sum of Opposite Diagonal Elements of a Matrix 2

In this C Program to find Sum of Opposite Diagonal Elements of a Matrix, We declared single Two dimensional arrays Multiplication of size of 10 * 10.
Below statements asks the User to enter the Matrix size (Number of rows and columns. For instance 2 Rows, 3 Columns = a[2][3] )

printf("\n Please Enter Number of rows and columns  :  ");
scanf("%d %d", &i, &j);

Next, we used for loop to iterate every cell present in a[3][3] matrix. Conditions inside the for loops ((rows < i) and (columns < j)) will ensure the compiler, not to exceed the matrix limit. Otherwise, the Matrix will overflow

The C Programming scanf statement inside the for loop will store the user entered values in every individual array element such as a[0][0], a[0][1], …..

for(rows = 0; rows < i; rows++).
{
  for(columns = 0; columns < j; columns++)
   {
     scanf("%d", &a[rows][columns]);
   }
}

In the next line, We have one more for loop to find Sum of Diagonal Elements of a Matrix

for(rows = 0; rows < i; rows++)
{
   		Sum = Sum + a[rows][i - rows  - 1];
}

User inserted values for C Program to find Sum of Opposite Diagonal Elements of a Matrix are: a[3][3] = {{10, 20, 30}, { 40, 50, 60}, {70, 80, 90}}

Row First Iteration: for(rows = 0; rows < 3; 0++)
The condition (0 < 3) is True.
Sum  = Sum + a[rows][i – rows – 1]
Sum  = Sum + a[0][3 – 0 – 1] => Sum + a[0][2]
Sum  = 0 + 30 = 30

Row Second Iteration: for(rows = 1; rows < 3; 1++)
The condition (1 < 3) is True.
Sum  = Sum + a[1][3 – 1 – 1] => Sum + a[1][1]
Sum  = 30 + 55 = 85

Row Second Iteration: for(rows = 2; rows < 3; 2++)
The condition (2 < 3) is True.
Sum  = Sum + a[2][3 – 2 – 1] => Sum + a[2][0]
Sum  = 85 + 95 = 180

Next, rows value incremented. After the increment, the condition inside the for loop (rows < 3) will fail. So it will exit from the loop.

At last, we used the printf statement to print the total Sum as output

C Program to find Sum of Opposite Diagonal Elements of a Matrix Example 2

This C program for sum of matrix diagonally opposite elements is the same as above, but this time we changed the algorithm a bit.

/* C Program to find Sum of Opposite Diagonal Elements of a Matrix */

#include<stdio.h>
 
int main()
{
 	int i, j, rows, columns, a[10][10], Sum = 0;
  
 	printf("\n Please Enter Number of rows and columns : ");
 	scanf("%d %d", &i, &j);
 
 	printf("\n Please Enter the Matrix Elements \n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0;columns < j;columns++)
    	{
      		scanf("%d", &a[rows][columns]);
    	}
  	}
   	  
 	for(rows = 0; rows < i; rows++)
  	{
  		for(columns = 0;columns < j; columns++)
  		{
  			if(rows + columns == ((i + 1) - 2))
  			       Sum = Sum + a[rows][columns];
		}
  	}
 
 	printf("\n The Sum of Opposite Diagonal Elements of a Matrix =  %d", Sum );

 	return 0;
}

C sum of matrix opposite diagonals output

 Please Enter Number of rows and columns : 3 3

 Please Enter the Matrix Elements 
1 2 3
4 5 6
7 8 15

 The Sum of Opposite Diagonal Elements of a Matrix =  15

User inserted values are: a[3][3] = {{1, 2, 3}, { 4, 5, 6}, {7, 8, 15}}

Row First Iteration: for(rows = 0; rows < 3; 0++)
The condition (0 < 3) is True. So, it will enter into second for loop

Column First Iteration: for(columns = 0; 0 < 3; 0++)
The condition (columns < 3) is True. So, it will start executing the If Statement

if(rows + columns == ((i + 1) – 2))
=> if(0 + 0 == ((3 + 1) – 2)) – Condition is False

Column Second Iteration: for(columns = 1; 1 < 3; 1++)
The condition (1 < 3) is True.

if(rows + columns == ((i + 1) – 2))
=> if(0 + 1 == ((3 + 1) – 2)) – Condition is False

Column Second Iteration: for(columns = 2; 2 < 3; 2++)
The condition (1 < 3) is True.

if(rows + columns == ((i + 1) – 2))
=> if(0 + 2 == ((3 + 1) – 2)) – Condition is True
Sum = Sum + a[rows][columns] => Sum + a[0][2]
Sum = 0 + 3 = 3

Do the same for remaining iteration where rows = 1 and rows = 2