【问题标题】:Find the index of the largest element查找最大元素的索引
【发布时间】:2021-07-02 22:34:03
【问题描述】:

我的代码的问题是它没有识别我的函数,我不确定函数是否不正确或使用错误的语法编写。我尝试的是为最大索引的位置创建一个新数组,但它似乎不起作用。

#include <iostream>
#include <iomanip>

using namespace std;

void locateLargest(const double a[][4], int location[]);

const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;

int main(){

    int location [ROW_SIZE][COLUMN_SIZE];

    double matrix [ROW_SIZE][COLUMN_SIZE];

    double input;

    cout<<"Enter the array: "<< endl;
    
    for (int i = 0; i < ROW_SIZE; i++){
        for(int j = 0; j < COLUMN_SIZE; j++){
            cin>>input;
            matrix[i][j] =  input;
        }
    }

     for(int i = 0; i < ROW_SIZE; i++){
        for(int j = 0; j < COLUMN_SIZE; j++){
            cout<< setw(4)<<matrix[i][j]<< " ";
        }
         cout<< endl;
    }

    locateLargest(matrix, location)
}

【问题讨论】:

  • 为什么要为行+列对返回一个 3x4 矩阵?
  • 为什么要把所有元素都和a[0][0]比较?您应该将所有元素与当前最大值进行比较。 location 应该包含位置而不是值。你需要类似 `if(a[location[0]][location[1]]
  • 你不是说位置是int location[2]吗?
  • 你有一个名为locateLargest的函数。您可能想回答这些问题。您希望它返回该位置还是打印该位置?如果要返回位置,位置(您如何表示它)?

标签: c++ arrays function loops matrix


【解决方案1】:

您可以在遍历矩阵时跟踪最大值的索引。

void max_idx(const double (&arr)[RS][CS]) {
  double curr_max = arr[0][0];
  size_t max_i = 0, max_j = 0;

  for (size_t i = 0; i < RS; ++i) {
    for (size_t j = 0; j < CS; ++j) {
      if (curr_max < arr[i][j]) {
        curr_max = arr[i][j];
        max_i = i;
        max_j = j;
      }
    }
  }
  cout << "Largest value is at (i=" << max_i << ", j=" << max_j << ")\n";
}

Demo

【讨论】:

  • 二级for循环的替代方法是使用std::max_element,将其视为一个大数组,通过索引计算得到行列号
  • @prehistoricpenguin “并将其视为一个大数组”,这将是未定义的行为。为了将其视为一个大数组,它需要一个大数组。
  • @n.'pronouns'm。我记得这里未定义行为的原因是参数是从一些大型二维数组中转换的,然后内存布局不会针对不同的行继续。我认为这个问题不是这样的。
  • @prehistoricpenguin 这不是内存布局的问题。行为是未定义的,仅仅是因为标准是这样说的。它在本网站的多个帖子中进行了讨论,例如this 及其许多重复项(请务必阅读所有答案和 cmets)。
  • @n.'pronouns'm。非常感谢,我已经阅读了答案和 cmets,现在我知道这是一个常见的错误。
【解决方案2】:

首先,你必须确保你的代码是一致的:在你的locateLargest函数的原型中,location是一个一维数组但是在你的main()函数中它是一个二维的一个。

我会这样写:

#include <iostream>
#include <iomanip>

using namespace std;

void locateLargest(double** a, int* location);

const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;

int main()
{
    int location [2];
    double* matrix [ROW_SIZE];
    for(int s= 0; s< ROW_SIZE; s++)
    {
        matrix[s]= new double[COLUMN_SIZE];
    }

    double input;

    cout<<"Enter the array: "<< endl;

    for (int i = 0; i < ROW_SIZE; i++)
    {
        for(int j = 0; j < COLUMN_SIZE; j++)
        {
            cin>>input;
            matrix[i][j] =  input;
        }
    }

    for(int i = 0; i < ROW_SIZE; i++)
    {
        for(int j = 0; j < COLUMN_SIZE; j++)
        {
            cout<< setw(4)<<matrix[i][j]<< " ";
        }
         cout<< endl;
    }

    locateLargest(matrix, location);
}

void locateLargest(double** a, int* location)
{
    int i, j;
    double maxVal= a[0][0]; location[0]= location[1]= 0;

    for(i = 0;i < ROW_SIZE; i++)
    {
        for(j = 0; j < COLUMN_SIZE; j++)
        {
           if(maxVal < a[i][j])
            {
               location[0] = i;
               location[1]= j;
               maxVal= a[i][j];
            }
        }
    }
    cout << "The location of the largest element is at ("<< location[0] << " , "<<
    location[1] <<" ) . it is : "<< maxVal<<endl;
}

max 表示矩阵元素的最大值,首先将其设置为等于第一个元素,然后将其与矩阵的每个元素进行比较。每次找到大于max 的元素时,将其值分配给max,将其位置分配给location,在迭代结束时,您将获得最大值及其位置。

【讨论】:

  • 危险的误导性答案。 double**double [ROW_SIZE][COLUMN_SIZE] 不兼容。幸运的是代码不会编译。
  • 感谢您的评论,代码上还有其他错误,我编辑了答案,现在可以编译。对不起
  • 所以现在它只是不必要的复杂而不是完全错误。为什么使用new?固定数组有什么问题? int location [ROW_SIZE][COLUMN_SIZE]; 工作得很好。
  • 在这种情况下,固定数组很好。是的,使用动态分配可能没用,但对于您事先不知道数组大小的情况,我猜这可能是一个好习惯
  • 最好的做法是使用std::vector。永远不要new 任何东西。
猜你喜欢
  • 2021-06-20
  • 1970-01-01
  • 2021-11-07
  • 2018-10-17
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 2014-06-28
相关资源
最近更新 更多