Matrix Allocating [closed]

  c++

Hello Can anyone help me fix my code please? This is what I need to fix according to the professor, Does it look right to you guys or should I ask why he said this? I think my code is right, can anyone give me an explanation of why my code is right? I need advice on how to proceed please.

Here are the original instructions to the assingment: Write a program that fills a 4 x 4 matrix of integers. The matrix must be a static matrix; its size must be fixed at compile-time. Display the contents of the matrix. Query the user for an integer. Pass the matrix and the integer to a bool function that searches for the first occurrence (and only the first occurrence) of the integer in the matrix. If the integer is found, the function returns true and passes back the row and column coordinates of the first occurrence of the integer using appropriate parameters; if not, the function returns false. Main then displays whether the integer was found in the matrix and if so, the coordinates of the first occurrence. The use of break to exit a loop or a function should be avoid and will be penalized. Solutions that do not use a static matrix are unacceptable. Solutions that do not use a bool function as described are unacceptable. Unacceptable solutions will be returned and must be fixed and resubmitted.

Using constexpr to set the values of ROWS and COLS and then using those values to allocate the matrix is causing runtime allocation. I was curious and actually checked and displayed the address of the matrix using your code, and it is clearly being allocated from the heap. These values MUST be fixed at compile time.

#include <iostream>

// I am using only what I need
// from namespace std;

using std::cin;
using std::cout;
using std::endl;

// Size of the matrix at compile-time.
constexpr int ROWS = 4;
constexpr int COLS = 4;

// display the contents of the matrix.
void displayMatrixContents(int matrix[][COLS])
{
cout << "Colt1t2t3t4n";
cout << "------------------------------------n";
for (int rows = 0; rows < ROWS; rows++)
{
cout << "Row: " << rows + 1;
for (int cols = 0; cols < COLS; cols++)
{
cout << "t" << matrix[rows][cols];
}
cout << endl;
}
}

bool searchNumberInMatrix(int matrix[][COLS], int &row, int &col, int number)
{
bool Found = false;

for (int rows = 0; rows < ROWS && !Found; rows++)
{
for (int cols = 0; cols < COLS && !Found; cols++)
{
if (matrix[rows][cols] == number)
{
// Once it is found, set the flag to true
// so the for loop will stop. No reason to
// keep looping.
Found = true;

row = rows + 1;
col = cols + 1;
}
}
}

return Found;
}

int main()
{
static int matrix[ROWS][COLS] = { { 18, 39, 91, 91 },
{ 67, 3, 9, 95},
{ 69, 3, 68, 93},
{ 69, 21, 92, 92} };

displayMatrixContents(matrix);

cout << "nEnter an integer: ";
int number{};
cin >> number;

// Declare these 2 variables to be used if the number is found
int row{};
int col{};

if (searchNumberInMatrix(matrix, row, col, number))
{
cout << "nFound " << number << " at "
<< "Row # " << row << " Col # "
<< col << endl;
}
else
{
cout << "nNumber " << number << " was not found" << endl;
}

return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT