SUM OF BOTH DIAGONALS IN 2D ARRAY

#include<iostream.h>
#include<conio.h>
#include<process.h>

int sum1(int a[50][50],int r)
{
int s=0;
for(int i=0;i<r;i++)
{ s=s+a[i][i];}
return(s);
}
int sum2(int a[50][50],int r)
{
int s=0;
int j;
j=r-1;
for(int i=0;i<r;i++)
s=s+a[i][j–];
return(s);
}

void row(int a[50][50],int r)
{
int s=0;
for(int i=0;i<r;i++)
{
s=0;
for(int j=0;j<r;j++)
{
s=s+a[i][j];

}
cout<<“Sum of Row “<<i+1<<” = “<<s<<endl;
}}

void col(int a[50][50],int r)
{ int s=0;
for(int i=0;i<r;i++)
{

s=0;
for(int j=0;j<r;j++)
{
s=s+a[j][i];

}
cout<<“Sum of Column “<<i+1<<” = “<<s<<endl;
}}
void main()
{
clrscr();
int i,s,j,r,c,ch,a[50][50];
x:
cout<<“Entr Array Limit–(Enter only Row as R=C)”<<endl;
cin>>r;

cout<<“Enter Array”<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
cin>>a[i][j];
}
}
y:
cout<<endl<<endl<<” Enter Choice :”<<endl<<“Sum of—- 1:main 2:Secondary 3.Rows 4.Columns 5.Re-enter 6.Exit “<<endl;
cin>>ch;
switch(ch)
{
case 1:
s=sum1(a,r);
cout<<“Sum = “<<s<<endl;
goto y;
case 2 :
s=sum2(a,r);
cout<<“Sum = “<<s<<endl;
goto y;

case 3:
row(a,r);
goto y;

case 4:
col(a,r);
goto y;
case 5:
goto x;

case 6:
exit(0);

default :
cout<<“Wrong Choice”<<endl;
break;

}
getch();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s