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();
}
#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(); }
Pingback: C++ projects for beginners with source code - Codeboks