a) Write a C++ program that uses a 3 3 array and randomly places each integer fr
ID: 3606717 • Letter: A
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 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.
716 | 15 |511| 15 413|8| 15 15 15 15 15 15Explanation / Answer
Solution:
code:
#include <iostream>
#include <time.h>
#include <fstream>
#define N 3
using namespace std;
//Function that checks for row totals
int checkRows(int **matrix, int total)
{
int i, j, sum = 0, cnt = 0;
//Iterating over rows
for(i=0; i<N; i++)
{
sum = 0;
//Iterating over columns
for(j=0; j<N; j++)
{
//Finding row sum
sum += matrix[i][j];
}
//If row sum is equal to total, increment counter
if(sum == total)
cnt++;
}
//If all rows produces actual total, return 1 (Pass)
if(cnt == N)
return 1;
else
//Fail
return 0;
}
//Function that checks for column totals
int checkColumns(int **matrix, int total)
{
int i, j, sum = 0, cnt = 0;
//Iterating over rows
for(i=0; i<N; i++)
{
sum = 0;
//Iterating over columns
for(j=0; j<N; j++)
{
//Finding column sum
sum += matrix[j][i];
}
//If row sum is equal to total, increment counter
if(sum == total)
cnt++;
}
//If all columns produces actual total, return 1 (Pass)
if(cnt == N)
return 1;
else
//Fail
return 0;
}
//Function that checks for primary diagonal total
int checkprimaryDiagonal1(int **matrix, int total)
{
int i, j, sum = 0;
//Iterating over rows
for(i=0; i<N; i++)
{
//Iterating over columns
for(j=0; j<N; j++)
{
//Handling primary diagonal elements
if(i==j)
//Finding column sum
sum += matrix[i][j];
}
}
//If sum is equal to total
if(sum == total)
//Pass
return 1;
else
//Fail
return 0;
}
//Function that checks for primary diagonal2 total
int checkprimaryDiagonal2(int **matrix, int total)
{
int i, j, sum = 0;
//Iterating over rows
for(i=0,j=N-1; i<N && j>-1; i++, j--)
{
//Finding column sum
sum += matrix[i][j];
}
//If sum is equal to total
if(sum == total)
//Pass
return 1;
else
//Fail
return 0;
}
//Function that gets magic number from given matrix
int getMagicNumber(int **matrix)
{
int i, j, sum=0;
//Iterating over rows
for(i=0; i<N; i++)
{
//Iterating over columns
for(j=0; j<N; j++)
{
//Finding sum
sum += matrix[i][j];
}
}
//Return magic number
return (sum/N);
}
//Filling array with random values
void fillArray(int **matrix)
{
int i, j;
//Iterating over rows
for(i=0; i<N; i++)
{
//Iterating over columns
for(j=0; j<N; j++)
{
//Generating a random number
matrix[i][j] = rand()%9 + 1;
}
}
}
//Displaying array
void showBoard(int **matrix)
{
int i, j;
//Iterating over rows
for(i=0; i<N; i++)
{
//Iterating over columns
for(j=0; j<N; j++)
{
//Displaying matrix element
cout << matrix[i][j] << " ";
}
cout << " ";
}
}
//Checking for magic
bool isMagic(int **square, int magic)
{
//Checking for magic
if( (checkRows(square, magic) == 1) && (checkColumns(square, magic) == 1) && (checkprimaryDiagonal1(square, magic) == 1) && (checkprimaryDiagonal2(square, magic) == 1) )
{
return true;
}
else
{
return false;
}
}
//Main function
int main()
{
int i, cnt=0, magic;
//Initializing random number
srand(time(NULL));
//Creating dynamic array of size N
int** square = new int*[N];
//Allocating memory
for(i = 0; i < N; ++i)
{
square[i] = new int[N];
}
//Iterating for 1000 times
for(i=1; i<=1000; i++)
{
//Filling matrix
fillArray(square);
//Getting magic number
magic = getMagicNumber(square);
//Checking for magic square
if(isMagic(square, magic))
{
cout << " Following Matrix is a magic square: " ;
//Displaying board
showBoard(square);
//Incrementing count
cnt += 1;
}
}
if(cnt == 0)
{
cout << " No Magic Squares generated.... ";
}
cout << " ";
return 0;
}
I hope this helps, if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.