C++: Write a program that uses a 3 X 3 array and randomly place each integer fro
ID: 641011 • Letter: C
Question
C++: Write a program that uses a 3 X 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 dividing the sum by 3. The 3 X 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 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
#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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.