I am working on a project using 2D arrays ( I have never really understood the use, I know how they work, just have always had problems implementing) that holds a 4x4 "magic square." Magic squares contain unique numbers whose rows, columns, and major diagonals add up to the magical value (34 in the case of this program). It is to allow the user to input the values and then checks the user's square.

Anyway, I am getting the error "cannot convert 'int (*)[((unsigned int)((int)colNum))]' to 'int (*)[4]' for argument '1' to 'bool checkRows(int (*)[4], int, int)' "

NOTE: This is not the complete program, just a snippet as I am just trying to work one function at a time.

Any help is always helpful.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

bool checkRows ( int theSquare[][4], const int numRows, const int magicValue );
bool checkColumns ( int theSquare[][4], int const numRows, const int magicValue);
bool checkDiagonals (int theSquare[][4], const int numRows, const int magicValue);
bool checkRange ( int theSquare[][4], const int numRows, const int magicValue);

int main() 
{

    int magicValue = 34;
    int rowNum = 0;
    int colNum = 0;
    int numRows = 0;
    int theSquare[rowNum][colNum];
    
    for ( int rowNum = 0; rowNum <= 4; rowNum++ )
    {
        for ( int colNum = 0; rowNum <= 4; colNum++ )
        {
            theSquare[rowNum][colNum] = 0;
        }
    }
    
    cout << "\nThe magic value for your square is " << magicValue << ", which "
         << "means that every row, column and diagonal of your square must add "
         << "up to that number.\n\n";
    
    for ( int rowNum = 0; rowNum < 4; rowNum++ )
    {
        
        cout << "Please enter the 4 values for row " << rowNum << ", "
             << "seperated by spaces: ";
        cin >> theSquare[rowNum][0] >> theSquare[rowNum][1]
            >> theSquare[rowNum][2] >> theSquare[rowNum][3];
            
        theSquare[rowNum][4] = theSquare[rowNum][0] + theSquare[rowNum][1] +
                               theSquare[rowNum][2] + theSquare[rowNum][3];
                               
        numRows = numRows + 1;
            
    }    
    
    checkRows ( theSquare, numRows, magicValue );
     
    return 0;
}

bool checkRows ( int theSquare[rowNum][4], const int numRows, const int magicValue )
{
     
     int counter = 0;
     
     cout << "ROWS: "
     
     for ( int rowNum = 0; rowNum < 4; rowNum++ )
     {
         
         if ( theSquare[rowNum][4] == magicValue )
         {
              counter =+ 1;
         }
         else
         {
              cout << " " << rowNum;
         }

     }
     
     if ( counter == 4 )

}

Not really a problem, but your prototypes don't need [4]. They can be specified as:

bool checkRows ( int theSquare[][], const int numRows, const int magicValue );
bool checkColumns ( int theSquare[][], int const numRows, const int magicValue);
bool checkDiagonals (int theSquare[][], const int numRows, const int magicValue);
bool checkRange ( int theSquare[][], const int numRows, const int magicValue);

In the checkRows() function itself, you can't use rowNum in the parameters. Since it's a 4x4, just say so:

bool checkRows ( int theSquare[4][4], const int numRows, const int magicValue )

That should help.

Suspect it has to do with these two lines:
1) bool checkRows ( int theSquare[][4], const int numRows, const int magicValue );
2) checkRows ( theSquare, numRows, magicValue );

Line one says checkRows is expecting a 2 dimensional array of type int with 4, and exactly 4 columns in it.

When you call it in the second line you pass it theSquare as parameter one. However, theSquare is declared using a non-constant variable of type unsigned int called colNum for the column part as you can see in the following two lines.

3) int colNum = 0;
4) int theSquare[rowNum][colNum];

The compiler has no way to convert a non-constant int, like colNum, into a constant int, like 4. To test my theory, change declaration line 3 above to:

const int MAXCOL = 4;

and use it like this in line 4 above:

int theSquare[][MAXCOL];

However, the first set of [] above should also have a const int inside, too, as I suspect you'll find out soon when you make the above correction and recompile.

In addition, there are a number of other errors I see. For example:

for ( int colNum = 0; rowNum <= 4; colNum++ )

this is a logic error since rowNum above should be colNum and it should be < not <=. The first error won't be evident until you realize that the loop will never stop since the first value for rowNum will be zero and it will never change in the above loop so it will be an endless loop when the program runs, eventhough it compiles okay. Then when you put colNum in in place of rowNum it will crash, if you're lucky, because 4 is not a valid index of max number of columns is theSquare. The value of 3 is the largest valid index for the column variable since arrays are zero indexed, meaning the first valid index is zero, not one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.