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:
Post a Comment