Thursday, February 10, 2011

this is a very very modified menu driven program to find the sum of all the diagonal operations....


#include<iostream.h>
#include<conio.h>
#include<process.h>
int smd(int a[10][10],int n)
{int i,j,sum=0;
  for(i=0;i<n;i++)
    for(j=0;j<n;j++)
      if(i==j)
        sum+=a[i][j];
          return(sum);
                          }
int sod(int a[10][10], int n)
  {int i,j,sum=0;
       for(i=0;i<n;i++)
         for(j=0;j<n;j++)
            if(i+j==n-1)
                sum+=a[i][j];
                  return(sum);
                                    }
int sut(int a[10][10],int n)
     {int i,j,sum=0;
           for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                if(j>i)
                 sum+=a[i][j];
                    return(sum);
     }
int slt(int a[10][10],int n)
{int i,j,sum=0;
   for(i=0;i<n;i++)
    for(j=0;j<n;j++)
     if(i>j)
       sum+=a[i][j];
          return(sum);
   }

void main()
{
clrscr();
int a[10][10],i,j,n,m,sum,k=0;
char choice,choice1;
cout<<"enter the no:of rows and colums"; endl;
cin>>m>>n;
if(m!=n)
   {cout<<"sum not possible"; endl;
    exit(0);}
    cout<<"enter the matrix"; endl;
for(i=0;i<n;i++)
  for(j=0;j<n;j++)
    cin>>a[i][j];
     cout<<"the matrix is\n";
for(i=0;i<n;i++)
 {for(j=0;j<n;j++)
{cout<<a[i][j];
 cout<<"\t";}
 cout<<"\n"; }

cout<<"choose the operation to be done\n";
cout<<"choose 1 for sum of main diagonal elements\n";
cout<<"choose 2 for sum of off diagonal elements\n";
cout<<"choose 3 for sum of upper triangle elements\n";
cout<<"choose 4 for sum of lower triangle elements\n";
 do{
cin>>choice;
switch (choice)
{case '1':sum=smd(a,n);
break;
case '2':sum=sod(a,n);
break;
case '3':sum=sut(a,n);
break;
case '4':sum=slt(a,n);
break;
default:
cout<<"please enter a choice";

}

cout<<"Therfore the sum obtained by doing the chosen operation is  "<<sum;
cout<<"\npress y  to repeat any other key to exit";
cin>>choice1;
if(choice1!='y')
exit(0);
else
k=1;
}while(k==1);
getch();
}

No comments:

Post a Comment