Advertisement
Lawnknome

sameSum.cpp

Nov 13th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. /****************************************************************************************************
  2. * Author:                                       Jared Hughes
  3. * Date Created:                                 11/13/2014
  4. * Last Modification Date:                       11//2014
  5. * Filename:                                     sameSum.cpp
  6. ****************************************************************************************************/
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11.  
  12. bool sameSum (int square[][10], int width);
  13. int diagonal_1 (int square[][10], int width);
  14. int diagonal_2 (int square[][10], int width);
  15.  
  16. int main()
  17. {
  18.     int length;
  19.     int width;
  20.  
  21.     cout << "\n\nPlease input the size of the side of a square you would like to compute: ";
  22.     cin >> width;
  23.  
  24.     int square [width][10];
  25.     int row = 0;
  26.  
  27.     cout << "\n\nYou will enter the square one row at a time.\n";
  28.  
  29.     while(row < width)
  30.     {  
  31.        
  32.         cout << "Please enter row " << (row+1) << " consisting of " << width << " integers, each separated by a space: ";
  33.        
  34.    
  35.             for (int col = 0; col < width; col++)
  36.             {
  37.                 cin >> square[row][col];       
  38.  
  39.             }
  40.    
  41.         row++;
  42.  
  43.     }  
  44.  
  45.     if(sameSum(square, width))
  46.     {  
  47.         cout << "\n\nThe square's sums are equal." << endl;
  48.     }
  49.        
  50.     else
  51.     {
  52.         cout << "\n\nThe square's sums are not equal." << endl;
  53.     }  
  54.  
  55.  
  56.     return 0;
  57. }  
  58.  
  59.  
  60. bool sameSum(int square[][10], int width)
  61. {
  62.     int diag1;
  63.     int diag2;
  64.     int row_sum;
  65.     int row = 0;
  66.  
  67.     diag1 = diagonal_1(square, width);
  68.     diag2 = diagonal_2(square, width);
  69.  
  70.     while(row < width)
  71.     {  
  72.         for (int col = 0; col < width; col++)
  73.         {      
  74.             row_sum += square[row][col];
  75.         }
  76.  
  77.         if(row_sum != diag1)
  78.         {
  79.             return false;
  80.         }  
  81.        
  82.         else
  83.             row++;
  84.             row_sum = 0;
  85.     }  
  86.  
  87.     if(diag1 == diag2)
  88.         return true;
  89.  
  90.     else
  91.         return false;
  92. }
  93.  
  94. int diagonal_1(int square[][10], int width)
  95. {
  96.     int row = 0;
  97.     int col = 0;
  98.     int diag1 = 0;
  99.  
  100.     while(row < width)
  101.     {
  102.         diag1 += square[row][col];
  103.  
  104.         row++;
  105.         col++;
  106.     }
  107.  
  108.     return diag1;
  109.  
  110. }
  111.  
  112. int diagonal_2(int square[][10], int width)
  113. {
  114.     int col = 0;
  115.     int diag2 = 0;
  116.     int count = 1;
  117.  
  118.     while(col < width)
  119.     {
  120.         diag2 += square[width-count][col];
  121.  
  122.         col++;
  123.         count++;
  124.     }
  125.  
  126.     return diag2;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement