查找数组位置的最大值

finding the maximum value of an array its location

本文关键字:最大值 位置 数组 查找      更新时间:2023-10-16
#include <iostream>
using namespace std;
int main()
{
    const int MAXNUM = 10; // this code creates 10 variables in fmax
    int fmax[MAXNUM], maximum, i, l; // initialize fmax that contains 10 variables , maximum, i for the loop, and l for storing the location
    cout << "enter 10 numbers: ";
    maximum = fmax[0]; // this sets the maximum to 0
    for(i = 0; i < MAXNUM; i++) // this is the code for finding the maximum numbers
    {
        cin >> fmax[i];
        if(fmax[i] > maximum){
            maximum = fmax[i];
            l = i;
        }
        else{
            maximum = maximum;
            l = l;
        }
    }
    cout << "the maximum number: " << maximum << endl; // outputs the results
    cout << "the location of the number: " << l << endl;
    return 0;
}

我在这个练习中遇到问题问题是程序的输出。 它不显示最大数量及其所在位置它总是这样显示

最大数量为:1987579782

号码的位置是:26355764

我需要显示输入的最大数量及其下标,我不知道如何我的代码有问题

这是运动问题

a.编写、编译并运行一个 C++ 程序,将 10 个整数输入到名为 fmax 的数组中,并确定输入的最大值 您的程序应仅包含一个循环,最大值应在输入数组元素值时确定(提示。 将最大值设置为等于第一个数组元素,该元素应在用于输入剩余数组值的循环之前输入。 并跟踪数组和最大值的索引号。

您需要先

初始化 fmax[MAXNUM] 数组,然后再使用它。简单地声明一个变量并不能保证它将是 0。目前,fmax[0]的值可以是int范围内的任何值,因为您尚未将其初始化为值。

您还需要将l变量初始化为 0(这就是您获得错误位置的原因(

试试这个:

#include <iostream>
using namespace std;
int main()
{
 const int MAXNUM = 10; // this code creates 10 variables in fmax
int fmax[MAXNUM], maxinum, i, l = 0; // initialize fmax that contains 10 variables , maxinum, i for the loop, and l for storing the location
for(i = 0; i < MAXNUM; ++i)
{
    fmax[i] = 0;
}
cout << "enter 10 numbers: ";
maxinum = fmax[0]; // this sets the maxinum to 0
for(i = 0; i < MAXNUM; i++) // this is the code for finding the maxinum numbers
{
    cin >> fmax[i];
    if(fmax[i] > maxinum){
        maxinum = fmax[i];
        l = i;
    }
    else{
        maxinum = maxinum;
        l = l;
    }
}
cout << "the maxinum number: " << maxinum << endl; // outputs the results
cout << "the location of the number: " << l << endl;
return 0;
}

编辑:由于要求代码只包含一个循环,因此您可能希望更改执行此操作的方式。我上面提供的示例也不是一种非常优雅的方法。这就是我的做法(不使用任何可能令人困惑的标准库函数C++(

#include <iostream>
int main()
{
    std::cout << "Enter 10 numbers: ";
    const int MAXNUM = 10;
    int fmax[MAXNUM] = {0}; // this is an easy way to initialize the array elements to zero
    int maxNum = 0, location;
    for(int i = 0; i < MAXNUM; i++) {
        std::cin >> fmax[i];
        if(fmax[i] > maxNum) {
            maxNum = fmax[i];
            location = i;
        }
    }
    std::cout << "The maximum number is " << maxNum << std::endl;
    std::cout << "The location of the number is " << location << std::endl;
    return 0;
}

确保您了解为什么这有效 - 如果您有任何问题,请告诉我。

您正在使用垃圾值初始化maximum

maxinum = fmax[0]; // this sets the maxinum to 0

由于您尚未输入任何元素。

我建议您使用库中的内置函数std::max_element algorithm:它将返回指向max元素的指针,因此,您可以输出max元素的位置及其值:

#include <algorithm>
// Your code
// You should enter the whole array
auto max_element = std::max_element(std::begin(fmax), std::end(fmax));
std::cout << "Position: " << (max_element - std::begin(fmax)) << std::end;
std::cout << "Value: " << *max_element << std::endl;

局部变量(非类(在 C++ 中默认不初始化为零。您的问题是您使用 fmax[0] 的值初始化maxinum,这在开始时是垃圾。如果您从不输入任何更大的数字,则I的值永远不会更改,也是垃圾。您需要将这些变量显式初始化为零:

int fmax[MAXNUM] = { 0 };
into maxinum = 0, I = 0

maxinum = fmax[0]; // this sets the maxinum to 0

这不会将maxinum设置为 0,因为您尚未将fmax[0]设置为 0

您可以这样做:

#include <iostream>
using namespace std;
int main()
{
    const int MAXNUM = 10; // this code creates 10 variables in fmax
    int fmax[MAXNUM], maxinum, i, l; // initialize fmax that contains 10 variables , maxinum, i for the loop, and l for storing the location
    cout << "enter 10 numbers: ";
    for(i = 0; i < MAXNUM; i++) // this is the code for finding the maxinum numbers
    {
        cin >> fmax[i];
        if(i==0)
            maxinum = fmax[i];  //....... this will do what you are trying to achieve
        if(fmax[i] > maxinum){
            maxinum = fmax[i];
            l = i;
        }                      // the else block you wrote is not necessary :)
    }
    cout << "the maxinum number: " << maxinum << endl; // outputs the results
    cout << "the location of the number: " << l << endl;
    return 0;
}