Solutions to Project Section 3


I'm very sorry that I couldn't put up the solutions earlier. The solution to the matrix addition program is given below  :


First Program : 

//To obtain two matrices and two find their sum
#include<iostream.h>
#include<iomanip.h>
int main( )
{
int i , j , r1 , r2 , c1 , c2 , a[20][20] , b[20][20] , c[20][20];
cout<<"Enter the number of rows and columns of first matrix :";
cin>>r1>>c1;
cout<<"Enter the number of rows and columns of second matrix :";
cin>>r2>>c2;

// If the matrix orders are not equal then addition is not possible
if ( (r1! = r2) || (c1!=c2) )
{
cout<<endl<<"Matrix addition is not possible";
return 1;
}
// If orders are equal then get input for two matrices a and b
for (i =1; i<= r1; i ++)
    {
        for (j = 1; j< = c1; j ++)
                {
                    cout<<"Enter the "<< i <<" x "<< j <<" element of first matrix: ";
                    cin>>a[ i ][ j ];
                }
}
for (i = 1; i<= r2 ; i ++)
    {
        for (j =1; j<= c2; j ++)
            {
                cout<<"Enter the "<<i<<"x"<<j<<" element of second matrix: ";
                cin>>b[ i ] [ j ];
            }
   
  }
cout<<endl;
// Displaying matrix a
for (i =1; i< =r1; i++)
    {
           cout<<endl;
                    for (j =1; j<= c1; j++)
                    {
                        cout<<setw(9)<<a[ i ][ j ];
                    }
      }
cout<<"\t"<<"+";
cout<<endl;
// Displaying matrix b
for (i = 1; i<= r1; i ++)
    {
    cout<<endl;
            for (j =1; j<= c1; j++)
            {
                cout<<setw(9)<<b[ i ] [ j ];
            }
     }
cout<<endl;
cout<<"\t \t \t = ";
cout<<endl;
// Calculating and displaying the result which is stored in matrix c
for (i =1; i<= r1; i++)
    {
    cout<<endl;
            for (j =1; j<=c1; j++)
            {
            c[ i ][ j ] = a[ i ][ j ] + b[ i ][ j ];
            cout<<setw(9)<<c[ i ][ j ];
              }
     }
return 0;
}


I think the above program is easy to understand. If you have any troubles then email me at : ssbell2000@yahoo.com

Go to the next section on : Inline Functions
or return back to Contents.


Copyright © 2002 Sethu Subramanian All rights reserved.