The C program should have at least the following functions s
The C++ program should have at least the following functions:
showArray. This function accepts a two-dimensional int array as an argument and displays its contents.
checkRange. This function accepts a two-dimensional int array as an
argument and returns true if the values are within the specified range
(1 - 9). Otherwise, it returns false.
checkRowSum. This function accepts a two-dimensional int array as an
argument, and returns true if the sum of the values in each of the array\'s rows are equal. Otherwise, it returns false.
checkColSum. This function accepts a two-dimensional int array as an
argument, and returns true if the sum of the values in each of the array\'s columns are equal. Otherwise, it returns false.
checkDiagSum. This function accepts a two-dimensional int array as an argument, and returns true if the sum of the values in each of the array\'s diagonals are equal. Otherwise, it returns false.
isMagicSquare. This function accepts a two-dimensional int array as an argument, tests to determine if it is a Lo Shu Magic Square and displays the result.
Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in Figure Figure 4 9 2 3 8 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 in Figure Figure 15 4 9 2 15 3 15 8 15 15 15 15 15 In a program you can simulate a magic square using a two-dimensional array. Write a function that accepts a two-dimensional array as an argument, and determines whether the array is a Lo Shu Magic Square. Test the function in a programSolution
#include<iostream>
using namespace std;
//Shows the matrix
void showArray(int arr[3][3])
{
int r, c;
cout<<\"\ Array Contents: \ \";
for(r = 0; r < 3; r++)
{
for(c = 0; c < 3; c++)
{
cout<<arr[r][c]<<\" \";
}
cout<<\"\ \";
}
}
//Checks the range
bool checkRange(int arr[3][3])
{
int r, c, n = 1;
for(r = 0; r < 3; r++)
{
for(c = 0; c < 3; c++)
{
if(arr[r][c] < 1 || arr[r][c] > 9)
n = 0;
}
}
if(n == 0)
return false;
else
return true;
}
//Calculates the each row sum and checks for equality
bool checkRowSum(int arr[3][3])
{
int t[3] = {0,0,0};
int r, c;
for(r = 0; r < 3; r++)
{
for(c = 0; c < 3; c++)
{
//Calculates each row sum
t[r] = t[r] + arr[r][c];
}
}
//Checks the equality of each row
if(t[0] == t[1] && t[1] == t[2] && t[0] == t[2])
return true;
else
return false;
}
//Calculates the each Column sum and checks for equality
bool checkColSum(int arr[3][3])
{
int t[3] = {0,0,0};
int r, c;
for(r = 0; r < 3; r++)
{
for(c = 0; c < 3; c++)
{
//Calculates each column sum
t[r] = t[r] + arr[c][r];
}
}
//Checks the equality of each column
if(t[0] == t[1] && t[1] == t[2] && t[0] == t[2])
return true;
else
return false;
}
//Calculates left and right diagonal sum and checks equality
bool checkDiagSum(int arr[3][3])
{
int ld = 0, rd = 0;
int r, c;
for(r = 0; r < 3; r++)
{
for(c = 0; c < 3; c++)
{
//Calculates left diagonal sum
if(r == c)
ld = ld + arr[r][c];
//Calculates right diagonal sum
if((r + c) == 2)
rd = rd + arr[r][c];
}
}
//Checks for the equality
if(ld == rd)
return true;
else
return false;
}
//Checks for the Magic Square matrix
void isMagicSquare(int arr[3][3])
{
if(checkRange(arr) && checkRowSum(arr) && checkColSum(arr) && checkDiagSum(arr))
{
cout<<\"\ Array is a Lo Shu Magic Square: \";
showArray(arr);
}
else
cout<<\"\ Array is not a Lo Shu Magic Square: \";
}
int main()
{
int a[3][3], r, c;
//Accept data
cout<<\"\ Enter data to the array: \ \";
for(r = 0; r < 3; r++)
{
for(c = 0; c < 3; c++)
{
cin>>a[r][c];
}
}
showArray(a);
if(checkRange(a))
cout<<\"\ Within range\";
else
cout<<\"\ Not Within range\";
if(checkRowSum(a))
cout<<\"\ Row Sum Equal\";
else
cout<<\"\ Row Sum Not Equal\";
if(checkColSum(a))
cout<<\"\ Column Sum Equal\";
else
cout<<\"\ Column Sum Not Equal\";
if(checkDiagSum(a))
cout<<\"\ Diagonal Sum Equal\";
else
cout<<\"\ Diagonal Sum Not Equal\";
isMagicSquare(a);
}
Output 1:
Enter data to the array:
4
9
2
3
5
7
8
1
6
Array Contents:
4 9 2
3 5 7
8 1 6
Within range
Row Sum Equal
Column Sum Equal
Diagonal Sum Equal
Array is a Lo Shu Magic Square:
Array Contents:
4 9 2
3 5 7
8 1 6
Output 2:
Enter data to the array:
1
2
3
4
5
6
7
8
9
Array Contents:
1 2 3
4 5 6
7 8 9
Within range
Row Sum Not Equal
Column Sum Not Equal
Diagonal Sum Equal
Array is not a Lo Shu Magic Square:
Output 3:
Enter data to the array:
-1
2
5
10
7
8
5
3
5
Array Contents:
-1 2 5
10 7 8
5 3 5
Not Within range
Row Sum Not Equal
Column Sum Not Equal
Diagonal Sum Not Equal
Array is not a Lo Shu Magic Square:



