int main ()
{
enterMatrices();
sumOfMatrices();
displaySumMatrix ();
return 0;
}
void enterMatrices()
{
int matrix1[3][3];
int matrix2[3][3];
int matrix1=0;
int matrix2=0;
int z;
cout << "Please enter the rows and columsnfor your first matrix.";
//for statement enter the value of rows inmatrix 1
for (int i = 0; i < 3; i++)
{
cout << "Please enterthe values of row " << i + 1 << endl;
//for statement to enter thevalue of columns in matrix 1
for (int j = 0; j <3; j++){
cout<<matrix1[i][j];
}}
cout << "Please enter the rows and columsnfor your first matrix.";
for (int i = 0; i < 4; i++)
{
cout << "Please enterthe values of row" << i + 1 << endl;
for (int j = 0; j <4; j++)
{
z=matrix2[i][j];
}}}
void sumOfMatrices()
{
int matrix1Rows, matrix2Columns, matrix1Columns,matrix2Rows, matrix1, matrix2;
int sum=0;
if (matrix1Rows == matrix2Columns &&matrix1Columns == matrix2Rows)
{
for (int i = 0; i <matrix1Rows; i++)
{
for (int j= 0; j < matrix1Columns; i++)
sum[i][j] = matrix1[i][j] +matrix2[i][j];
}
}
else
cout << "Incorrect matrix. Pleasetry again later!";
exit(0);
}
void displaySumMatrix ()
{
int matrix1, matrix2, sumMatrix;
for (int i = 0; i < 3; i++)
{
cout << "| ";
for (int j = 0; j<3; j++)
cout<< sumMatrix[i][j];
cout<< " | " << endl;
}
}
You need to find an indentation style you like and use it consistently. Your code with no formatting is very very difficult to read. https://en.wikipedia.org/wiki/Indent_style
# include<iostream>
# include<conio.h>
usingnamespace std;
void main()
{
int a[3][3];
int b[3][3];
int sum[3][3];
cout<< "Matrix A "<<endl<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<"Enter Value for loaction : ("<< i << "," << j << ") " ; //you can use "i+1" and "j+1" instead of "i" and "j" to understand better for locations.
cin>>a[i][j];
cout<<endl;
}
} // i am copying the same code as above for getiing input in b matrix but if you are aware of functions you can write it in a function and use twice.
cout<< "Matrix B "<<endl<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<"Enter Value for loaction : ("<< i << "," << j << ") " ; //you can use "i+1" and "j+1" instead of "i" and "j" to understand better for locations.
cin>>b[i][j];
cout<<endl;
}
}
for (int i=0;i<3;i++)
{
for( int j=0;j<3;j++ )
{
sum[i][j]=a[i][j]+b[i][j];
}
}
cout<<endl<<endl;
for (int i=0;i<3;i++)
{
for( int j=0;j<3;j++ )
{
cout<<sum[i][j]<< " " ;
}
cout<<endl;
}
getch();
}