Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************************************
- * Author: Jared Hughes
- * Date Created: 11/13/2014
- * Last Modification Date: 11//2014
- * Filename: sameSum.cpp
- ****************************************************************************************************/
- #include <iostream>
- using namespace std;
- bool sameSum (int square[][10], int width);
- int diagonal_1 (int square[][10], int width);
- int diagonal_2 (int square[][10], int width);
- int main()
- {
- int length;
- int width;
- cout << "\n\nPlease input the size of the side of a square you would like to compute: ";
- cin >> width;
- int square [width][10];
- int row = 0;
- cout << "\n\nYou will enter the square one row at a time.\n";
- while(row < width)
- {
- cout << "Please enter row " << (row+1) << " consisting of " << width << " integers, each separated by a space: ";
- for (int col = 0; col < width; col++)
- {
- cin >> square[row][col];
- }
- row++;
- }
- if(sameSum(square, width))
- {
- cout << "\n\nThe square's sums are equal." << endl;
- }
- else
- {
- cout << "\n\nThe square's sums are not equal." << endl;
- }
- return 0;
- }
- bool sameSum(int square[][10], int width)
- {
- int diag1;
- int diag2;
- int row_sum;
- int row = 0;
- diag1 = diagonal_1(square, width);
- diag2 = diagonal_2(square, width);
- while(row < width)
- {
- for (int col = 0; col < width; col++)
- {
- row_sum += square[row][col];
- }
- if(row_sum != diag1)
- {
- return false;
- }
- else
- row++;
- row_sum = 0;
- }
- if(diag1 == diag2)
- return true;
- else
- return false;
- }
- int diagonal_1(int square[][10], int width)
- {
- int row = 0;
- int col = 0;
- int diag1 = 0;
- while(row < width)
- {
- diag1 += square[row][col];
- row++;
- col++;
- }
- return diag1;
- }
- int diagonal_2(int square[][10], int width)
- {
- int col = 0;
- int diag2 = 0;
- int count = 1;
- while(col < width)
- {
- diag2 += square[width-count][col];
- col++;
- count++;
- }
- return diag2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement