Write a program which will serve as a Magic Square Tester. In particular, your p
ID: 3629868 • Letter: W
Question
Write a program which will serve as a Magic Square Tester. In particular, your program is to1. 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, 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:
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
5. Read all input from a text file and output the results to a text file. Define the input and output
streams globally so that they are visible to the functions that do input/output. 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 your program myself with my data, name and open your input disk file as “Magic.dat” (again, no path name!), which will be the file name that I will create.
6. 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
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 columns sums
void Calc_Diags ( ) ; // Calculates the two diagonal sums
public:
Magic_Square( ) ; // Constructor to initialize ColSums, RowSums, Diag1 and
// Diag2 to zeroes.
void Input_Values ( ); // Inputs the matrix values
void Output_Values ( ); // Displays the 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 displays whether a magic square or not
and if not, which criteria failed.
Explanation / Answer
hi im giving 2 solutions FIRST SOLUTION#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;
void check(int& column, int& row, int rc);
const int MAX = 81;
int main()
{ int array[MAX][MAX], rc, row, column, num(1), rcsqrd, rowcheck(0);
char choice;
cout << "Please enter your magic number(odd number used for dimensions of "
<< "rows and columns:";
cin >> rc;
rcsqrd = rc * rc;
while(!(rc%2) && !(rc >= MAX) && !(rc <= 0))
{ cout << "Please enter an odd number less than or equal to " << MAX << ": ";
cin >> rc;
}
column = (rc - 1) / 2;
row = 0;
for(int i = 0; i <= rcsqrd; i++)
{ if(array[column][row] != 0)
{ row++;
}
array[column][row] = num;
check(column, row, rc);
num++;
}
column = 0;
row = 0;
for(int i = 0; i <= rcsqrd; i++)
{ if(row == rc - 1)
{ cout << setw(3) << array[column][row] << endl;
column = 0;
row = rowcheck + 1;
rowcheck++;
}
else
{ cout << setw(3) << array[column][row];
row++;
}
}
system("pause"); return 0;
}
void check(int& column, int& row, int rc)/*This function checks where the
location of the point is, and whether it is off the board or not. It then puts
them in the correct position.
preconditons: column, row, and rc have been given values.
postconditions: column and row have been corrected. */
{ if(row - 1 < 0 && column + 1 > rc - 1)//case for diagonal
{ row++;
return;
}
if(row - 1 < 0 && column + 1 <= rc - 1)//case for ROW
{
column++;
row = rc - 1;
return;
}
if(column + 1 > rc - 1 && (row - 1 < rc - 1 && row - 1 >= 0))//case for COLUMN { row--;
column = 0;
return;
}
return;
}II SOLUTION USING TEMPLATES
template <typename T>
T** allocate_2d_matrix(int N1, int N2)
{ T** p = new T*[N1];
for (int i=0; i<N2; i++)
p[i] = new T[N2];
return (p);
}
template <typename T>
void destroy_matrix(T** p, int N1, int N2)
{ for (int i=0; i<N2; i++)
delete [] p[i];
delete [] p;
}
template <typename T>
void fill(T** p, int N1, int N2)
{ for (int i=0; i<N1; i++)
for (int j=0; j<N2; j++)
p[i][j] = rand();
}
template <typename T>
T sum_row(T** p, int N1, int N2, int row)
{ assert(row < N1);
return (std::accumulate(p[row], p[row] + N2, 0));
}
template <typename T>
T sum_col(T** p, int N1, int N2, int col)
{ assert(col < N2);
int sum = 0;
for (int i=0; i<N1; i++)
sum += p[i][col];
return (sum);
}
template <typename T>
bool is_magic(T** p, int N1, int N2)
{ int sum_base = sum_row(p, N1, N2, 0);
for (int i=1; i<N1; i++)
{ if (sum_base != sum_row(p, N1, N2, i))
return (false);
}
for (int i=1; i<N2; i++)
{ if (sum_base != sum_col(p, N1, N2, i))
return (false);
}
int sum_diag1 = 0;
int sum_diag2 = 0;
for (int i=0; i<N1; i++)
for (int j=0; j<N2; j++)
{ sum_diag1 += p[i][j];
sum_diag2 += p[i][N2-j-1];
}
return
(sum_base == sum_diag1) &&
(sum_base == sum_diag2);
}
void test_matrix()
{ const int N = 2;
int** p = allocate_2d_matrix<int>(N, N);
fill(p, N, N);
std::cout << is_magic(p, N, N) << std::endl;
destroy_matrix(p, N, N);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.