Adding of Matrices

//Adding of Matrices.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

void main()
{
int A[3][3], B[3][3], C[3][3], r, c;
clrscr();

//Taking Input for A
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
cout<<"Enter the value for A["<<r<<"]["<<c<<"]: ";
cin>>A[r][c];
}
cout<<endl;
}

//Taking Input for B
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
cout<<"Enter the value for B["<<r<<"]["<<c<<"]: ";
cin>>B[r][c];
//Processing the A and B inputs in storing in C
C[r][c]=A[r][c]+B[r][c];
}
cout<<endl;
}

//Printing the A, B and C Matrices
for(r=0;r<3;r++)
{
//Printing of A Matrix
for(c=0;c<3;c++)
{
cout<<setw(5)<<A[r][c];
}
cout<<"\t\t";

//Printing of B Matrix
for(c=0;c<3;c++)
{
cout<<setw(5)<<B[r][c];
}
cout<<"\t\t";

//Printing of C Matrix
for(c=0;c<3;c++)
{
cout<<setw(5)<<C[r][c];
}
cout<<"\n";
}
getch();
}

/*
Output

Enter the value for A[1][0]: 4
Enter the value for A[1][1]: 5
Enter the value for A[1][2]: 6

Enter the value for A[2][0]: 7
Enter the value for A[2][1]: 8
Enter the value for A[2][2]: 9

Enter the value for B[0][0]: 11
Enter the value for B[0][1]: 12
Enter the value for B[0][2]: 13

Enter the value for B[1][0]: 14
Enter the value for B[1][1]: 15
Enter the value for B[1][2]: 16

Enter the value for B[2][0]: 17
Enter the value for B[2][1]: 18
Enter the value for B[2][2]: 19

    1    2    3            11   12   13            12   14   16
    4    5    6            14   15   16            18   20   22
    7    8    9            17   18   19            24   26   28

*/

No comments

Post your comments

Powered by Blogger.