Write a program to add two matrices by using multi-dimensional arrays.
Program to add two matrices by using multi-dimensional arrays
Object: Write a program to add two matrices by using multi-dimensional arrays. C++ projects for beginners with source code
Code:
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int sum[2][3],i,j;
int mat1[2][3]={{2,4,5},{4,5,6}};
int mat2[2][3]={{6,3,1},{3,2,6}};
cout<<“1st Matrix: \n\n”;
for(i=0;i<2;i++)
{
cout<<“[ “;
for(j=0;j<3;j++)
{
cout<<mat1[i][j]<<” “;
}
cout<<“]”<<endl;
}
cout<<“\n2nd Matrix: \n\n”;
for(i=0;i<2;i++)
{
cout<<“[ “;
for(j=0;j<3;j++)
{
cout<<mat2[i][j]<<” “;
}
cout<<“]”<<endl;
}
cout<<“\nSum of Two Matrix: \n\n”;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
sum[i][j]=mat1[i][j]+mat2[i][j];
}
}
for(i=0;i<2;i++)
{
cout<<“[ “;
for(j=0;j<3;j++)
{
cout<<sum[i][j]<<” “;
}
cout<<“]”<<endl;
}
getch();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include<iostream> #include<conio.h> using namespace std; void main() { int sum[2][3],i,j; int mat1[2][3]={{2,4,5},{4,5,6}}; int mat2[2][3]={{6,3,1},{3,2,6}}; cout<<"1st Matrix: \n\n"; for(i=0;i<2;i++) { cout<<"[ "; for(j=0;j<3;j++) { cout<<mat1[i][j]<<" "; } cout<<"]"<<endl; } cout<<"\n2nd Matrix: \n\n"; for(i=0;i<2;i++) { cout<<"[ "; for(j=0;j<3;j++) { cout<<mat2[i][j]<<" "; } cout<<"]"<<endl; } cout<<"\nSum of Two Matrix: \n\n"; for(i=0;i<2;i++) { for(j=0;j<3;j++) { sum[i][j]=mat1[i][j]+mat2[i][j]; } } for(i=0;i<2;i++) { cout<<"[ "; for(j=0;j<3;j++) { cout<<sum[i][j]<<" "; } cout<<"]"<<endl; } getch(); } |
1 Response
[…] Write a program to add two matrices by using multi-dimensional arrays. […]