Write a program that uses a 3 3 3 array and randomly place each integer from 1 to 9 into the nine squares. The program calculates the magic number by adding all the numbers in the array and then divid- ing the sum by 3. The 3 3 3 array is a magic square if the sum of each row, each column, and each diagonal is equal to the magic number. Your program must contain at least the following functions: a function to randomly fill the array with the numbers and a function to deter- mine if the array is a magic square. Run these functions for some large number of times, say 1,000, 10,000, or 1,000,000, and see the number of times the array is a magic square.

Respuesta :

Answer:

c++ helper magic-square

#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

// Functions used in the program.

bool uniqueCheck(int arr[][3], int num);

bool magicSquare(int arr[][3]);

int rTotal(int arr[][3], int row);

int cTotal(int arr[][3], int col);

int dTotal(int arr[][3], bool left);

void display(int arr[][3]);

int main()

{

int arr[3][3]; //makes the array 3x3 makes it into the square

int test[3][3] = {2, 7, 6, 9, 5, 1 , 4 , 3 ,8}; //numbers from 1-9 in order of magic square.

while (1)

{

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

arr[i][j] = 0;// nested for loop the i is for rows and the j is for columns

srand((unsigned)time(NULL));// generates random numbers

for (int i = 0; i < 3; i++)

{

for (int j = 0; j < 3; j++)

{

while (1)

{

int num = (rand() % 9) + 1; // Random function will spit out random numbers from 1-9.

if (uniqueCheck(arr, num))

{

arr[i][j] = num;

break;

}

}

}

}

display(arr);

if (magicSquare(arr))//If the magic square array is an actual magic square than this outputs

{

cout << "This is the Magic Square !" << endl;

break;

}

else //if not than it'll keep looping this until the top one is displayed

cout << "This is Not the Magic Square !" << endl;

}

return 0;

}

// check if the random number generated is a unique number

bool uniqueCheck(int arr[][3], int num)

{

for (int k = 0; k < 3; k++)

for (int i = 0; i < 3; i++)

if (arr[k][i] == num)

return false;

return true;

}

bool magicSquare(int arr[][3]) //This will check if the number presented (randomly) correspond with the magic square numbers.

{

int sum = dTotal(arr, true); // Will check the sum of the diagonal.

if (sum != dTotal(arr, false))

return false;

for (int i = 0; i < 3; i++)

{

if (sum != rTotal(arr, i)) // This will check each row and see if its true or false.

return false;

if (sum != cTotal(arr, i)) // This will check each column and see if its true or false.

return false;

}

return true;

}

int rTotal(int arr[][3], int row) // This will calculate the sum of one row at a time.

{

int sum = 0;

for (int i = 0; i < 3; i++)

sum += arr[row][i];

return sum;

}

int cTotal(int arr[][3], int col) // This will calculate the sum of one column at a time.

{

int sum = 0;

for (int i = 0; i < 3; i++)

sum += arr[i][col];

return sum;

}

int dTotal(int arr[][3], bool left) // This will calculate the sum of diagonal. if the left is true, it will calculate from the left to the right diagonal. If false it will calculate from the right to the left diagonal.

{

int sum = 0;

if (left == true)

{

for (int i = 0; i < 3; i++)

sum += arr[i][i];

return sum;

}

for (int i = 0; i < 3; i++)

sum += arr[i][3 - i - 1];

return sum;

}

void display(int arr[][3]) //This will display the array.

{

for (int i = 0; i < 3; i++)

{

for (int j = 0; j < 3; j++)

cout << arr[i][j] << " ";

cout << endl;

}

cout << endl;

}

Of course this is written in C++, a lower level language but if you wanted to convert it to python or something like java just treat this as pseudocode and rewrite it in python etc because it is more user friendly.

Hope this helps!

ACCESS MORE