Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// C++ // Lo Shu Magic Square // // I WANNA OUTPUT TO BE RANDOM // BUT HERE MY OUTPUT IS NOT RANDOM #include #include

// C++

// Lo Shu Magic Square

//

// I WANNA OUTPUT TO BE RANDOM

// BUT HERE MY OUTPUT IS NOT RANDOM

#include

#include

#include

using namespace std;

void getRandomNumbers();

void addNumberToArray(int );

bool isMagicSquare();

void populateGrid();

void winningTest();

const int SIZE = 3;

int numbers [SIZE][SIZE] {{0, 0, 0},{0, 0, 0},{0, 0, 0}};

int main()

{

int counter = 0;

do

{

counter++;

cout << counter << endl;

getRandomNumbers();

if (counter == 10000)

break;

winningTest();

}

while (!isMagicSquare());

populateGrid();

cout << "It took " << counter << " tries." << endl;

return 0;

}

void populateGrid()

{

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

{

for (int c = 0; c < SIZE; c++)

{

cout << numbers [i][c] << "";

}

cout << endl;

}

}

bool isMagicSquare()

{

int temSum = numbers [0][0] + numbers [0][1] + numbers [0][2];

// Compare rows; start fromrow 1 because tempSum already holds the sum for row 0

for (int i = 1; i < SIZE; i++)

{

if (numbers [i][0] + numbers [i][1] + numbers [i][2] != temSum)

return false;

}

// Compare colums

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

{

if (numbers [0][i] + numbers [1][i] + numbers [2][i] != temSum)

return false;

}

// Compare cross values left to right

if (numbers [0][0] + numbers [1][1] + numbers [2][2] != temSum)

return false;

// Compare cross values right to left

if (numbers [0][2] + numbers [1][1] + numbers [2][0] != temSum)

return false;

return true;

}

void getRandomNumbers()

{

int number = 0;

int usedNumbers[] {0, 0, 0, 0, 0, 0, 0, 0, 0,}; // Array that holds numbers

srand((unsigned) time(NULL));

bool wasNumberUsed = true;

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

{

do

{

wasNumberUsed = true;

number = rand() % 9 + 1; // Get random number 1-9

if (usedNumbers[number - 1] == 0)

{

usedNumbers[number - 1] = number;

wasNumberUsed = false;

}

}

while (wasNumberUsed);

addNumberToArray(number);

}

}

void addNumberToArray(int number)

{

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

{

for (int c; c < SIZE; c++)

{

if (numbers[i][c] == 0)

{

numbers[i][c] = number;

return;

}

}

}

}

// For testing, this is the winning configuration

void winningTest()

{

numbers[0][0] = 4;

numbers[0][1] = 9;

numbers[0][2] = 2;

numbers[1][0] = 3;

numbers[1][1] = 5;

numbers[1][2] = 7;

numbers[2][0] = 8;

numbers[2][1] = 1;

numbers[2][2] = 6;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Define portected access modifier in your own words

Answered: 1 week ago

Question

14-13: When are people mostand leastlikely to help?

Answered: 1 week ago

Question

2. Define communication.

Answered: 1 week ago