Here is the program to find the sum of the secondary diagonal elements (From right top to left bottom ) of the entered integer square matrix. The program asks for the order (M, N) [where M should equal N] of the matrix. Soon after entering the order, the cursor takes to the proper position in the screen to input the matrices. User need to hit ‘Enter’ button after each entry. Matrix is stored as A.
Logic : The secondary diagonal elements will have a constant relation in their row and column column count. As the row count increases, the column count decreases, to maintain the sum of the two to ( M+1) where M is the order. By keeping this in mind, we can trace the matrix till the end using the help of a for loop. The flag ‘sum’ is updated in each of the iterations.
Finally, we’ll get the sum of the diagonal elements after the end of the while loop.
You are always welcome with your suggestion and doubts into our discussion forum.
#include<stdio.h>
void main()
{
int A[5][5],i,j,m,n,sum = 0;
clrscr();
printf(“nnt ENTER A ORDER OF THE MATRIX M,N…: “);
scanf(“%d,%d”,&m,&n);
printf(“nnt ENTER THE ELEMENTS OF THE MATRIX..:nn”);
if(m == n)
{
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
gotoxy(20+j*4,12+i*2);
scanf(“%d”,&A[i][j]);
}
printf(“n”);
}
for(i=1;i<=m;i++)
sum = sum + A[i][n+1-i];
printf(“nt THE SUM OF SECONDARY DIAGONAL OF A MATRIX IS…: %d”, sum);
}
else
{
printf(“nt THE ORDER OF THE MATRIX IS NOT CORRECT”);
printf(“nnt HELP : ‘M’ SHOULD BE EQUAL TO ‘N’”);
}
getch();
}
Download exe and source code here.
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
February 12th, 2011 at 9:54 pm
source code has numerous errors ……..rectify it soon
March 30th, 2011 at 8:32 pm
its givin an error ‘misplaced else’
June 23rd, 2011 at 2:09 pm
this programme is not working.plz rectify soon
January 18th, 2012 at 4:45 am