### HELP ME PLEASE MAGIC SQUARE

need to create a magic square code that takes in any odd order input from the user and creates a magic square of that dimension.
getting error that says no matching function for call to magic and I'm sure there are other bugs I haven't gotten to yet that I can't see yet.

#include <iostream>
using namespace std;

template<typename T>
void magic(T* theArray, int row, int column){
int n = row;
row = 0;
column = n/2;
int value = 1;
theArray[row][column] = 1;
int k=0;
while ( k<(n*n) ){
if( row == 0){
theArray[row][column]=(value + 1);
row = (n - 1);
column++;
k++;
value++;
}
else if( column == (n - 1) ){
theArray[row][column] = (value +1);
row++;
column = 0;
value++;
k++;
}
else if( theArray[row][column] != 0 ){
row++;
value = value - 1;
}
else{
theArray[row][column] = (value + 1);
row--;
column++;
k++;
}
}
row = n;
column = n;
for(int x=0; x < row; x++){
for(int y=0; y < column; y++){
cout<<theArray[x][y]<<" ";
if(y == (column-1) ){
cout<<endl;
}
}
}

}

bool prompt( int input ){
cout<<"Please enter an odd order dimension for the Magic Square: ";
cin >> input;
cout<<endl;
if(!cin){
cout<<"Not an integer!"<<endl;
return false;
}
else if((input%2 == 0) || (input < 2)){
cout<<"Input not greater than 2 or is even."<<endl;
}
return true;
}

cout<<"Would you like to continue? Y/N";
return true;
}
cout<<"Thank you, Goodbye."<<endl;
return false;
}
else{
cout<<"Invalid response, terminating program."<<endl;
cout<<"Thank you, Goodbye."<<endl;
return false;
}
}
int main(){
cout<<"Welcome to the Magic Square Creator!!"<<endl;
bool valid;
int input;
bool restart = true;
while (restart = true){
cout<<"Please enter an odd order dimension for the Magic Square: ";
valid = prompt(input);
if (valid == true){
int r, c;
r = input;
c = input;
int square[r][c];
magic(square, r, c);
}
}
return 0;
}
First, you should use code tag.

`int square[r][c];`

r and c must be constant. If you want to let the user enter the size, then you need to allocate the 2D. Check this first, then see if this solves the problem.
Last edited on
Topic archived. No new replies allowed.