Question

IN C++ Write a program that uses a 3x3 array and randomly places each integer from...

IN C++

Write a program that uses a 3x3 array and randomly places 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 said to be a "magic square" if the sum of each row, each column, and each diagonal is equal to the magic number.

Your program must use 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 10000 times and report the number of times the array is a magic square.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ 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;

}

Add a comment
Know the answer?
Add Answer to:
IN C++ Write a program that uses a 3x3 array and randomly places each integer from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that uses a 4 X 4 array and randomly place each integer...

    Write a C++ program that uses a 4 X 4 array and randomly place each integer from 1 to 16 into the 16 squares. The program calculates the magic number by adding all the numbers in the array and then dividing the sum by 4. The 4 X 4 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...

  • C++: Write a program that uses a 3 X 3 array and randomly place each integer...

    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:...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • Write a Python program that generate randomly a magic square of 3 x 3 elements. For...

    Write a Python program that generate randomly a magic square of 3 x 3 elements. For example: 4 3 8 9 5 1 2 7 6      = Matrix1 Matrix 1 above is an example of magic square. The rows total, the columns total, and the diagonal totals are all 15. Your program should randomly generate a 3x3 matrix. Check if it is a magic square. If it is a magic square, then print some information and quit. If the...

  • A magic square is a square of numbers with each row, column, and diagonal of the square adding up to the same sum, called the magic sum

    A magic square is a square of numbers with each row, column, and diagonal of the square adding up to the same sum, called the magic sum. Arrange the numbers,-1,0,1,2,3,4,5,6,and 7 into a magic square. How does the average of these numbers compare with the magic sum?

  • Help pls! and kindly explain how you do this as well. Thaank you Write a program...

    Help pls! and kindly explain how you do this as well. Thaank you Write a program to test whether a square is a 3x3 magic square. A magic square is a grid with 3 rows and 3 columns, like the figure below. A magic square has the following properties: the grid contains only the numbers 1 through 9 the sum of each row, each column, and each diagonal all add up to the same number Notes: I have provided the...

  • Write a C program Design a program that uses an array to store 10 randomly generated...

    Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...

  • 9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that h...

    9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown in the following figure · 15 4 9 2-15 3 5 7-15 81 615 15 15 15 15 Write a program that simulates a magic square using...

  • please use java language please used ArrayList The Lo Shu Magic Square is a grid with...

    please use java language please used ArrayList The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-31. The • The sum of each row, each column, and each diagonal all add up to the same number 20. Lo Shu Magic Square Lo Shu Magic Square has the following properties: • The grid contains the numbers 1 through 9 exactly. This is shown in Figure 7-32. In a program you can simulate a...

  • Write a full C++program to do the following processes on a two dimension array of integers....

    Write a full C++program to do the following processes on a two dimension array of integers. Assume the size is 4x4 . 1- Fill the array with the numbers like this pattern: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 Note: Do not accept the values as input, or hard code it. Find the math to generate these numbers and fill the array with them.    2- Reset the main diagonal elements...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT