Write a program that creates a "magic" square. The "magic" square is a matrix in which the sums of numbers in each horizontal row, in each vertical column and along the diagonals are the same.

diagonally everything turned out, but the horizontal and vertical - is problematic. help pliz)

#include <iostream> #include <time.h> using namespace std; void main() { label: const int row = 3, col = 3; int arr[row][col]; srand(time(NULL)); int diag1 = 0, diag2 = 0, gor = 0, vert = 0, gor2=0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { arr[i][j] = rand() % 10; cout << arr[i][j] << " "; } cout << endl; } cout << endl << endl; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (i == j) { diag1 += arr[i][j]; } if (i + j == row - 1) { diag2 += arr[i][j]; } } } //gor for (int l = 0; l < row; l++) { for (int v = 0; v < col; v++) { gor += arr[l][v]; } gor = 0; } int tmp = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { gor += arr[i][j]; if ((i < row) && (j < col)) gor2 += arr[i + 1][j + 1]; else gor2 += arr[i - 1][j - 1]; // cout << gor << " "; tmp = gor; } if (tmp != gor2) { goto label; } gor2 = 0; } if (diag1 != diag2) { goto label; } cout << diag1 << " " << diag2 << endl << gor << endl; } 
  • What is the problem? - Vladimir Martyanov
  • @ Vladimir Martiyanov confused with verticals and contours - tkachuk
  • one
    3x3 matrix exactly needed? Here is a ready-made example using stl codeproject.com/Articles/4952/Magic-Square - Duracell

2 answers 2

So you need to check generated randomly or generate? If check - see check() , if generate 3x3 - see main() :)

 bool check(int * arr, int size) { #define a(i,j) (*(arr+i*size+j)) int sum1 = 0, sum2 = 0; for(int i = 0; i < size; ++i) { sum1 += a(i,i); sum2 += a(i,size-i-1); } if (sum1 != sum2) return false; int sh, sv; for(int i = 0; i < size; ++i) { sv = sh = 0; for(int j = 0; j < size; ++j) { sv += a(j,i); sh += a(i,j); } if ((sv != sum1) || (sh != sum1)) return false; } return true; } int main(int argc, const char * argv[]) { const int row = 3, col = 3; int arr[row][col]; srand(time(nullptr)); int a = rand()%10 + 1; int b = rand()%10 + 1; int c = rand()%10 + 1; arr[0][0] = a + b; arr[0][1] = a + 2*(b+c); arr[0][2] = a + c; arr[1][0] = a + 2*c; arr[1][1] = a + b + c; arr[1][2] = a + 2*b; arr[2][0] = a + 2*b + c; arr[2][1] = a; arr[2][2] = a + b + 2*c; for(int r = 0; r < 3; ++r) { for(int c = 0; c < 3; ++c) { cout << setw(4) << arr[r][c]; } cout << "\n"; } cout << "\nIs magic: " << (check((int*)arr,3) ? "yes" : "no") << "\n"; } 

Upd. Option more specifically:

 bool check(int arr[][3]) { int sum1 = 0, sum2 = 0; for(int i = 0; i < 3; ++i) { sum1 += arr[i][i]; sum2 += arr[i][2-i]; } if (sum1 != sum2) return false; int sh, sv; for(int i = 0; i < 3; ++i) { sv = sh = 0; for(int j = 0; j < 3; ++j) { sv += arr[j][i]; sh += arr[i][j]; } if ((sv != sum1) || (sh != sum1)) return false; } return true; } 
  • I need a universal generation of validation - toit needs to be done so that after changing the number of columns \ rows - no need to change the code. Now I will try to implement it with the help of your code. thanks =) - tkachuk
  • @tkachuk is actually a universal code there. - pavel
  • @pavel nouveau I put const int row = 4, col = 4; and everything, and you need to change the code, where the generation of 3x3 - tkachuk
  • one
    @tkachuk Well, you understand, multidimensional arrays must be passed with the last index, like (int arr[][3]) - and this kills universality. Therefore, we simply use the fact that a multidimensional array is the same one-dimensional, it’s just that the lines in it go in a row. Items 0-2 - first line, 3-5 - second, etc. - so #define a(i,j) (*(arr+i*size+j)) is a macro to simplify accessing the [i][j] element of the square two-dimensional arr array with size , which is passed to the function just as pointer. - Harry
  • one
    @tkachuk This is all a long time to explain, so look at the answer for another option, for specifically the 3x3 matrix, without tricks ... - Harry

made

 #include <iostream> #include <time.h> using namespace std; void main() { const int row = 3, col = 4; int arr[row][col], count = 0; srand(time(NULL)); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { arr[i][j] = rand() % 100; cout << arr[i][j] << " "; } cout << endl; } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (((i - 1) < 0) && ((j - 1) < 0)) { if ((arr[i][j] < arr[i + 1][j]) && (arr[i][j] < arr[i][j + 1])){ count++; cout << arr[i][j] << " "; } } else if (((i + 1) >= row) && ((j + 1) >= col)) { if ((arr[i][j] < arr[i - 1][j]) && (arr[i][j] < arr[i][j - 1])){ count++; cout << arr[i][j] << " "; } } else if ((j - 1) < 0) { if ((arr[i][j] < arr[i][j + 1]) && (arr[i][j] < arr[i - 1][j]) && (arr[i][j] < arr[i + 1][j])){ count++; cout << arr[i][j] << " "; } } else if ((i + 1) >= row) { if ((arr[i][j] < arr[i - 1][j]) && (arr[i][j] < arr[i][j + 1])){ cout << arr[i][j] << " "; count++; } } else if ((i - 1) < 0) { if ((arr[i][j] < arr[i][j - 1]) && (arr[i][j] < arr[i][j + 1]) && (arr[i][j] < arr[i + 1][j])){ count++; cout << arr[i][j] << " "; } } else if ((arr[i][j] < arr[i + 1][j]) && (arr[i][j] < arr[i - 1][j]) && (arr[i][j] < arr[i][j + 1]) && (arr[i][j] < arr[i][j - 1])) { count++; cout << arr[i][j] << " "; } else if (((j - 1) < 0)&&((i + 1) >= row)) { if ((arr[i][j] < arr[i][j - 1]) && (arr[i][j] < arr[i + 1][j])) { count++; cout << arr[i][j] << " "; } } else if ((j + 1) >= col) { if ((arr[i][j] < arr[i + 1][j]) && (arr[i][j] < arr[i][j - 1])) { count++; cout << arr[i][j] << " "; } } else if ((i + 1) >= row) { if ((arr[i][j] < arr[i - 1][j]) && (arr[i][j] < arr[i][j + 1]) && (arr[i][j] < arr[i][j - 1])){ count++; cout << arr[i][j] << " ";} } } } cout << endl << count << endl; }