C++ 查找二维数组C++中每一行的最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34210401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:21:07  来源:igfitidea点击:

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

c++multidimensional-arraymax

提问by feco

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 ?

我找不到第二个函数有什么问题,为什么它只显示它找到的第一个最大值。有什么帮助吗?

回答by vitaut

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

这两个函数都返回整个数组而不是每一行的结果(最大值或最小值),因为您设置了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
     }
}

or, even better, use the standard library function max_element:

或者,更好的是,使用标准库函数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 4

回答by 101010

If your compiler supports C++11, for concrete arrays you could use the following alternative, that's based on std::minmax_element:

如果您的编译器支持 C++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

现场演示

回答by Yves Daoust

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.

将最小/最大变量的初始化移动到外循环内。

回答by The_Novice

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

回答by user11358360

{

{

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;

}

}