Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. /*
  2. Author: X
  3. Date: 04/27/2015
  4. Purpose: Creates a 3x3 magic square. It's a magic square if the sum of the elements in each row, in each column, and in the two
  5. diagonals is the same value (see the magic square below).
  6. Algorithm:
  7. 1. 2D array of 3 * 3.
  8. 2. Array is filled with unique values (1-9) that are generated randomly.
  9. 3. Checks if the random number generated has been previously generated in the arrack.
  10. 4. If the number is unique, it is stored in the array.
  11. 5. The array is displayed.
  12. 6. Check if the array is a magic square.
  13. 7. Keep looping steps until magic square is found.
  14. */
  15. //Includes
  16. #include <iostream>
  17. #include <ctime>
  18. #include <cstdlib>
  19. //Functions
  20. int checkUnique(); // Function to check if the random number generated is a unique number, i.e if the number is not previously generated in the 2D array.
  21. int magicSquare(); // Function to check if the 2D array is a magic square. Checks if sum of each row and sum of each column and diagonal are equal.
  22.  
  23. using namespace std;
  24.  
  25. bool checkUnique(int s[][3], int num){
  26.   for (int i = 0; i < 3; i++){
  27.     for(int j = 0; j < 3; j++){
  28.       if (s[i][j] == 0)
  29.         return true;
  30.       else if (s[i][j] == num)
  31.         return false;
  32.     }
  33.   }
  34.   return true;
  35. }
  36.  
  37. void initializeArr(int s[][3], int size = 3){
  38.   // Code to generate a randum number.
  39.   bool uniq = false;
  40.   int num;
  41.  
  42.   // Iterate through the 2D array
  43.   for (int i = 0; i < 3; i++){
  44.     for (int j = 0; j < 3; j++){
  45.       // Generates a number until it is unique.
  46.       while (uniq == false){
  47.         num = rand() % 9 + 1;
  48.         uniq = checkUnique(s, num);
  49.       }
  50.       s[i][j] = num;
  51.       uniq = false;
  52.     }
  53.   }
  54. }
  55.  
  56. void displayArr(int s[][3], int size = 3){
  57.   // Iterate through the 2D array and display the element
  58.   for (int i = 0; i < 3; i++){
  59.     for (int j = 0; j < 3; j++){
  60.       cout << s[i][j] << "\t";
  61.     }
  62.     cout << endl;
  63.   }
  64. }
  65.  
  66. bool magicSquare(int s[][3], int size = 3){
  67.   // Value to store diagonals
  68.   int d1=0, d2=0;
  69.  
  70.   // Calulating the sum of each row and assigning it to subscript 3
  71.   for (int i = 0; i < 3; i++){
  72.     for (int j = 0; j < 3; j++){
  73.       s[i][3] += s[i][j];
  74.     }
  75.   }
  76.  
  77.   // Calculating the sum of each col and assigning to to subscript 3
  78.   for (int j = 0; j < 3; j++){
  79.     for (int i = 0; i < 3; i++){
  80.       s[3][j] += s[i][j];
  81.     }
  82.   }
  83.  
  84.   // Calculating the sum of the diagonal
  85.   for (int i = 0, j = 0; i < 3; i++, j++){
  86.     d1 += s[i][j];
  87.   }
  88.   for (int i = 3, j = 0; j < 3; i--, j++){
  89.     d2 += s[i][j];
  90.   }
  91.  
  92.   // See if the sum of everything are equal
  93.   if (s[0][2] == d1 && s[1][2] == d1 && s[2][0] == d1 && s[2][1] == d1 && d1 == d2)
  94.     return true;
  95.   else
  96.     return false;
  97. }
  98.  
  99. void zeroArr(int s[][3], int size = 3){
  100.   for (int i = 0; i < 3; i++){
  101.     for (int j = 0; j < 3; j++){
  102.       s[i][j] = 0;
  103.     }
  104.   }
  105. }
  106.  
  107. int main(){
  108.     srand(time(NULL)); //called at the beginning of main so
  109.   // Create the 2D 3x3 array
  110.   int square [3][3] = {0};
  111.   bool mSq = false;
  112.  
  113.   // Continue creating a new matrix until a magic square is found
  114.   while (mSq == false){
  115.     // Function call to fill in the array
  116.     initializeArr(square);
  117.     displayArr(square);
  118.     // Function call to determine if it is a magic square
  119.     mSq = magicSquare(square);
  120.     if (mSq == true)
  121. {
  122.  
  123.     displayArr(square);
  124.       cout << "Magic Square!" << endl;
  125. }
  126.     else{
  127.       cout << "Not a Magic Square!" << endl;
  128.       // Zero out the array
  129.       zeroArr(square);
  130.     }
  131.   }
  132.  
  133.   return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement