Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS PROGRAM IS TO BE DONE IN BASIC C++ LANGUAGE. AVOID USAGE OF VECTORS, COMPLE

ID: 3917980 • Letter: T

Question

THIS PROGRAM IS TO BE DONE IN BASIC C++ LANGUAGE. AVOID USAGE OF VECTORS, COMPLEX LANGUAGE ETC. PLEASE USE ONLY BASIC C++ LANGUAGE. THANK YOU

What is a Magic Square?

1. It is an n by n array

2. The integer element values range from 1 to n2

3. All the integer elements must be unique

4. The rows, columns and diagonals all have the same sum

SUM = [n(n2 +1)]/2

Program Requirements:

Menu:

The menu should display 4 options:

1. Create a random square

2. Read squares from a file

3. Create a magic square

4. Quit

Each menu option should do the following:

1. Create a random square

Ask the user to input a size (n) between 3 and 10

Create an n by n square with the numbers 1 through n^2 (n squared)

Randomize the position of the numbers using a seeded random number generator

Run all tests to see if the result is a magic square

2. Read squares from a file

Ask the user to input a file name

Read the data one number at a time

A zero (0) indicates the end of the square

Determine the size (n) of the square from the number of values

Create an n by n square using the values in order

Run all tests to see if it is a magic square

Continue by reading the next set of data until the file is empty

3. Create a magic square

Ask the user to input a size (n) between 3 and 10

Create an n by n square with the numbers 1 through n2

Randomly swap the position of two numbers

Run all tests to see if the result is a magic square

Continue to swap until the result is a magic square

4. Quit

Exit the program

At the end of options 1 and 3, the square should be displayed along with a description of what tests it passed/failed.

For option 2, each square should be displayed along with a description of what tests it passed/failed.

If the user gives the menu invalid data, the menu should redisplay. If the user gives a menu option the wrong data, the menu option should restart. Assume the file will be in the proper format (i.e. numbers, one per line).

Tests

Each test should be done by a separate function, and should return true/false (true if passed). Each test should take as parameters the array, and the size of the square. The tests are as follows:

elementTest – do the values in the square range from 1 to n^2(n-squared) (without duplicates)?

sumTest – do the sums of the rows, the sums of the columns, and the sums of the diagonals all equate to the magic sum? (test each separately with a different function). Display

Each square should be displayed as a grid of numbers. The columns should align perfectly. Leading zeros are not necessary but leading spaces are. The results should be after the square, as should the magic sum.

EXAMPLE: 11 24 7 20 3

4 12 25 8 16

17 5 13 21 9

10 18 1 14 22

23 6 19 2 15

Column sums match!

Row sums match!

Diagonal sums match!

Element values correct!

Magic sum is 65!

Explanation / Answer

here is your program : ----------->>>>>>>>

#include<iostream>
#include<fstream>
#include<ctime>
#include<cmath>
#include<cstdlib>

using namespace std;

void dispaly(int **square,int n){
cout<<" Sqaure : ";
for(int i = 0;i<n;i++){
  for(int j = 0;j<n;j++){
   printf("%2d ",square[i][j]);
  }
  cout<<endl;
}
}

bool check_magic_square(int **square,int n,bool track){
int sum = (n*(n*n + 1))/2;
int tot = 0;
bool magic = true;
int st = 0;
//for checking Rows sum
for(int i = 0;i<n;i++){
  tot = 0;
  for(int j = 0;j<n;j++){
   tot += square[i][j];
  }
  if(tot != sum){
   if(track)
    cout<<" Row "<<(i+1)<<" Does not Match the sum !!! ";
   magic = false;
   st = 1;
  }
}
if(st == 0 && track){
  cout<<" Row Sum Match !!! ";
}
st = 0;
//for checking Columns sum
for(int i = 0;i<n;i++){
  tot = 0;
  for(int j = 0;j<n;j++){
   tot += square[j][i];
  }
  if(tot != sum){
   if(track)
    cout<<" Column "<<(i+1)<<" Does not Match the sum !!! ";
   magic = false;
   st = 1;
  }
}
if(st == 0 && track){
  cout<<" Column Sum Match !!! ";
}
st = 0;
//for checkin diagonal elements sum
tot = 0;
//first diagonal
for(int i = 0;i<n;i++){
  tot += square[i][i];
}
if(tot != sum){
  if(track)
  cout<<" First diagonal does match the sum ";
  magic = false;
  st = 1;
}
tot = 0;
//second diagonal
for(int i = 0;i<n;i++){
  tot += square[i][n-i-1];
}
if(tot != sum){
  if(track)
  cout<<" Second diagonal does not match the sum ";
  magic = false;
  st = 1;
}
if(st == 0 && track){
  cout<<" Diagonal Sum match !!! ";
}
bool elem[n*n];
//initializing elem array
for(int i = 0;i<n*n;i++){
  elem[i] = false;
}
//checking if all elements is present
for(int i = 0;i<n;i++){
  for(int j = 0;j<n;j++){
   elem[square[i][j] - 1] = true;
  }
}
for(int i = 0;i<n*n;i++){
  if(!elem[i]){
   st = 1;
   magic = false;
   if(track)
   cout<<" Element Value Not Correct !!! ";
   break;
  }
}
if(st == 0 && track){
  cout<<" Element Value Correct !!! ";
}

if(magic && track){
  cout<<" Magic Sum = "<<sum;
}
return magic;
}

void create_random_square(){
int n;
while(true){
  cout<<" Enter the size of N between(3-10) : ";
  cin>>n;
  if(n >= 3 && n <= 10){
   break;
  }
  cout<<" Wrong size of N ";
}
int **square = new int*[n];
for(int i = 0;i<n;i++){
  square[i] = new int[n];
}
//initializing new square all to -1
for(int i = 0;i<n;i++){
  for(int j = 0;j<n;j++){
   square[i][j] = -1;
  }
}
srand(time(0));
int pos1,pos2;
//assigning value to new Square at random place
for(int i = 1;i<=n*n;i++){
  while(true){
   pos1 = rand()%n;
   pos2 = rand()%n;
   if(square[pos1][pos2] == -1){
    square[pos1][pos2] = i;
    break;
   }
  }
}
dispaly(square,n);
bool magic = check_magic_square(square,n,false);
if(magic){
  cout<<" Is a Magic Square !!! ";
}else{
  cout<<" Not a Magic Square !!! ";
}
}


void read_square_file(){
string file;
cout<<" Enter a file name : ";
cin>>file;
ifstream ifs;
ifs.open(file.c_str());
if(!ifs.is_open()){
  cout<<" File Does not open !!! ";
  return;
}
//declaring this for storing the value from the file 100 is because max N value = 10;
int maxSquare[100];
int val = -1;
int n = 0;
int prev = 2;
int **square = new int*[2];
for(int i = 0;i<prev;i++){
  square[i] = new int[prev];
}
while(!ifs.eof()){
  n = 0;
  while(val != 0){
   ifs>>val;
   if(ifs.eof()){
    break;
   }
   maxSquare[n] = val;
   n++;
  }
  if(ifs.eof()){
   cout<<" File End !!! ";
   break;
  }
  //deleteing square spaces
  for(int i = 0;i<prev;i++){
   delete[] square[i];
  }
  delete[] square;
  n = (int)sqrt(n);
  if(n == 0){
   break;
  }
  square = new int*[n];
  for(int i = 0;i<n;i++){
   square[i] = new int[n];
  }
  //initializing square with val read from file
  int pos = 0;
  for(int i = 0;i<n;i++){
   for(int j = 0;j<n;j++){
    square[i][j] = maxSquare[pos++];
   }
  }
  dispaly(square,n);
  bool magic = check_magic_square(square,n,true);
  if(magic){
   cout<<" Is a Magic Square !!! ";
  }
}
ifs.close();
}

void generate_magic_square(){
int n;
while(true){
  cout<<" Enter the size of N between(3-10) : ";
  cin>>n;
  if(n >= 3 && n <= 10){
   break;
  }
  cout<<" Wrong size of N ";
}
int **square = new int*[n];
for(int i = 0;i<n;i++){
  square[i] = new int[n];
}
//initializing new square all to -1
for(int i = 0;i<n;i++){
  for(int j = 0;j<n;j++){
   square[i][j] = -1;
  }
}
srand(time(0));
int pos1,pos2;
//assigning value to new Square at random place
for(int i = 1;i<=n*n;i++){
  while(true){
   pos1 = rand()%n;
   pos2 = rand()%n;
   if(square[pos1][pos2] == -1){
    square[pos1][pos2] = i;
    break;
   }
  }
}
int pos3,pos4;
int temp;
while(!check_magic_square(square,n,false)){
  while(true){
   pos1 = rand()%n;
   pos2 = rand()%n;
   pos3 = rand()%n;
   pos4 = rand()%n;
   if(pos1 != pos3 && pos2 != pos4){
    break;
   }
  }
  temp = square[pos1][pos2];
  square[pos1][pos2] = square[pos3][pos4];
  square[pos3][pos4] = temp;
}
dispaly(square,n);
if(check_magic_square(square,n,true)){
  cout<<" Is a Magic Square !!! ";
}
}

void menu(){
cout<<" 1. Create a random square 2. Read squares from a file 3. Create a magic square 4. Quit";
cout<<" Choose : ";
}


int main(){
char choice = '0';
while(choice != '4'){
  menu();
  cin>>choice;
  cin.clear();
  switch(choice){
   case '1':create_random_square();break;
   case '2':read_square_file();break;
   case '3':generate_magic_square();break;
   case '4':break;
   default:
    cout<<" Wrong choice !!! ";
  }
  cin.clear();
}
return 0;
}