Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- bool square(int sq[3][3]) {
- int sum = sq[0][0] + sq[0][1] + sq[0][2];
- int row = 0;
- int column = 0;
- int diag1 = 0;
- int diag2 = 0;
- for (int c = 0; c < 3; c++) {
- for (int r = 0; r < 3; r++) {
- row += sq[r][c];
- column += sq[c][r];
- diag1 += sq[r][r];
- diag2 += sq[3 - r - 1][r];
- }
- }
- if (row/3 != sum || column/3 != sum || diag1/3 != sum || diag2/3 != sum) {
- return false;
- }
- else return true;
- }
- void display(int sq[3][3]) {
- cout << "____________" << endl;
- cout << "| " << sq[0][0] << " | " << sq[1][0] << " | " << sq[2][0] << " |" << endl;
- cout << "____________" << endl;
- cout << "| " << sq[0][1] << " | " << sq[1][1] << " | " << sq[2][1] << " |" << endl;
- cout << "____________" << endl;
- cout << "| " << sq[0][2] << " | " << sq[1][2] << " | " << sq[2][2] << " |" << endl;
- cout << "____________" << endl;
- }
- int main() {
- int sq[3][3];
- int count = 1;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- sq[j][i] = count;
- count++;
- }
- }
- cout << "Square: \n";
- display(sq);
- cout << square(sq);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement