Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.07 KB | None | 0 0
  1. // Chapter 7, Programming Challenge 8: Lo Shu Magic Square
  2. #include<iostream>
  3. using namespace std;
  4. // Global constants
  5. const int ROWS = 3; // The number of rows in the array
  6. const int COLS = 3; // The number of columns in the array
  7. const int MIN = 1; // The value of the smallest number
  8. const int MAX = 9; // The value of the largest number
  9. // Function prototypes
  10. void showResult(int[][COLS]);
  11. void showArray(int[][COLS]);
  12. bool isMagicSquare(int[][COLS]);
  13. bool checkRange(int[][COLS]);
  14. bool checkUnique(int[][COLS]);
  15. bool checkRowSum(int[][COLS]);
  16. bool checkColSum(int[][COLS]);
  17. bool checkDiagSum(int[][COLS]);
  18. int main()
  19. {
  20.  // Create a magic two-dimensional array.
  21.  int magicArray[ROWS][COLS] = { {4, 9, 2},
  22.  {3, 5, 7},
  23.  {8, 1, 6} };
  24.  // Create a normal two-dimensional array.
  25.  int normalArray[ROWS][COLS] = { {10, 2, 3},
  26.  {4, 15, 6},
  27.  {7, 8, -8} };
  28.  // Display the normal array.
  29.  showArray(normalArray);
  30.  // Test the normal array and display the result.
  31.  showResult(normalArray);
  32.  // Display the magic array.
  33.  showArray(magicArray);
  34.  // Test the magic array and display the result.
  35.  showResult(magicArray);
  36.  return 0;
  37. }
  38. // ********************************************************
  39. // The showResult function accepts a two-dimensional int *
  40. // array as an argument, tests to determine if it is a *
  41. // Lo Shu Magic Square and displays the result. *
  42. // ********************************************************
  43. void showResult(int values[][COLS])
  44. {
  45.  // Determine if the array is a Lo Shu Magic Square.
  46.  if (isMagicSquare(values))
  47.  {
  48.  // If so, display a message indicating that the
  49.  // array is a magic square.
  50.  cout << "This is a Lo Shu magic square.\n\n";
  51.  }
  52.  else
  53.  {
  54.  // If not, display a message indicating that the
  55.  // array is not a magic square.
  56.  cout << "This is not a Lo Shu magic square.\n\n";
  57.  }
  58. }
  59. // ********************************************************
  60. // The showArray function accepts a two-dimensional int *
  61. // array as an argument and displays its contents. *
  62. // ********************************************************
  63. void showArray(int values[][COLS])
  64. {
  65.  // Step through all the values in the array.
  66.  for (int row = 0; row < ROWS; row++)
  67.  {
  68.  for (int col = 0; col < COLS; col++)
  69.  {
  70.  // Display the values in this row.
  71.  cout << values[row][col] << " ";
  72.  }
  73.  // End the line, so we can display the next row.
  74.  cout << endl;
  75.  }
  76. }
  77. // ********************************************************
  78. // The isMagicSquare function accepts a two-dimensional *
  79. // int array as an argument, and returns true if the *
  80. // array meets all the requirements of a magic square. *
  81. // Otherwise, it returns false. *
  82. // ********************************************************
  83. bool isMagicSquare(int values[][COLS])
  84. {
  85.  // Initialize the status to false.
  86.  bool status = false;
  87.  // Call the functions to check the array.
  88.  bool isInRange = checkRange(values);
  89.  bool isUnique = checkUnique(values);
  90.  bool isEqualRows = checkRowSum(values);
  91.  bool isEqualCols = checkColSum(values);
  92.  bool isEqualDiag = checkDiagSum(values);
  93.  // Determine if the array meets all the requirments.
  94.  if (isInRange &&
  95.  isUnique &&
  96.  isEqualRows &&
  97.  isEqualCols &&
  98.  isEqualDiag)
  99.  {
  100.  // If so, set the status to true.
  101.  status = true;
  102.  }
  103.  // Return the status.
  104.  return status;
  105. }
  106. // ********************************************************
  107. // The checkRange function accepts a two-dimensional int *
  108. // array as an argument, and returns true if the values *
  109. // are within the specified range. Otherwise, it returns *
  110. // false. *
  111. // ********************************************************
  112. bool checkRange(int values[][COLS])
  113. {
  114.  // Initialize status to true.
  115.  bool status = true;
  116.  // Step through all the values in the array.
  117.  for (int row = 0; row < ROWS; row++)
  118.  {
  119.  for (int col = 0; col < COLS; col++)
  120.  {
  121.  // Determine if the value is out of range.
  122.  if (values[row][col] < MIN || values[row][col] > MAX)
  123.  {
  124.  // If so, set status to false.
  125.  status = false;
  126.  }
  127.  }
  128.  }
  129.  // Return the status.
  130.  return status;
  131. }
  132. // ********************************************************
  133. // The checkUnique function accepts a two-dimensional int *
  134. // array as an argument, and returns true if the values *
  135. // in the array are unique. Otherwise, it returns false. *
  136. // ********************************************************
  137. bool checkUnique(int values[][COLS])
  138. {
  139.  bool status = true; // Initialize status to true.
  140.  int searchValue = MIN; // Initialize the search value.
  141.  int count = 0; // Initialize the counter to zero.
  142.  // Perform the search while the maximum value has
  143.  // not been reached and the values are unique.
  144.  while (searchValue <= MAX && status == true)
  145.  {
  146.  // Step through all the values in the array.
  147.  for (int row = 0; row < ROWS; row++)
  148.  {
  149.  for (int col = 0; col < COLS; col++)
  150.  {
  151.  // Determine if the current value equals
  152.  // the search value.
  153.  if (values[row][col] == searchValue)
  154.  {
  155.  // If so, increment the counter.
  156.  count++;
  157.  }
  158.  // Determine if the counter is greater than one.
  159.  if (count > 1)
  160.  {
  161.  // If so, the values are not unique.
  162.  // Set the status to false.
  163.  status = false;
  164.  }
  165.  }
  166.  }
  167.  // Increment the search value.
  168.  searchValue++;
  169.  // Reset the counter variable.
  170.  count = 0;
  171.  }
  172.  // Return the status.
  173.  return status;
  174. }
  175. // ********************************************************
  176. // The checkRowSum function accepts a two-dimensional *
  177. // int array as an argument, and returns true if the sum *
  178. // of the values in each of the array's rows are equal. *
  179. // Otherwise, it returns false. *
  180. // ********************************************************
  181. bool checkRowSum(int values[][COLS])
  182. {
  183.  // Initialize status to true.
  184.  bool status = true;
  185.  // Calculate the sum of the values in the first row.
  186.  int sumRowA = values[0][0] + values[0][1] + values[0][2];
  187.  // Calculate the sum of the values in the second row.
  188.  int sumRowB = values[1][0] + values[1][1] + values[1][2];
  189.  // Calculate the sum of the values in the third row.
  190.  int sumRowC = values[2][0] + values[2][1] + values[2][2];
  191.  // Determine if the sum of the columns is not equal.
  192.  if ( (sumRowA != sumRowB) ||
  193.  (sumRowA != sumRowC) ||
  194.  (sumRowB != sumRowC) )
  195.  {
  196.  // If so, set status to false.
  197.  status = false;
  198.  }
  199.  // Return the status.
  200.  return status;
  201. }
  202. // ********************************************************
  203. // The checkColSum function accepts a two-dimensional *
  204. // int array as an argument, and returns true if the sum *
  205. // of the values in each of the array's columns are *
  206. // equal. Otherwise, it returns false. *
  207. // ********************************************************
  208. bool checkColSum(int values[][COLS])
  209. {
  210.  // Initialize status to true.
  211.  bool status = true;
  212.  // Calculate the sum of the values in the first column.
  213.  int sumColA = values[0][0] + values[1][0] + values[2][0];
  214.  // Calculate the sum of the values in the second column.
  215.  int sumColB = values[0][1] + values[1][1] + values[2][1];
  216.  // Calculate the sum of the values in the third column.
  217.  int sumColC = values[0][2] + values[1][2] + values[2][2];
  218.  // Determine if the sum of the columns is not equal.
  219.  if ( (sumColA != sumColB) ||
  220.  (sumColA != sumColC) ||
  221.  (sumColB != sumColC) )
  222.  {
  223.  // If so, set status to false.
  224.  status = false;
  225.  }
  226.  // Return the status.
  227.  return status;
  228. }
  229. // ********************************************************
  230. // The checkDiagSum function accepts a two-dimensional *
  231. // int array as an argument, and returns true if the sum *
  232. // of the values in each of the array's diagonals are *
  233. // equal. Otherwise, it returns false. *
  234. // ********************************************************
  235. bool checkDiagSum(int values[][COLS])
  236. {
  237.  // Initialize status to true;
  238.  bool status = true;
  239.  // Calculate the sum of the values in the first diagonal.
  240.  int sumDiagA = values[0][0] + values[1][1] + values[2][2];
  241.  // Calculate the sum of the values in the second diagonal.
  242.  int sumDiagB = values[2][0] + values[1][1] + values[0][2];
  243.  // Determine if the sum of the diagonals is not equal.
  244.  if (sumDiagA != sumDiagB)
  245.  {
  246.  // If so, set status to false.
  247.  status = false;
  248.  }
  249.  // Return the status.
  250.  return status;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement