Reputation: 3
I'm creating a code to allow a user to create a size of a 2D array and then enter numbers to fill each array space and then have it check if it is a Magic Square for a school project, but I keep receiving the two above errors.
Here is my entire code:
#include <iostream>
bool isMagicSquare(int row, int col, int magicSquare);
using namespace std;
int main()
{
int row, col;
int input = 0;
cout << "Enter the number of rows: ";
cin >> row;
cout << endl;
cout << "Enter the number of columns: ";
cin >> col;
cout << endl;
int magicSquare[row][col];
if (col != row)
{
cout << "Sorry, this cannot be a magic square." << endl;
cout << "Enter the number of rows: ";
cin >> row;
cout << endl;
cout << "Enter the number of columns: ";
cin >> col;
cout << endl;
}
else if ( col == row )
{
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < col; j++)
{
cout << "Enter a number for row " << i+1 << " column " << j+1 << ": ";
cin >> input;
magicSquare[i][j] = input;
}
}
}
isMagicSquare(row, col, magicSquare[row][col]);
{
if (isMagicSquare(row, col, magicSquare[row][col]) == true)
{
cout << "This is a magic square." << endl;
}
if (isMagicSquare(row, col, magicSquare[row][col]) == false)
{
cout << "This is not a magic square." << endl;
}
}
return 0;
}
bool isMagicSquare(int row, int col, int magicSquare[row][col])
{
int rowTotal, colTotal, diagA, diagB;
rowTotal = 0;
colTotal = 0;
diagA = 0;
diagB = 0;
for ( int r = 0; r < row; r++)
{
rowTotal = 0;
for ( int c = 0; c < col; c++)
{
rowTotal += magicSquare[r][c];
}
}
for ( int c = 0; c < col; c++)
{
colTotal = 0;
for ( int r = 0; r < row; r++)
{
colTotal += magicSquare[ r ][ c ];
}
}
for ( int r = 0; r < row; r++)
{
diagA += magicSquare[r][r];
}
for ( int i = 0; i < row; i++)
{
diagB += magicSquare[i][i];
}
if (rowTotal != colTotal)
{
return false;
}
else if (diagA != rowTotal)
{
return false;
}
else if (diagB != rowTotal)
{
return false;
}
return true;
}
and here is the error message in its entirety:
Ld /Users/macintosh/Library/Developer/Xcode/DerivedData/project_5_new-fwiwizttrvaiwxesfsfyyahdetpv/Build/Products/Debug/project\ 5\ new normal x86_64 cd "/Users/macintosh/Desktop/project 5 new" export MACOSX_DEPLOYMENT_TARGET=10.10 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/macintosh/Library/Developer/Xcode/DerivedData/project_5_new-fwiwizttrvaiwxesfsfyyahdetpv/Build/Products/Debug -F/Users/macintosh/Library/Developer/Xcode/DerivedData/project_5_new-fwiwizttrvaiwxesfsfyyahdetpv/Build/Products/Debug -filelist /Users/macintosh/Library/Developer/Xcode/DerivedData/project_5_new-fwiwizttrvaiwxesfsfyyahdetpv/Build/Intermediates/project\ 5\ new.build/Debug/project\ 5\ new.build/Objects-normal/x86_64/project\ 5\ new.LinkFileList -mmacosx-version-min=10.10 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/macintosh/Library/Developer/Xcode/DerivedData/project_5_new-fwiwizttrvaiwxesfsfyyahdetpv/Build/Intermediates/project\ 5\ new.build/Debug/project\ 5\ new.build/Objects-normal/x86_64/project\ 5\ new_dependency_info.dat -o /Users/macintosh/Library/Developer/Xcode/DerivedData/project_5_new-fwiwizttrvaiwxesfsfyyahdetpv/Build/Products/Debug/project\ 5\ new
Undefined symbols for architecture x86_64: "isMagicSquare(int, int, int)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
I've tried many things to try and resolve the issues, but have found no solution. Please help!
Upvotes: 0
Views: 658
Reputation: 1272
The problem with your code is that you declared the ismagicsquare function with signature
bool isMagicSquare(int row, int col, int magicSquare);
but you defined the function with a different signature
bool isMagicSquare(int row, int col, int[][] magicSquare);
which leaves the first declaration with no definition.
Also the simple way to implement this is
#include <iostream>
using namespace std;
bool isMagicSquare(int row, int col, int** magicSquare)
{
int rowTotal, colTotal, diagA, diagB;
rowTotal = 0;
colTotal = 0;
diagA = 0;
diagB = 0;
for ( int r = 0; r < row; r++)
{
rowTotal = 0;
for ( int c = 0; c < col; c++)
{
rowTotal += magicSquare[r][c];
}
}
for ( int c = 0; c < col; c++)
{
colTotal = 0;
for ( int r = 0; r < row; r++)
{
colTotal += magicSquare[ r ][ c ];
}
}
for ( int r = 0; r < row; r++)
{
diagA += magicSquare[r][r];
}
for ( int i = 0; i < row; i++)
{
diagB += magicSquare[i][i];
}
if (rowTotal != colTotal)
{
return false;
}
else if (diagA != rowTotal)
{
return false;
}
else if (diagB != rowTotal)
{
return false;
}
return true;
}
int main()
{
int row, col;
int input = 0;
cout << "Enter the number of rows: ";
cin >> row;
cout << endl;
cout << "Enter the number of columns: ";
cin >> col;
cout << endl;
int** magicSquare;
if (col != row)
{
cout << "Sorry, this cannot be a magic square." << endl;
cout << "Enter the number of rows: ";
cin >> row;
cout << endl;
cout << "Enter the number of columns: ";
cin >> col;
cout << endl;
}
else if ( col == row )
{
magicSquare=new int *[row];
for ( int i = 0; i < row; i++)
{
magicSquare[i]=new int[col];
for ( int j = 0; j < col; j++)
{
cout << "Enter a number for row " << i+1 << " column " << j+1 << ": ";
cin >> input;
magicSquare[i][j] = input;
}
}
}
if (isMagicSquare(row, col, magicSquare) == true)
{
cout << "This is a magic square." << endl;
}else
{
cout << "This is not a magic square." << endl;
}
return 0;
}
Upvotes: 1