Your programs should include appropriate comments and should

Your programs should include appropriate comments and should follow the program documentation requirements listed in the syllabus.

(30 points) Do programing project # 8 (Lo Shu Magic Square) on page 450.  

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

Solution

CODE:
#include<iostream>
#define ROWS 3;
#define COLS 3;

void showArray(int arr[][3]) {
int rows=ROWS;
int cols=COLS;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
std::cout<<arr[i][j]<<\" \";
}
std::cout<<\"\ \";
}
}

bool checkRange(int arr[][3]) {
int rows=ROWS;
int cols=COLS;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(arr[i][j]<1 || arr[i][j]>9) //if any value doesn\'t fit in range it returns false
return false;
}
}
return true; //If all values were in range it returns true
}

bool checkRowSum(int arr[][3]) {
int rows=ROWS;
int cols=COLS;
int sum0,sum1;
sum0=sum1=0;

for(int i=0;i<rows;i++){
sum1=0;
for(int j=0;j<cols;j++){
sum1+=arr[i][j];
}
if(i !=0 && sum1!=sum0){ //If sum of this row is not equal to previous row sum it returns false
return false;
}
sum0=sum1;
}
return true; //It Returns true is sum were equal
}

bool checkColSum(int arr[][3]) {
int rows=ROWS;
int cols=COLS;
int sum0,sum1;
sum0=sum1=0;

for(int i=0;i<cols;i++){
sum1=0;
for(int j=0;j<rows;j++){
sum1+=arr[j][i];
}
if(i !=0 && sum1!=sum0){ //If sum of current column is not equal to previous column, it returns false
return false;
}
sum0=sum1;
}
return true;
}

bool checkDiagSum(int arr[][3]) {
int rows=ROWS;
int cols=COLS;
int d1,d2;
d1=d2=0;

for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(i==j){ //For first diagonal
d1+=arr[i][j];
}
if(i+j==(cols-1)){ //For second diagonal
d2+=arr[i][j];
}
}
}

if(d1==d2) //If both diagonal sums are equal
return true;
else
return false;
}

bool isMagicSquare(int arr[][3]) {
if(checkRange(arr)){
int rows=ROWS;
int cols=COLS;
int values[9]={0,0,0,0,0,0,0,0,0};
  
for(int i=0;i<cols;i++){
for(int j=0;j<rows;j++){
values[arr[i][j]-1]++; //Increment the value by one at same index as the value
}
}
  
for (int i=0;i<9;i++) {
if(values[i]>1){ //If any value is greater than one it shows that value was repeated
return false;
}
}
return true;
}
return false;
}
  
int main(){
   std::cout<<\"Enter values:\"<<\"\ \";
int arr[3][3];
int rows=ROWS;
int cols=COLS;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
std::cin>>arr[i][j];
}
}
  
std::cout<<\"Check Range:\"<<checkRange(arr)<<\"\ \";
std::cout<<\"Check Row Sum:\"<<checkRowSum(arr)<<\"\ \";
std::cout<<\"Check Col Sum:\"<<checkColSum(arr)<<\"\ \";
std::cout<<\"Check Diagonal Sum:\"<<checkDiagSum(arr)<<\"\ \";
std::cout<<\"Check Magic Square:\"<<isMagicSquare(arr)<<\"\ \";
std::cout<<\"Entered values:\"<<\"\ \";
showArray(arr);
   }

OUTPUT:

Enter values:
1
2
3
4
5
6
7
8
9
Check Range:1
Check Row Sum:0
Check Col Sum:0
Check Diagonal Sum:1
asihsoiadCheck Magic Square:1
Entered values:
1 2 3
4 5 6
7 8 9

Your programs should include appropriate comments and should follow the program documentation requirements listed in the syllabus. (30 points) Do programing pro
Your programs should include appropriate comments and should follow the program documentation requirements listed in the syllabus. (30 points) Do programing pro
Your programs should include appropriate comments and should follow the program documentation requirements listed in the syllabus. (30 points) Do programing pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site