Magic Square Note: This assignment gives explicit design specifications for the
ID: 3626827 • Letter: M
Question
Magic SquareNote: This assignment gives explicit design specifications for the class that sup-ports magic square calculations. There are cases in program development in which the interface and implementation is pre-estalished and we are given the task to provide code based solely on these specifications.
The notion of a Magic Square is a familiar topic in recreational mathemat-ics. A Magic Square is defined to be a square array of dimension N x N in which the following conditions or criteria are true:
I. Sequence Criteria: the elements of the array are the distinct positive integers 1,2,3,...,N*N in any order. Each of the integers in the sequence is used only once to populate the magic square. (For example, a magic square of dimension 3 will use the following integers: 1,2,3,4,5,6,7,8,9. By definition, there are nine locations in the resulting 3 x 3 array. Each number in the sequence must only appear once in a specific location in the array.)
II. Sum Criteria: the individual sums of each row, each column, and each of the two main diagonals are the same value.
Write a program which will serve as a Magic Square Tester. In particular, your program is to:
1. Read data sets of the following form:
a. N, an integer representing the dimension of the array to be tested (maximum 5)
b. The N*N elements of the array, in row order (first row values, then second row values, etc.)
2. Print the N*N values of the array (matrix) in a two-dimensional format.
3. Print the result of performing the test for both criteria as follows:
a. If both criteria are met, print the message Magic Square! below the matrix values.
b. If either condition is not met (or both are not met), print the message Not a Magic Square! below the matrix and also print the criteria that were not met (Sequence criteria failed. and/or Sum criteria failed.).
4. Test your program on the following data entered into a text file (the first header line is just for informational use only and should not be included in the actual input file):
N Data entered in row-major order
2 1 3 2 4
3 1 6 8 5 7 3 9 2 4
2 1 5 2 3
3 3 3 3 3 3 3 3 3 3
4 1 6 11 5 3 7 8 16 12 13 4 2 15 9 10 14
5 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
1 1
3 8 1 6 3 5 7 4 9 2
3 3 6 7 4 9 2 8 1 6
4 7 6 12 9 10 11 5 8 13 16 2 3 4 1 15 14
3 6 1 8 9 4 2 3 7 5
4 1 15 6 12 8 10 3 13 11 5 16 2 14 4 9 17
6. Read all input from the text file and output the results to a text file. De-fine the input and output stream globally so that they are visible to the functions that do input/output. See pages 583-586 in the course text for information on how to declare file input and file output streams. Do not include a path on the names of both input and output disk files so that the project directory will be accessed.
In order to run and test your program with my data, name and open your input disk file as “Magic.dat” (again, no path name!), and name your output disk file as “MagicOut.dat”.
7. Solve this problem using a Magic Square class defined as follows:
class Magic Square
{
private:
int Size; // Number of rows and columns in the Magic Square array
2
int MagSq[5][5]; // Magic Square matrix int ColSums[5]; // Array of column sums int RowSums[5]; // Array of row sums int Diag1; // Sum of major diagonal int Diag2; // Sum of minor diagonal
bool Sum Criteria; // Was the sum test met?
bool Sequence Criteria; // Was the sequence test met? void Calc RowSums(); // Calculates the row sums
void Calc ColSums(); // Calculates the column sums void Calc Diags(); // Calculates the two diagonal sums
public:
Magic Square(); // Constructor initializes all member variables
void Input Values(); // Reads the input matrix values void Output Values(); // Writes the output matrix values
void Seq Test(); // Determines if the sequence test was met void Sum Test(); // Determines if the sum test was met
void Evaluate(); // Determines and generates message
// whether a magic square or not.
// If not, generates message about which criteria failed
}
Submit both your source .cpp file and your output file. There will be a point reduction if the output data file is not received.
The last one I received was in java. It helped with some some concepts but not everything.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
using namespace std;
ifstream in;
ofstream out;
int n;
class MagicSquare
{private:
int Size; // Number of rows and columns in the Magic Square array
int MagSq[5][5]; // Magic Square matrix int ColSums[5]; // Array of column sums int RowSums[5]; // Array of row sums int Diag1; // Sum of major diagonal int Diag2; // Sum of minor diagonal
bool SumCriteria; // Was the sum test met?
bool SequenceCriteria; // Was the sequence test met? void Calc RowSums(); // Calculates the row sums
void CalcColSums(); // Calculates the column sums void Calc Diags(); // Calculates the two diagonal sums
public:
MagicSquare() // Constructor initializes all member variables
{
Size=n;
SumCriteria=true;
SequenceCriteria=true;
}
void InputValues() // Reads the input matrix values void Output Values(); // Writes the output matrix values
{int i,j;
out<<" The Square ";
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{in>>MagSq[i][j];
out<<MagSq[i][j]<<" ";
}
out<<endl;
}
}
void SeqTest()// Determines if the sequence test was met
{bool num[Size*Size];
for(int i=0;i<Size*Size;i++)
num[i]=true;
for(int i=0;i<Size;i++)
for(int j=0;j<Size;j++)
num[MagSq[i][j]-1]=false;
for(int i=0;i<Size*Size;i++)
if(num[i])
SequenceCriteria= false;
}
void SumTest() // Determines if the sum test was met
{int tot=0,total,i,j;
for(i=0;i<Size;i++)
tot+=MagSq[i][i];
total=tot;
for(i=0;i<Size;i++)
{tot=0;
for(j=0;j<Size;j++)
tot+=MagSq[i][j];
if(tot!=total)
SumCriteria=false;
}
for(j=0;j<Size;j++)
{tot=0;
for(i=0;i<Size;i++)
tot+=MagSq[i][j];
if(tot!=total)
SumCriteria=false;
}
tot=0;
for(i=0;i<Size;i++)
tot+=MagSq[i][Size-i-1];
if(tot!=total)
SumCriteria=false;
}
void Evaluate() // Determines and generates message
// whether a magic square or not.
// If not, generates message about which criteria failed
{
if(SumCriteria&&SequenceCriteria)
out<<"Magic Square ";
else
if(SumCriteria)
out<<"Not Magic Square due to Sequence Criteria ";
else
out<<"Not Magic Square due to Sum Criteria ";
}
};
int main()
{
in.open("Magic.dat");
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
out.open("MagicOut.dat");
in>>n;
while(in)
{
MagicSquare s;
s.InputValues();
s.SeqTest();
s.SumTest();
s.Evaluate();
in>>n;
}
in.close();
out.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.