Monday, May 5, 2008

Using two diamensional array to find sum of diagonal in c++

Write a program that will find out the sum of diagonal of two dimensional array of A [N] [N].

PROGRAM:

#include <iostream.h>

#include <conio.h>


void main() {

int a[10][10];

int r,c,i,j,sum=0;

do {

clrscr();

cout<<"\nEnter the number of rows for the matrix: ";

cin>>r;

cout<<"\nEnter the number of cols for the matrix: ";

cin>>c;


} while(c!=r);

cout<<"\nEnter values for matrix a: \n";

for(i=0;i<r;i++) {

for(j=0;j<c;j++) {

cout<<"a["<<i<<"] ["<<j<<"] : ";

cin>>a[i][j];

if(i==j) {

sum+=a[i][j];

}

}

}

cout<<"sum of the diagonal of two dimensional array: "<<sum;

getch();

}

No comments: