1

I wrote my code and I'm ready to submit it but the teacher will be testing it on Visual studio 2015. Every time I test it, it gives me an error that this int magicSquare[n][n] is wrong and that n can't be read.

How do i revise this part to make visual studio read this array from n ?

My code:

 #include <iostream>
 using namespace std;
 // This function is to create the requested magic squares
int main()
{

  int n;
  //asking for n
  cout << "Please enter an odd number" << endl;
  cin >> n;

  //checking in case n doesnt follow rules
  if (n < 3 || n % 2 == 0)
  {
     cout << "Invalid Entry, Please re-enter an odd number that is 3 or larger " << endl;
  }
  else
  {
     // A function to generate odd sized magic squares

     int magicSquare[n][n];

     // Setting every slot to 0
     for (int i = 0; i < n; i++)
     {
        for (int j = 0; j < n; j++)
        {
           magicSquare[j][i] = 0;
        }
     }

     // Initializing position to 1
     int j = n / 2;
     int i = n - 1;

     // Setting each value to generate the magic square
     for (int num = 1; num <= n * n; )
     {
        if (j == -1 && i == n)
        {
            i = n - 2;
            j = 0;
        }
        else
        {
            //send to next number
            // moving it to the right side
            if (i == n)
                i = 0;
            //send to next number again
            // moving it to the upper side
            if (j < 0)
                j = n - 1;
        }
        //second condition
        if (magicSquare[j][i])
        {
            i -= 2;
            j++;
            continue;
        }
        else
            //add the number
            magicSquare[j][i] = num++;
        //first condition
        i++; j--;
    }
    //displaying sum of col/row
    cout << "The sum of each row/col: " << n * (n*n + 1) / 2 << endl;
    //Dispplaying magic square
    for (j = 0; j<n; j++)
    {
        for (i = 0; i<n; i++)
            cout << " " << magicSquare[i][j];
        cout << "\n";
    }

 }
   cout << endl;
   //re running program
   return main();
}
Graham
  • 7,431
  • 18
  • 59
  • 84
  • 3
    The reason it doesn't work is because variable-length arrays are not part of the C++ standard: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – Jeremy Friesner Feb 09 '18 at 05:08
  • C++ does not have run-time-sized arrays. Use `std::vector`. – AnT stands with Russia Feb 09 '18 at 07:20
  • More accurately, in C variable-length arrays are part of the core language, while in C++ they're provided by the Standard library (as `std::vector`). – MSalters Feb 09 '18 at 09:36

2 Answers2

0

The standard requires the array length to be a value that is computable at compile time so that the compiler is able to allocate enough space on the stack. In your case, you are trying to set the array length to a value that is unknown at compile time. Yes, i know that it seems obvious that it should be known to the compiler, but this is not the case here. The compiler cannot make any assumptions about the contents of non-constant variables.'n' must be a constant value.

0

In C++, arrays that are declared that way must use an n that is known at compile time. There are various ways to construct a matrix in C++. Perhaps the simplest is to define a vector of vectors.

Change

 int magicSquare[n][n];

to

std::vector<std::vector<int>> magicSquare(n);
for (auto &row : magicSquare) row.resize(n);
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65