The task is still insufficiently specified. Here is a solution that prints the coordinates of the desired matrices larger than 2x2 (including nested solutions — there will be three 2x2 matrices in the 3x3 matrix).
#include <iostream> #include <windows.h> using namespace std; int adim (int a[50][50], int n, int m, int i, int j) { int t, c; c = 1; while (1) { // проверяем следующую границу if (i+c == n || j+c == m) goto ex; for(t = i; t <= i + c; t++) if(a[t][j+c] == 0) goto ex; for(t = j; t < j+c; t++) if(a[i+c][t] == 0) goto ex; c++; } ex: return c; } int main() { int i,j,n,m,c; int a[50][50]; srand(GetTickCount()); cerr << "Input n,m:" << endl; cin >> n >> m; cout << "Array a" << endl; /* sozdanie mtarici iz 1 and 0*/ for(i=0;i<n;i++) { for(j=0;j<m;j++) { cout << (a[i][j]=rand()%2); if (j+1 < m) cout << " "; } cout << endl; } for(i=0; i<n; i++) for(j=0; j<m; j++) if (a[i][j] && (c = adim(a, n, m, i, j)) > 1) cout << i << ' ' << j << ' ' << c << endl; return 0; }
Well, goto
aesthetes can be replaced with exceptions.
UPD c - size, height / width. In the adim function, this is a loop counter. The cells are checked (i + c, j) ... (i + c, j + c) and (i, j + c) ... (i + c, j + c) - the right and lower edges (from 1 is smaller, for a 2x2 matrix c == 1). With a successful test, c is incremented.