Finding the maximum value of each row in a C ++ 2D array

I managed to find the minimum value of each row of my 2D array using this

void findLowest(int A[][Cm], int n, int m)
{
    int min = A[0][0];
    for (int i = 0; i < n; i++)
    {
         for (int j = 0; j < m; j++)
         {
             if (A[i][j] < min)
             {
                 min = A[i][j];
             }
         }
     out << i << " row lowest value " << min << endl;
    }
}

I am trying to find the maximum value of each row using the same path, but this only shows the first maximum value

void findHighest(int A[][Cm], int n, int m)
{
     int max = A[0][0];
     for (int i = 0; i < n; i++)
     {
         for (int j = 0; j < m; j++)
         {
             if (A[i][j] > max)
             {
                max = A[i][j];
             }
         }
     out << i << " row highest value " << max << endl;
     }
}

I can’t find what is wrong with the second function, and why it only shows me the first maximum value that it finds. Any help?

+4
source share
4 answers

( ) , , max , . :

void findHighest(int A[][Cm], int n, int m)
{
     for (int i = 0; i < n; i++)
     {
         int max = A[i][0];
         for (int j = 1; j < m; j++)
         {
             if (A[i][j] > max)
             {
                max = A[i][j];
             }
         }
         // do something with max
     }
}

, , max_element:

void findHighest(int A[][Cm], int n, int m)
{
     if (m <= 0) return;
     for (int i = 0; i < n; i++)
     {
         int max = *std::max_element(A[i], A[i] + m);
         // do something with max
     }
}

, :

#include <algorithm>
#include <iostream>

enum { Cm = 2 };

void findHighest(int A[][Cm], int n, int m) {
  if (m <= 0) return;
  for (int i = 0; i < n; i++) {
    int max = *std::max_element(A[i], A[i] + m);
    std::cout << max << " ";
  }
}

int main() {
  int A[2][2] = {{1, 2}, {3, 4}};
  findHighest(A, 2, 2);
}

2 4.

+4

++ 11, , std::minmax_element:

template<typename T, std::size_t N, std::size_t M>
void
minmax_row(T const (&arr)[N][M], T (&mincol)[N], T (&maxcol)[N]) {
  for(int i(0); i < N; ++i) {
    auto mnmx = std::minmax_element(std::begin(arr[i]), std::end(arr[i]));
    if(mnmx.first != std::end(arr[i]))  mincol[i] = *(mnmx.first);
    if(mnmx.second != std::end(arr[i])) maxcol[i] = *(mnmx.second);
  }
}

Live Demo

+3

, .

, .

, .

As others have pointed out, your function detects a global minimum / maximum, without the extrema of each line.

Move the initialization of the min / max variable inside the outer loop.

+2
source

As already mentioned, your code only shows the maximum element in the entire array. Here is the code to help you.

 void findHighest(int A[][Cm], int n, int m)
 {
    int max[n];
    max[0]=A[0][0];
    for (int i = 0; i < n; i++)
  {
      for (int j = 0; j < m; j++)
      {
         if (A[i][j] > max[i])
         {
            max[i] = A[i][j];
         }
     }
  cout << i << " row highest value " << max[i] << endl;
 }
}
+1
source

Source: https://habr.com/ru/post/1619402/