Advertisement
Brandan

CS Assignment 161 #2.cpp

Oct 14th, 2013
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. // Brandan Tyler Lasley
  2. // Sources: None
  3. // 10/6/2013 20:44
  4. // This program checks and tests for a magic square.
  5.  
  6. #include <iostream>
  7. #include <string>
  8. using namespace std;
  9.  
  10. int main () {
  11.     // Declare Varibles
  12.     int square[16];
  13.  
  14.     cout << "Welcome to Brandan's Magic Square Checker!" << endl << endl;
  15.     cout << "Please enter a magic square! Do NOT exceed 16 (ex. 1 2 3 4 5 6... etc)" << endl;
  16.  
  17.     // Begain square collection loop.
  18.     int i = 0;
  19.     do {
  20.         /* Delete this if you want a more user friendly approach.
  21.         cout << "Please enter a number [" << i + 1 << "\\16]  ";*/
  22.  
  23.         cin >> square[i];
  24.         i++;
  25.     } while (i < 16);
  26.  
  27.     // Calcuate and declare calcuation varibles 1
  28.     int sum_r1 = 0;
  29.     int sum_r2 = 0;
  30.     int sum_r3 = 0;
  31.     int sum_r4 = 0;
  32.     int i2 = 0;
  33.  
  34.     // Loop array and calcuate
  35.     for each(int sqr in square) {
  36.         if (i2 < 4) {
  37.             sum_r1 = sum_r1 + sqr;
  38.         } else if (i2 < 8) {
  39.             sum_r2 = sum_r2 + sqr;
  40.         } else if (i2 < 12) {
  41.             sum_r3 = sum_r3 + sqr;
  42.         } else if (i2 < 16) {
  43.             sum_r4 = sum_r4 + sqr;
  44.         }
  45.         i2++;
  46.     }
  47.    
  48.  
  49.     // Check if square or not.
  50.     if (sum_r1 == sum_r2 && sum_r2 == sum_r3 && sum_r3 == sum_r4 && sum_r1) {
  51.         cout << endl << "Magic Square!" << endl;
  52.     } else {
  53.         cout << endl << "This is NOT a magic square!!" << endl;
  54.     }
  55.  
  56.  
  57.     // Exit footer
  58.     getchar();
  59.     getchar();
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement