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

落爺英雄遲暮 提交于 2019-12-21 05:35:22

问题


I've managed to find the minimum value of every row of my 2D array with 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's lowest value " << min << endl;
    }
}

I'am trying to find the maximum value of every row using the same way,but it only shows me 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's highest value " << max << endl;
     }
}

I can't find what's wrong with the second function and why is it only showing me the first maximum value it finds. Any help ?


回答1:


Both functions return the result (maximum or minimum) for the whole array rather than each row, because you set max once rather than once per row. You can get the result for each row as follows:

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
     }
}

or, even better, use the standard library function 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
     }
}

This should give you all values which is easy to check:

#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);
}

prints 2 4.




回答2:


If your compiler supports C++11, for concrete arrays you could use the following alternative, that's based on 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:


Your test data is guilty for not clearly showing you the defect.

The row minima occur in decreasing values, so that they get updated on every row.

And the row maxima also occur in decreasing values, so that the first one keeps winning.

As others pointed, your function finds the global minimum/maximum, no the per-row extrema.

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




回答4:


As mentioned your code only shows the maximum element in the whole array. Here is the code which will 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's highest value " << max[i] << endl;
 }
}



回答5:


{

int i,j;

int arr[4][2]={(1,2),(3,4),(5,6),(7,8)};
int max;
max=arr[0][0];

for( int i=0; i<4; i++)
{
    for(int j=0; j<2; j++)
    {
        if(max<arr[i][j])
        {
            max=arr[i][j];

        }

    }

}

int min;
min=arr[0][0];

for(int i=0; i<4; i++)
{
    for(int j=0; j<2; j++)
    {
        if(min>arr[i][j])
        {
            min=arr[i][j];
        }
    }
}

cout<<"maximum number is:"<<max;
cout<<endl;
cout<<"Minimum Number is:"<<min;

}



来源:https://stackoverflow.com/questions/34210401/finding-the-maximum-value-of-every-row-in-2d-array-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!