Program to show the Multiple use of Matrix :)
#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];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
y:
cout<<"\nEnter Choice :"<<endl<<"Sum of---- \n\t1:main \n\t2:Secondary \n\t3.Rows \n\t4.Columns \n\t5.Re-enter \n\t6.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();
}
Here is the Output :)
No comments:
Post a Comment