#include <iostream> #include <conio.h> #include <math.h> using namespace std; void main() { int n; int ** x; cin >> n; x = new int * [2 * n]; for (int i = 0; i < 2 * n; i++) { x[i] = new int[2 * n]; } for (int i = 0; i < 2 * n; i++) for (int j = 0; j < 2 * n; j++) { if (i < n && j < n) { x[i][j] = 1; } if (i < n && j > n) { x[i][j] = 2; } if (i > n && j < n) { x[i][j] = 3; } if (i > n && j > n) { x[i][j] = 4; } } for (int i = 0; i < 2 * n; i++) { cout << endl; for (int j = 0; j < 2 * n; j++) { cout << x[i][j]; } } _getch(); } 
  • one
    And what should show? - Anton Shchyrov
  • You have not considered the options, where i = n for any j and j = n, for any i. Here in all the combinations of these indices and get garbage. - Vladimir

1 answer 1

Your conditions do not cover the case when i or j is equal to n , and therefore uninitialized data remains in the cells with these indices. Just add pre-zeroing in the set-up loop:

 for (int i = 0; i < 2 * n; i++) for (int j = 0; j < 2 * n; j++) { x[i][j] = 0; // <-- вот это if (i < n && j < n) { 

Well, or process the branch separately.