K
K
kirillon372020-07-19 08:38:03
C++ / C#
kirillon37, 2020-07-19 08:38:03

How to write a program that finds the maximum number in a row and at the same time the minimum number in a column in a two-dimensional C++ array?

For example:

1 13 1
1 12 5
2 90 8

Answer: 12

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergei Chamkin, 2020-07-19
@Sergei1337

This program is not optimal, since in the case of several maxima in a row, the first one will be selected.
Create a function for part 2 and pass the necessary parameters to it.

for (int row = 0; row < n; row++) {
            int localMax = arr[row][0];
            int maxColumn=0;
            for (int column = 0; column < m; column++) {
                if (localMax < arr[row][column]) {
                    localMax = arr[row][column];
                    maxColumn=column;
                }
            }
           //2 часть
            int localMin = localMax;
            bool isMinInColumn=true;
            for(int row2=0;row2<n;row2++){
                if(arr[row2][maxColumn]<=localMin && row2!=row){
                    isMinInColumn=false;
                    break;
                }
            }
            if(isMinInColumn){
                //печатаем ответ тут
                //System.out.println("Ответ:"+localMin);
                break;
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question