Sum of Diagonal Elements of Matrix using C Languagae

Matrix:

A general 2 * 2 matrix A[i,j] is given below where 2*2 is the order of matrix. There are two rows and two columns. It consists of four elements.There are two diagonal elements A00  and  A11.
A [ i , j ] =

Now what you have to do is to sum these two diagonal elements. Now matrix may be pre defined in the program or you can ask the user to enter the elements 2*2 matrix and then go for next step. In my program i ask the user to enter the elements of matrix.

Note: You can see that diagonal elements are those where subscripts are equal like A00  and A11.

Explanation of Program code:

To take values from the user ,i define array of type integer , two for loops are used if you have no idea of for loop and arrays and how it works , read this article of few paragraphs

As there are two rows and two columns , two nested for loops are used to take values from the user and store in the array.
After the values taken form user , i display the elements of matrix.
After this i wrote the code for to check where subscripts of matrix i and j are equal. Remember elements stores like this in the array in positions as shown below because value for i and j starts from zero not from one.
Elements stores in Array a[2]][2]


Algorithm:

Both the loops runs twice because minimum value for both i and j is 0 and maximum value is less than 2 ,(not equal to 2).

First loop(outer loop) when runs for the first time when i = 0 , second loop ( inner loop ) runs twice (for j = 0 , j = 1)
so when i = 0 and j = 0 it means A00   
when i = 0 and j = 1 it means A01

First loop(outer loop) when runs for the second time when i = 1 , second loop ( inner loop ) runs twice (for j = 0 , j = 1)
when i = 1 and j = 0 it means  A10

when i = 1 and j = 1 it means  A11

so statement below only adds the diagonal elements
if( i == j)
sum = sum + a[i][j];       or  sum += a[i][j]            same thing.

Program Code:

#include<stdio.h>

int main()
{
printf("************************************************\n");
printf("        Diagonal Elements Sum Program\n");
printf("************************************************\n\n\n");
int a[2][2] , sum = 0;
int i =0,  j= 0;
printf("Enter Elements of 2*2 matrix :\n");
for( i=0 ; i < 2 ; i++)
{
for( j=0; j < 2 ; j++)
{
scanf("%d" , &a[i][j]);
}
}
printf("\n\nElements of 2*2 Matrix are as follows:\n\n");
for( i=0 ; i < 2 ; i++)
{
for( j=0;  j < 2  ; j++)
{
   printf("\t\t");
printf("%d" , a[i][j]);
}
printf("\n");
}
// code for diagonal elements
for(i = 0 ; i < 2 ; i++)
{
for(j=0 ; j < 2; j++)
{
if ( i == j )
sum+= a[i][j];      // sum = sum + a[i][j]
}
}
printf("\n\nSum of diagnol elements of 2*2 matrix is  =   %d", sum);
printf("\n************************************************");
return 0;
}

Output:

Output

Download Program Code:

You can also download program code from this as follows.

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.