enter image description here

Already confused with this task, in general, it seems to work, but sometimes the minimum / maximum elements are determined incorrectly. Where is the mistake? Help me please.

#include <iostream> #include <locale.h> #include <cstdlib> #include <ctime> using namespace std; int main() { int a[10][10]; int i, j, l, k, n, maximal=-1, minimal=10; setlocale(LC_ALL, "rus"); cout << "Введите n" << endl; cin >> n; cout << "Введите K" << endl; cin >> k; cout << "Введите L" << endl; cin >> l; cout << endl; srand(time(NULL)); for (i = 0; i < n; i++) { for(j = 0; j < n; j++) { a[i][j] = rand()%10; cout << a[i][j] << " "; } cout << endl; } for (i = 1; i < k; i++) { for (j = l; j < n; j++) { if (a[i][j] > maximal) { maximal = a[i][j]; } } } for (i = 1; i < k; i++) { for (j = l; j < n; j++) { if (a[i][j] < minimal) { minimal = a[i][j]; } } } cout << "Максимальный элемент матрицы данной области равен " << maximal << endl; cout << "Минимальный элемент матрицы данной области равен " << minimal << endl; } 
  • What are k and l , and why does the cycle start with one? - Igor
  • k and l - indices of the lower left element of the area - Shinkiro
  • "left bottom" or "bottom left"? - Igor

1 answer 1

k and l - indices of the lower left element of the region

 minimal = a[0][l]; maximal = a[0][l]; for (i = 0; i <= k; i++) { for (j = l; j < n; j++) { if (a[i][j] > maximal) { maximal = a[i][j]; } if (a[i][j] < minimal) { minimal = a[i][j]; } } } 
  • everything works, thank you very much! - Shinkiro
  • @Shinkiro On health. Successes! The check mark is to the left of the answer. - Igor