Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Chapter 7, Programming Challenge 8: Lo Shu Magic Square
- #include<iostream>
- using namespace std;
- // Global constants
- const int ROWS = 3; // The number of rows in the array
- const int COLS = 3; // The number of columns in the array
- const int MIN = 1; // The value of the smallest number
- const int MAX = 9; // The value of the largest number
- // Function prototypes
- void showResult(int[][COLS]);
- void showArray(int[][COLS]);
- bool isMagicSquare(int[][COLS]);
- bool checkRange(int[][COLS]);
- bool checkUnique(int[][COLS]);
- bool checkRowSum(int[][COLS]);
- bool checkColSum(int[][COLS]);
- bool checkDiagSum(int[][COLS]);
- int main()
- {
- // Create a magic two-dimensional array.
- int magicArray[ROWS][COLS] = { {4, 9, 2},
- {3, 5, 7},
- {8, 1, 6} };
- // Create a normal two-dimensional array.
- int normalArray[ROWS][COLS] = { {10, 2, 3},
- {4, 15, 6},
- {7, 8, -8} };
- // Display the normal array.
- showArray(normalArray);
- // Test the normal array and display the result.
- showResult(normalArray);
- // Display the magic array.
- showArray(magicArray);
- // Test the magic array and display the result.
- showResult(magicArray);
- return 0;
- }
- // ********************************************************
- // The showResult 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. *
- // ********************************************************
- void showResult(int values[][COLS])
- {
- // Determine if the array is a Lo Shu Magic Square.
- if (isMagicSquare(values))
- {
- // If so, display a message indicating that the
- // array is a magic square.
- cout << "This is a Lo Shu magic square.\n\n";
- }
- else
- {
- // If not, display a message indicating that the
- // array is not a magic square.
- cout << "This is not a Lo Shu magic square.\n\n";
- }
- }
- // ********************************************************
- // The showArray function accepts a two-dimensional int *
- // array as an argument and displays its contents. *
- // ********************************************************
- void showArray(int values[][COLS])
- {
- // Step through all the values in the array.
- for (int row = 0; row < ROWS; row++)
- {
- for (int col = 0; col < COLS; col++)
- {
- // Display the values in this row.
- cout << values[row][col] << " ";
- }
- // End the line, so we can display the next row.
- cout << endl;
- }
- }
- // ********************************************************
- // The isMagicSquare function accepts a two-dimensional *
- // int array as an argument, and returns true if the *
- // array meets all the requirements of a magic square. *
- // Otherwise, it returns false. *
- // ********************************************************
- bool isMagicSquare(int values[][COLS])
- {
- // Initialize the status to false.
- bool status = false;
- // Call the functions to check the array.
- bool isInRange = checkRange(values);
- bool isUnique = checkUnique(values);
- bool isEqualRows = checkRowSum(values);
- bool isEqualCols = checkColSum(values);
- bool isEqualDiag = checkDiagSum(values);
- // Determine if the array meets all the requirments.
- if (isInRange &&
- isUnique &&
- isEqualRows &&
- isEqualCols &&
- isEqualDiag)
- {
- // If so, set the status to true.
- status = true;
- }
- // Return the status.
- return status;
- }
- // ********************************************************
- // The checkRange function accepts a two-dimensional int *
- // array as an argument, and returns true if the values *
- // are within the specified range. Otherwise, it returns *
- // false. *
- // ********************************************************
- bool checkRange(int values[][COLS])
- {
- // Initialize status to true.
- bool status = true;
- // Step through all the values in the array.
- for (int row = 0; row < ROWS; row++)
- {
- for (int col = 0; col < COLS; col++)
- {
- // Determine if the value is out of range.
- if (values[row][col] < MIN || values[row][col] > MAX)
- {
- // If so, set status to false.
- status = false;
- }
- }
- }
- // Return the status.
- return status;
- }
- // ********************************************************
- // The checkUnique function accepts a two-dimensional int *
- // array as an argument, and returns true if the values *
- // in the array are unique. Otherwise, it returns false. *
- // ********************************************************
- bool checkUnique(int values[][COLS])
- {
- bool status = true; // Initialize status to true.
- int searchValue = MIN; // Initialize the search value.
- int count = 0; // Initialize the counter to zero.
- // Perform the search while the maximum value has
- // not been reached and the values are unique.
- while (searchValue <= MAX && status == true)
- {
- // Step through all the values in the array.
- for (int row = 0; row < ROWS; row++)
- {
- for (int col = 0; col < COLS; col++)
- {
- // Determine if the current value equals
- // the search value.
- if (values[row][col] == searchValue)
- {
- // If so, increment the counter.
- count++;
- }
- // Determine if the counter is greater than one.
- if (count > 1)
- {
- // If so, the values are not unique.
- // Set the status to false.
- status = false;
- }
- }
- }
- // Increment the search value.
- searchValue++;
- // Reset the counter variable.
- count = 0;
- }
- // Return the status.
- return status;
- }
- // ********************************************************
- // The checkRowSum 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. *
- // ********************************************************
- bool checkRowSum(int values[][COLS])
- {
- // Initialize status to true.
- bool status = true;
- // Calculate the sum of the values in the first row.
- int sumRowA = values[0][0] + values[0][1] + values[0][2];
- // Calculate the sum of the values in the second row.
- int sumRowB = values[1][0] + values[1][1] + values[1][2];
- // Calculate the sum of the values in the third row.
- int sumRowC = values[2][0] + values[2][1] + values[2][2];
- // Determine if the sum of the columns is not equal.
- if ( (sumRowA != sumRowB) ||
- (sumRowA != sumRowC) ||
- (sumRowB != sumRowC) )
- {
- // If so, set status to false.
- status = false;
- }
- // Return the status.
- return status;
- }
- // ********************************************************
- // The checkColSum 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. *
- // ********************************************************
- bool checkColSum(int values[][COLS])
- {
- // Initialize status to true.
- bool status = true;
- // Calculate the sum of the values in the first column.
- int sumColA = values[0][0] + values[1][0] + values[2][0];
- // Calculate the sum of the values in the second column.
- int sumColB = values[0][1] + values[1][1] + values[2][1];
- // Calculate the sum of the values in the third column.
- int sumColC = values[0][2] + values[1][2] + values[2][2];
- // Determine if the sum of the columns is not equal.
- if ( (sumColA != sumColB) ||
- (sumColA != sumColC) ||
- (sumColB != sumColC) )
- {
- // If so, set status to false.
- status = false;
- }
- // Return the status.
- return status;
- }
- // ********************************************************
- // The checkDiagSum 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. *
- // ********************************************************
- bool checkDiagSum(int values[][COLS])
- {
- // Initialize status to true;
- bool status = true;
- // Calculate the sum of the values in the first diagonal.
- int sumDiagA = values[0][0] + values[1][1] + values[2][2];
- // Calculate the sum of the values in the second diagonal.
- int sumDiagB = values[2][0] + values[1][1] + values[0][2];
- // Determine if the sum of the diagonals is not equal.
- if (sumDiagA != sumDiagB)
- {
- // If so, set status to false.
- status = false;
- }
- // Return the status.
- return status;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement