Question

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 :

a) a function to randomly fill the array with the numbers

b) a function to determine if the array is a magic square

Run these functions for 10000 number of times and print the number of times the array is a magic square.

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

input code:

output:

code:

#include <bits/stdc++.h>
using namespace std;
#define M 4
/*generate the numbers from 1 to 16 and fill into array*/
void generate_array(int array[][M])
{
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
array[i][j]=rand()%16+1;
}
}
}

bool Check_magicsquare (int array[][M])
{

int sum = 0, sum2 = 0;
/*find the sum of diagonal */
for (int i = 0; i < M; i++)
{
sum = sum + array[i][i];
}
/*find the sum of second diagonal */
for (int i = 0; i < M; i++)
{
sum2 = sum2 + array[i][M - 1 - i];
}
/*if diagonal sum is not same than return false */
if (sum != sum2)
{
return false;
}

/*find the sum of all rows */
for (int i = 0; i < M; i++)
{

int Rows = 0;
for (int j = 0; j < M; j++)
{
Rows += array[i][j];
}
/*if row sum is not same as diagonal sum than return false */
if (Rows != sum)
{
return false;
}
}

/*find the sum of all cols */
for (int i = 0; i < M; i++)
{

int Cols = 0;
for (int j = 0; j < M; j++)
{
Cols += array[j][i];
}
/*if row sum is not same as diagonal sum than return false*/
if (sum != Cols)
return false;
}
return true;
}

int main ()
{
/*declare array and counter*/
int magicsquare=0;
int array[M][M];
cout<<"Array generating is starting."<<endl;
cout<<"Loading....."<<endl;
cout<<"Loading....."<<endl;
cout<<"Loading....."<<endl;
/*do process for 10000 times*/
for(int i=0;i<10000;i++)
{
/*fill value into array*/
generate_array(array);
  
/*check array is magicsquare or not*/
if(Check_magicsquare(array))
{
magicsquare++;
}
}
/*print Total magicsquare */
cout<<"Total number of magic square is generated is : "<<magicsquare;
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that uses a 4 X 4 array and randomly place each integer...
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
  • 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:...

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

  • 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?

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

  • I need this written in C++. Magic Squares. An n x n matrix that is filled...

    I need this written in C++. Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square,...

  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...

  • C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is...

    C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square, place k+1 into the square...

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

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