Monday, February 28, 2011

the biggest program that i hav ever made....also a project for school..


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


void rowsum(int a[10][10],int m,int n)
{int i,j,s;
   for(i=0;i<m;i++)
    {s=0;
    for(j=0;j<n;j++)
    s+=a[i][j];
     cout<<"\nrow sum of "<<i<<"row ="<<s;
     cout<<"\n";}
}


void columnsum(int a[10][10],int m,int n)
{int i,j,s;
   for(j=0;j<n;j++)
    {s=0;
    for(i=0;i<m;i++)
    s+=a[i][j];
     cout<<"\ncolumn sum of"<<j<<"column ="<<s;
     cout<<"\n";}
}


void readmatrix(int a[10][10],int m,int n)
   {int i,j;
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
    cin>>a[i][j];
   }
void dispmatrix(int a[10][10],int m,int n)
{   int i,j;
    for(i=0;i<m;i++)
     {cout<<"\n";
    for(j=0;j<n;j++)
     {cout<<a[i][j];
    cout<<"\t";
     }
     }
}


void summatrix(int a[10][10],int m,int n,int b[10][10],int c[10][10])
  {int i,j;
   for(i=0;i<m;i++)
   for(j=0;j<n;j++)
   c[i][j]=a[i][j]+b[i][j];
  }


void diffmatrix(int a[10][10],int m,int n,int b[10][10],int c[10][10])
  {int i,j;
   for(i=0;i<m;i++)
   for(j=0;j<n;j++)
   c[i][j]=a[i][j]-b[i][j];
  }




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 sumposneg(int a[10][10],int m,int n)
{int i,j,sp=0,sn=0;
 for(i=0;i<m;i++)
 for(j=0;j<n;j++)
  if(a[i][j]<0)
   sn+=a[i][j];
  else
   sp+=a[i][j];

   cout<<"\nsum of positive  no ="<<sp;

   cout<<"\nsum of negative  no ="<<sn;

}



void transpose(int a[10][10],int b[10][10],int m,int n)

{int i,j;
  for(i=0;i<m;i++)
  for(j=0;j<n;j++)
  b[j][i]=a[i][j];
}



void main()

{clrscr();
int A[10][10],B[10][10],C[10][10],m,n,p,q;
int a[10][10],sum;
char choice,ch1;
do{

cout<<"this is a menu driven program to access..various functions on 2d arrays\n";
cout<<"enter a to find the rowsum of a 2d array\n";
cout<<"enter b to find the columnsum of a 2d array\n";
cout<<"enter c to find the sum of two matrixes\n";
cout<<"enter d to find the difference of two matrixes\n";
cout<<"enter e to find the sum of main diagonal elements of a matrix\n";
cout<<"enter f to find the sum of off diagonal elements of a matrix\n";
cout<<"enter g to find the sum of upper triangle elements of a matrix\n";
cout<<"enter h to find the sum of lower triangle elements ot a matrix\n";
cout<<"enter i to find the sum of positive and negative no's of a 2d array separately\n";
cout<<"enter j to find the transpose of a matrix\n";
cout<<"enter k to exit this program\n";



cin>>choice;

switch(choice)
{
case 'a':

cout<<"enter m and n\n";
cin>>m>>n;
cout<<"enter an array\n";
readmatrix(a,m,n);
dispmatrix(a,m,n);
rowsum(a,m,n);
break;
 case 'b':cout<<"enter m and n\n";
 cin>>m>>n;
 cout<<"enter an array\n";
 readmatrix(a,m,n);
 dispmatrix(a,m,n);
 columnsum(a,m,n);
 break;
 case 'c':
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter p and q";
cin>>p>>q;
if((m!=p)&&(n!=q))
cout<<"sum not possible";
else{
cout<<"enter 1st matrix";
readmatrix(A,m,n);
cout<<"enter 2nd matrix";
readmatrix(B,m,n);

summatrix(A,m,n,B,C);
cout<<"1st matrix";
dispmatrix(A,m,n);
cout<<"\n";
cout<<"2nd matrix";
dispmatrix(B,p,q);
cout<<"\n";
cout<<"summatrix";
cout<<"\n";
dispmatrix(C,m,n);
  }
break;


case 'd':
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter p and q";
cin>>p>>q;
if((m!=p)&&(n!=q))
cout<<"sum not possible";
else{
cout<<"enter 1st matrix";
readmatrix(A,m,n);
cout<<"enter 2nd matrix";
readmatrix(B,m,n);

diffmatrix(A,m,n,B,C);
cout<<"1st matrix";
dispmatrix(A,m,n);
cout<<"\n";
cout<<"2nd matrix";
dispmatrix(B,p,q);
cout<<"\n";
cout<<"diffmatrix";
cout<<"\n";
dispmatrix(C,m,n);
  }
break;

case 'e':
cout<<"enter m and n";
cin>>m>>n;
readmatrix(a,m,n);

if(m!=n)
cout<<"sum not possible";
else{
cout<<"\nenter an array";
dispmatrix(a,m,n);
 sum=smd(a,n);
cout<<"sum ="<<sum;  }
break;


case 'f':
cout<<"enter m and n";
cin>>m>>n;
cout<<"\nenter an array";
readmatrix(a,m,n);

if(m!=n)
cout<<"sum not possible";
else{
cout<<"\nenter an array";
dispmatrix(a,m,n);
sum=sod(a,n);
cout<<"sum ="<<sum;  }
break;

case 'g':
cout<<"enter m and n";
cin>>m>>n;
readmatrix(a,m,n);

if(m!=n)
cout<<"sum not possible";
else{
cout<<"\nenter an array";
dispmatrix(a,m,n);
sum=sut(a,n);
cout<<"sum ="<<sum;}
break;


case 'h':
cout<<"enter m and n";
cin>>m>>n;
cout<<"\nenter an array";
readmatrix(a,m,n);

if(m!=n)
cout<<"sum not possible";
else{
dispmatrix(a,m,n);
sum=slt(a,n);
cout<<"sum ="<<sum;}
break;

case 'i':
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter an array";
readmatrix(a,m,n);
dispmatrix(a,m,n);
sumposneg(a,m,n);
break;

case 'j':
 cout<<"enter m and n";
 cin>>m>>n;
 cout<<"enter an array";
 readmatrix(a,m,n);
 dispmatrix(a,m,n);
 transpose(a,B,m,n);
 dispmatrix(B,n,m);
 break;
case 'k': exit(0);
 break;

default:cout<<"enter a valid choice\n";
}



cout<<"\nenter y to repeat n to exit";
cin>>ch1;
if(ch1=='n'||ch1=='N')
exit(0);

}
while(ch1=='y'||ch1=='Y');
   getch();
   }

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();
}

Wednesday, February 9, 2011

a simple program to find the sum of two matrixes...


#include<iostream.h>
#include<conio.h>
#include<process.h>
void readmatrix(int a[10][10],int m,int n)
{int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
void dispmatrix(int a[10][10],int m,int n)
{int i,j;
for(i=0;i<m;i++)
{cout<<"\n";
for(j=0;j<n;j++)
{cout<<a[i][j];
cout<<"\t";
}}}
void summatrix(int a[10][10],int m,int n,int b[10][10],int c[10][10])
{int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
}

void main()
{clrscr();
int A[10][10],B[10][10],C[10][10],m,n,p,q;
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter p and q";
cin>>p>>q;
if((m!=p)&&(n!=q))
{cout<<"sum not possible";
exit(0);
}
cout<<"enter 1st matrix";
readmatrix(A,m,n);
cout<<"enter 2nd matrix";
readmatrix(B,m,n);

summatrix(A,m,n,B,C);
cout<<"1st matrix";
dispmatrix(A,m,n);
cout<<"\n";
cout<<"2nd matrix";
dispmatrix(B,p,q);
cout<<"\n";
cout<<"summatrix";
cout<<"\n";
dispmatrix(C,m,n);
getch();
}

this is a simple c++ program to find the rowsum and coloum sum of a matrix...




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

void sum(int a[10][10],int m,int n)
{int i,j,s;
for(i=0;i<m;i++)
{s=0;
for(j=0;j<n;j++)
s+=a[i][j];
cout<<"row sum of "<<i<<"row ="<<s;
cout<<"\n";}
for(j=0;j<n;j++)
{s=0;
for(i=0;i<m;i++)
s+=a[i][j];
cout<<"column sum of"<<j<<"column ="<<s;
cout<<"\n";}
}


void main()
{clrscr();
int a[10][10],m,n,i,j;
cout<<"enter the no:of elements";
cin>>m>>n;
cout<<"enter the array";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{cout<<a[i][j];
cout<<"\t";}
cout<<"\n";}

sum(a,m,n);
getch();
}