Question: a) Write a C++ program that uses a 3 3 array and randomly places each integer from 1 to 9 into one of the nine
a) Write a C++ program that uses a 3 3 array and randomly places each integer from 1 to 9 into one of the nine squares. Then, the program calculates the magic number by adding all the numbers in the array and then dividing the sum by 3. Finally, the program outputs the number of times a magic square occurred. Your program must contain the following functions: Function fillBoard that randomly fills the array with numbers from 1 to 9 placed randomly; Function isMagic that determines if the array is a magic square; Function showBoard that prints out the array that is found to be a magic square.
b) Run your program for a large number of times such as 1000, 10000, or 1000000, and observe the number of occurrences where the array is a magic square.
Whenever I run this program, I get an answer for the 1 million trials which is way too high I believe. Can anyone double check this for me?
#include
const int rows = 3; const int columns = 3; int table [rows][columns]; void init (int table[][columns]); bool isMagicNum (int table[][columns]); bool check_gen_numbers(int table[][3], int num);
int main(){ srand(time(0)); int countOne; int countTwo; int countThree; for (int i = 0; i < 1000; i++ ){ init(table); if (isMagicNum(table) == true){ countOne++; } } cout << "The number of magic squares in 1,000 tries is: " << countOne << endl; for (int i = 0; i < 100000; i++){ init(table); if (isMagicNum(table) == true){ countTwo++; } } cout << "The number of magic squares in 100,000 tries is: " << countTwo << endl; for (int i = 0; i < 1000000; i++){ init(table); if (isMagicNum(table) == true){ countThree++; } } cout << "The number of magic squares in 1,000,000 tries is: " << countThree << endl; return 0; }
void init(int table[][columns]){ int x; int tempArray[9] = {0,0,0,0,0,0,0,0,0}; for (int i = 0; i < rows; i++){ for (int j = 0; j < columns; j++){ x = rand() % 9; while(tempArray[x] != 0) x = rand() % 9; if (check_gen_numbers(table, x) == true) table[i][j] = x + 1; tempArray[x] = 1; } } } bool check_gen_numbers(int table[][3], int num){ for (int k = 0; k < 3; k++) for (int i = 0; i < 3; i++) if (table[k][i] == num){ return false; } return true; }
bool isMagicNum(int table[][columns]){ int sum; int magic = 0; for (int i = 0; i < rows; i++){ for (int j = 0; j < columns; j++){ sum += table[i][j]; } } magic = sum / 3; for (int i = 0; i < rows; i++){ sum = 0; for (int j = 0; j < columns; j++){ sum += table[i][j]; } if (sum != magic) return false; } for (int i = 0; i < columns; i++){ sum = 0; for (int j = 0; j < rows; j++){ sum += table[i][j]; } if (sum != magic) return false; } sum = table[0][0] + table[1][1] + table[2][2]; if (sum != magic) return false; sum = table[0][2] + table[1][1] + table[2][0]; if (sum != magic) return false; return true;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
