Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CIS-210 Introduction to C++ Programming (Fall 2017) Assignment-5 Points: 100 Due

ID: 3842099 • Letter: C

Question

CIS-210 Introduction to C++ Programming (Fall 2017) Assignment-5 Points: 100 Due Date: 4/26/2017 (beginning of class) 1. A magic square is a two-dimensional array of positive integers, which have the following characteristics The number of rows equals the number of columns All entries must be positive integers The sum of all entries in each row, each column and the two diagonals are write a C++ program to determine if a set of numbers represents a magic4x4 Square. Read a set of numbers from the keyboard (see sample input below) Validate the input to ensure integer numbers are positive. If a negative number is given, generate an error message and prompt the user to reenter a number Your program must include the following functions: o main-display welcome message, declare array, call user defined functions getlnput, checksums, checkRowSums, checkColSums, checkDiagSums, and a splayResult o getlnput prompts uur for and validates the input (see sample input below) o checkSums computes an initial sum and then calls the functions checkRowSums, checkCoISums, and checkDiagsums. The functions checkRowSums, checkColSums, and checkDiagsums should return a Boolean value that indicates ifthe corresponding sums represent a magic Square Note: the check R check Col and checkDiagSums functions should only be called if necessary o display Result -displays the results (see sample output below)

Explanation / Answer

Here is your program below: -

#include<iostream>
using namespace std;

const int NUM_ROWS = 4;
const int NUM_COLS = 4;

void getInput(int magicSq[][NUM_COLS], int NUM_ROWS);
bool checkSums(int magicSq[][NUM_COLS], int NUM_ROWS);
bool checkRowSums(int magicSq[][NUM_COLS], int NUM_ROWS);
bool checkColSums(int magicSq[][NUM_COLS], int NUM_ROWS);
bool checkDiagSums(int magicSq[][NUM_COLS], int NUM_ROWS);
void displayResult(bool isMagicSq);

int main() {
   int magicSq[NUM_ROWS][NUM_COLS];
   cout<<"*** Welcome to the Magic "<<NUM_ROWS<<"X"<<NUM_COLS<<" Square Problem **** ";
   //display welcome message
   getInput(magicSq,NUM_ROWS);
   //cout<< magicSq[0][0] << endl;
   //cout<< magicSq[1][2] << endl;
  
   if(checkSums(magicSq,NUM_ROWS)) {
       displayResult(true);
   } else {
       displayResult(false);
   }
   system("pause");
   return 0;
}

void getInput(int magicSq[][NUM_COLS], int NUM_ROWS) {
   int input;
   for(int i = 0;i<NUM_ROWS;i++) {
       for(int j = 0;j<NUM_COLS;j++) {
           input = -1;
           while(input < 0) {
               cout<< "Enter the value of the cell in row "<<(i+1)<<" and column "<<(j+1)<<":";
               cin >> input;
               if(input < 0) {
                   cout<< "** Error - Numbers must be positive"<<endl;
               }
           }
           magicSq[i][j] = input;
       }
       cout<<endl;
   }
}

bool checkSums(int magicSq[][NUM_COLS], int NUM_ROWS) {
   if(checkRowSums(magicSq,NUM_ROWS) && checkColSums(magicSq,NUM_ROWS) && checkDiagSums(magicSq,NUM_ROWS)) {
       return true;
   } else {
       return false;
   }
}

bool checkRowSums(int magicSq[][NUM_COLS], int NUM_ROWS) {
   // Compare all results to the First sum.
   int firstSum = 0;
   for (int j = 0; j < NUM_ROWS; j++)
   {
       firstSum += magicSq[j][j];
   }
  
   for(int i = 0;i< NUM_ROWS;i++) {
       int rowSum = 0;
       for (int j = 0; j < NUM_ROWS; j++)
       {
           rowSum += magicSq[i][j];
       }
       if(rowSum != firstSum) {
           return false;
       }
   }
   return true;
}

bool checkColSums(int magicSq[][NUM_COLS], int NUM_ROWS) {
   // Compare all results to the First sum.
   int firstSum = 0;
   for (int j = 0; j < NUM_ROWS; j++)
   {
       firstSum += magicSq[j][j];
   }
  
   for(int i = 0;i< NUM_COLS;i++) {
       int colSum = 0;
       for (int j = 0; j < NUM_COLS; j++)
       {
           colSum += magicSq[j][i];
       }
       if(colSum != firstSum) {
           return false;
       }
   }
   return true;
}

bool checkDiagSums(int magicSq[][NUM_COLS], int NUM_ROWS) {
   // Compare all results to the First sum.
   int disgSumFromLeft = 0;
   for (int j = 0; j < NUM_ROWS; j++)
   {
       disgSumFromLeft += magicSq[j][j];
   }
   int disgSumFromRight = 0;
   for (int j = NUM_ROWS-1; j >= 0; j--)
   {
       disgSumFromRight += magicSq[j][j];
   }
   if(disgSumFromRight != disgSumFromLeft) {
       return false;
   } else {
       return true;
   }
}

void displayResult(bool isMagicSq) {
   if(isMagicSq) {
       cout<<"** Congratulations! - Your Number form a magic square"<<endl;
   } else {
       cout<<"** Sorry! - Your Number does not form a magic square"<<endl;
   }
}

Sample Run: -

1.

*** Welcome to the Magic 4X4 Square Problem ****

Enter the value of the cell in row 1 and column 1:1
Enter the value of the cell in row 1 and column 2:-14
** Error - Numbers must be positive
Enter the value of the cell in row 1 and column 2:14
Enter the value of the cell in row 1 and column 3:14
Enter the value of the cell in row 1 and column 4:4

Enter the value of the cell in row 2 and column 1:11
Enter the value of the cell in row 2 and column 2:7
Enter the value of the cell in row 2 and column 3:6
Enter the value of the cell in row 2 and column 4:9

Enter the value of the cell in row 3 and column 1:8
Enter the value of the cell in row 3 and column 2:10
Enter the value of the cell in row 3 and column 3:10
Enter the value of the cell in row 3 and column 4:5

Enter the value of the cell in row 4 and column 1:13
Enter the value of the cell in row 4 and column 2:2
Enter the value of the cell in row 4 and column 3:3
Enter the value of the cell in row 4 and column 4:15

** Congratulations! - Your Number form a magic square
Press any key to continue . . .

2.

*** Welcome to the Magic 4X4 Square Problem ****

Enter the value of the cell in row 1 and column 1:1
Enter the value of the cell in row 1 and column 2:14
Enter the value of the cell in row 1 and column 3:14
Enter the value of the cell in row 1 and column 4:4

Enter the value of the cell in row 2 and column 1:11
Enter the value of the cell in row 2 and column 2:7
Enter the value of the cell in row 2 and column 3:6
Enter the value of the cell in row 2 and column 4:9

Enter the value of the cell in row 3 and column 1:8
Enter the value of the cell in row 3 and column 2:10
Enter the value of the cell in row 3 and column 3:2
Enter the value of the cell in row 3 and column 4:5

Enter the value of the cell in row 4 and column 1:13
Enter the value of the cell in row 4 and column 2:2
Enter the value of the cell in row 4 and column 3:3
Enter the value of the cell in row 4 and column 4:15

** Sorry! - Your Number does not form a magic square
Press any key to continue . . .