Example of 2D Array Row, Column and Diagonal Sum in C++
#include <iostream.h>
#include <conio.h>
void getdata(int a[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"Enter data ";
cin>>a[i][j];
}
}
}
void display(int a[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
void diagnol(int a[][3])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
sum+=a[i][j];
}
}
}
cout<<"The sum of diagnol "<<sum;
}
void sum(int a[][3])
{
int i,j,sum;
cout<<"The sum of rows are "<<endl;
for(i=0;i<3;i++)
{
sum=0;
for(j=0;j<3;j++)
{
sum+=a[i][j];
}
cout<<sum<<endl;
}
cout<<"The sum of colums are "<<endl;
for(i=0;i<3;i++)
{
sum=0;
for(j=0;j<3;j++)
{
sum+=a[j][i];
}
cout<<sum<<" ";
}
cout<<endl;
}
void main ()
{
int b[3][3];
clrscr();
getdata(b);
display(b);
sum(b);
diagnol(b);
getch();
}
No comments
Post your comments