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;
}
bool ask(){
cout<<"Would you like to continue? Y/N";
char reply;
cin >> reply;
if( reply == 'Y' || reply == 'y'){
return true;
}
if( reply == 'N' || reply == 'n'){
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);
}
restart = ask( );
}
return 0;
}
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.