Question
In C++ Write a program that will take a 3x3 matrix and test to see if it is a magic square. A 3x3 matrix is
In C++ Write a program that will take a 3x3 matrix and test to see if it is a magic square. A 3x3 matrix is magic if all of the following sums are the same: each row, each column, and both diagonals.
The following example is a magic square because everything adds up to the same number (2+7+6=15, 9+5+1=15, 4+3+8=15, 2+9+4=15, 7+5+3=15, 6+1+8=15, 2+5+8=15, and 4+5+6=15).
2 | 7 | 6 |
9 | 5 | 1 |
4 | 3 | 8 |
Use functions dont put everything in main
Use loops to calculate the sums dont hardcode the calculation. For example, you dont want something like sum = matrix[0][0] + matrix[0][1] + matrix[0][2]. That is hard to update if we change the size of the array
And I have this. And have everything working except the three highlited lines
#include
#include
using namespace std;
string checkMagicSquare(int(&square)[3][3]);
bool checkValidNum(int nos[], int value);
int main()
{
int value, count = 0, flag = 0;
int square[3][3];
int nos[9] = { 0 };
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3;)
{
while (true)
{
cout << "Get value of row " << (row + 1) << " column " << (column + 1) << "::";
cin >> value;
if (value < 1 || value > 9)
{
cout << " Has to be between 1-9 " << endl;
continue;
}
else
{
if (checkValidNum(nos, value))
{
cout << " Error. Number has to be unique " << endl;
continue;
}
else
{
square[row][column] = value;
nos[count] = value;
count++;
column++;
break;
}
}
}
}
}
string res = checkMagicSquare(square);
cout << res << endl; //No Operator "<<" matches these operands
return 0;
}
string checkMagicSquare(int(&square)[3][3])
{
int sumDiagnal[2], sumColumn = 0, sumRow = 0, row[3], coll[3];
for (int row = 0; row < 3; row++)
{
sumRow = 0;
for (int column = 0; column < 3; column++)
{
sumRow += square[row][column];
row[row] = sumRow; //expression must have pointer-to-objective-type
}
}
for (int column = 0; column < 3; column++)
{
sumColumn = 0;
for (int row = 0; row < 3; row++)
{
sumColumn += square[row][column];
column[column] = sumColumn; //expression must have pointer-to-type
}
}
sumDiagnal[0] = 0;
for (int row = 0; row < 3; row++)
{
sumDiagnal[0] += square[row][row];
}
sumDiagnal[1] = 0;
for (int row = 0; row < 3; row++)
{
sumDiagnal[1] += square[row][3 - 1 - row];
}
bool boolean = true;
int sum = row[0];
for (int T = 1; T < 3; T++)
{
boolean = boolean && (sum == row[T]);
}
for (int T = 0; T < 3; T++)
{
boolean = boolean && (sum == coll[T]);
}
for (int T = 0; T < 2; T++)
{
boolean = boolean && (sum == sumDiagnal[T]);
}
if (boolean)
{
return ":: You have a magic square ::";
}
else
{
return ":: You do not have a Magic Square :: ";
}
}
bool checkValidNum(int nos[], int value)
{
for (int T = 0; T < 9; T++)
{
if (nos[T] == value)
return true;
}
return false;
system("pause");
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started