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

Write a program that uses a 3x3 array and randomly place each integer from 1 to

ID: 3707935 • Letter: W

Question

Write a program that uses a 3x3 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 dividing the sum by 3. The 3x3 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 thee following functions: a function, to randomly fill the array with the numbers and a function to determine 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 times and see the number of times the array is a magic square.

Explanation / Answer

CODE:

#include <iostream>

#include <cstdlib>

#include <iomanip>

using namespace std;

const int ROWS = 3;

const int COLS = 3;

void fillBoard(int b[][COLS]);

void showBoard(int b[][COLS]);

bool isMagicSquare(int b[][COLS], int rows);

int main()

{

   int board[ROWS][COLS];

long long count = 0;

long long numOfTrials;

cout << "Enter the number of times to test magic square: ";

cin >> numOfTrials;

cout << endl;

for (long long i = 0; i < numOfTrials; i++)

{

fillBoard(board);

if (isMagicSquare(board, ROWS))

{

showBoard(board);

count++;

}

}

cout << "Number of times magic square occurred: " << count << endl;

   return 0;

}

void fillBoard(int b[][COLS])

{

int row;

int col;

int num = 1;

bool placedNum;

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

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

b[i][j] = -1;

for (int i = 0; i < ROWS * COLS; i++)

{

placedNum = false;

while (!placedNum)

{

row = rand() % ROWS;

col = rand() % COLS;

if (b[row][col] == -1)

{

b[row][col] = num;

placedNum = true;

}

}

num++;

}

}

void showBoard(int b[][COLS])

{

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

{

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

cout << setw(4) << b[i][j];

cout << endl;

}

cout << endl;

}

bool isMagicSquare(int b[][COLS], int rows)

{

int magicNum;

int sum = 0;

bool isMagic = true;

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

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

sum = sum + b[i][j];

magicNum = sum / rows;;

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

{

sum = 0;

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

sum = sum + b[i][j];

if (sum != magicNum)

{

isMagic = false;

break;

}

}

if (isMagic)

{

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

{

sum = 0;

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

sum = sum + b[i][j];

if (sum != magicNum)

{

isMagic = false;

break;

}

}

}

if (isMagic)

{

sum = 0;

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

sum = sum + b[i][i];

isMagic = false;

}

if (isMagic)

{

sum = 0;

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

sum = sum + b[i][rows - 1 - i];

isMagic = false;

}

return isMagic;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote