I am having a problem adding two matrices in c++ here are theinstructions... If
ID: 3618709 • Letter: I
Question
I am having a problem adding two matrices in c++ here are theinstructions...If A and B are two m x n matrices, their sum is defined as follows:If Aij and Bij are the entries int ith row and jth column of A andB, respectively, then Aij + Bij is the entry in the ith row and jthcolumn of their sum, which will also be an m x n matrix. Write a program to read two m x n matrices, display them, andcalculate and display their sum...
The errors I am getting are as follows...
subscript requires array or pointer type (this error comes up 6times for me)
Here is my code...
#include <iostream>
using namespace std;
//Display functions
void enterMatrices();
void sumOfMatrices();
void displaySumMatrix();
int main ()
{
//function to enter two matrices
enterMatrices();
//function to calculate sum of matrices
sumOfMatrices();
//function to display the sum matrix
displaySumMatrix ();
//return a value
return 0;
}
//enterMatrices function
void enterMatrices()
{
//Declare vairables
int matrix1Rows, matrix1Columns, matrix2Rows,matrix2Columns, matrix1, matrix2;
//Have user enter in first matrix
cout << "Please enter the rows and columsnfor your first matrix.";
cout << "Enter the rows of the firstmatrix: ";
cin >> matrix1Rows;
cout << "Enter the columns of the firstmatrix: ";
cin >> matrix1Columns;
//for statement enter the value of rows inmatrix 1
for (int i = 0; i < matrix1Rows; 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 <matrix1Columns; j++)
{
cin>> matrix1[i][j];
}
}
//Have user enter in second matrix
cout << "Please enter the rows and columsnfor your first matrix.";
cout << "Enter the rows of the secondmatrix: ";
cin >> matrix2Rows;
cout << "Enter the columns of the secondmatrix: ";
cin >> matrix2Columns;
//for statement to enter value of rows in matrix2
for (int i = 0; i < matrix2Rows; i++)
{
cout << "Please enterthe values of row" << i + 1 << endl;
//for statement to entervalue of rows in matrix 2
for (int j = 0; j <matrix2Columns; j++)
{
cin>> matrix2[i][j];
}
}
}
//Function to compute the sum of the matrices
void sumOfMatrices()
{
//declare variables
int matrix1Rows, matrix2Columns, matrix1Columns,matrix2Rows, sumMatrix, matrix1, matrix2;
//Determine if correct matrices to ass
if (matrix1Rows == matrix2Columns &&matrix1Columns == matrix2Rows)
{
for (int i = 0; i <matrix1Rows; i++)
{
for (int j= 0; j < matrix1Columns; i++)
sumMatrix[i][j] = matrix1[i][j] +matrix2[i][j];
}
}
else
cout << "Incorrect matrix. Pleasetry again later!";
//Exit the program if incorrect
exit(0);
}
//function to display the sum of the matrix
void displaySumMatrix ()
{
//Declare variables
int matrix1Rows, matrix2Columns, sumMatrix;
for (int i = 0; i < matrix1Rows; i++)
{
cout << "| ";
for (int j = 0; j <matrix2Columns; j++)
cout<< setw(5) << sumMatrix[i][j];
cout<< " | " << endl;
}
}
Explanation / Answer
please rate - thanks this is functions, reuse them #include #include using namespace std; //Display functions void enterMatrices(int[][20],int,int); void sumOfMatrices(int[][20],int[][20],int[][20],int,int); void displaySumMatrix(int[][20],int,int); int main () {//Declare vairables int m,n,mat1[20][20],mat2[20][20],mat3[20][20]; coutm; coutn; //function to enter two matrices coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.