Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool square(int sq[3][3]) {
  6. int sum = sq[0][0] + sq[0][1] + sq[0][2];
  7. int row = 0;
  8. int column = 0;
  9. int diag1 = 0;
  10. int diag2 = 0;
  11. for (int c = 0; c < 3; c++) {
  12. for (int r = 0; r < 3; r++) {
  13. row += sq[r][c];
  14. column += sq[c][r];
  15. diag1 += sq[r][r];
  16. diag2 += sq[3 - r - 1][r];
  17. }
  18. }
  19. if (row/3 != sum || column/3 != sum || diag1/3 != sum || diag2/3 != sum) {
  20. return false;
  21. }
  22. else return true;
  23. }
  24.  
  25. void display(int sq[3][3]) {
  26. cout << "____________" << endl;
  27. cout << "| " << sq[0][0] << " | " << sq[1][0] << " | " << sq[2][0] << " |" << endl;
  28. cout << "____________" << endl;
  29. cout << "| " << sq[0][1] << " | " << sq[1][1] << " | " << sq[2][1] << " |" << endl;
  30. cout << "____________" << endl;
  31. cout << "| " << sq[0][2] << " | " << sq[1][2] << " | " << sq[2][2] << " |" << endl;
  32. cout << "____________" << endl;
  33. }
  34.  
  35. int main() {
  36. int sq[3][3];
  37. int count = 1;
  38. for (int i = 0; i < 3; i++) {
  39. for (int j = 0; j < 3; j++) {
  40. sq[j][i] = count;
  41. count++;
  42. }
  43. }
  44. cout << "Square: \n";
  45. display(sq);
  46. cout << square(sq);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement