### Struggle with matrices

I am struggling to complete this matrices task. I just need the program to display each matrix and stat if they are equal. I would really appreciate any help of where I have went wrong as I am new to this.

#include <iostream>
#include <iomanip>

#define ROWS 4
#define COLS 4

using namespace std;

void displayMatrix(int matrix[ROWS][COLS]);

int main()
{
int matrixA[ROWS][COLS] =
{
{ 1, 0, 19, -3 },
{ 0, 2, -147, 5 },
{ 19, -147, 3, 42 },
{ -3, 5, 42, 4 }
};

int matrixB[ROWS][COLS]= {
{42, 17, -1, 3},
{92, 77, 0, -2},
{4, 4, -2, 3},
{1, 23, 19, 47}
};

// Display matrixA
cout << "matrixA:" << endl;
displayMatrix(matrixA);

//Display matrixB
cout << "matrixB:" << endl;
displayMatrix(matrixB);

int matrix(int matrixA[ROWS][COLS], int matrixB[ROWS][COLS], bool isEqual);

bool isEqual = 1;

if(isEqual == 1)
{
cout << "\nMatrix a is Equal to Matrix b" << isEqual << endl;
}
cout << endl;
return 0;
}

int matrix(int matrixA[ROWS][COLS], int matrixB[ROWS][COLS], bool isEqual){

int columns, rows;

isEqual = 1;
for(rows = 0; rows < ROWS; rows++)
{
for(columns = 0; columns < COLS; columns++)
{
if(matrixA[rows][columns] != matrixB[rows][columns])
{
isEqual = 0;
}
}
}

return isEqual;
}

/******************************************************************************
*
* Name: displayMatrix
* Parameters: matrix - 2D matrix to display
* Returns: nothing
* Globals: none
* Description: This functions displays the contents of the 2D matrix passed
* as a parameter.
*
*****************************************************************************/

void displayMatrix(int matrix[ROWS][COLS])
{
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
cout << setw(8) << matrix[row][col];
}
cout << endl;
}
}
Use code tags.

Don't put the declaration of your function inside main() - put it at the top where you have the other one.

You don't need an argument isEqual since you return this from the function - it can be just a local variable in that function.

You don't write anything if the matrices are NOT equal.

In C++ it's preferable to use constexpr instead of #define. Perhaps:

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````include #include constexpr size_t ROWS {4}; constexpr size_t COLS {4}; void displayMatrix(const int matrix[ROWS][COLS]); bool isEqual(const int matrixA[ROWS][COLS], const int matrixB[ROWS][COLS]); int main() { const int matrixA[ROWS][COLS] { { 1, 0, 19, -3 }, { 0, 2, -147, 5 }, { 19, -147, 3, 42 }, { -3, 5, 42, 4 } }; const int matrixB[ROWS][COLS] { {42, 17, -1, 3}, {92, 77, 0, -2}, {4, 4, -2, 3}, {1, 23, 19, 47} }; std::cout << "matrixA:\n"; displayMatrix(matrixA); std::cout << "matrixB:\n"; displayMatrix(matrixB); std::cout << "\nMatrix a is Equal to Matrix b: " << std::boolalpha << isEqual(matrixA, matrixB) << '\n'; } bool isEqual(const int matrixA[ROWS][COLS], const int matrixB[ROWS][COLS]) { for (size_t rows = 0; rows < ROWS; ++rows) for (size_t columns = 0; columns < COLS; ++columns) if (matrixA[rows][columns] != matrixB[rows][columns]) return false; return true; } void displayMatrix(const int matrix[ROWS][COLS]) { for (size_t row = 0; row < ROWS; ++row) { for (size_t col = 0; col < COLS; ++col) std::cout << std::setw(8) << matrix[row][col]; std::cout << '\n'; } }``````

Topic archived. No new replies allowed.