简体   繁体   English

我如何找出数组中哪个元素的最大值?

[英]How do i find out which element in an array holds the highest value?

#include <iostream>

using namespace std;

int main()
{
    int personPancake[10];
    int small, big;

    for (int c = 0; c < 10; c++)
    {
        cout << "Enter how many pancakes person " << c + 1 << " ate: ";
        cin >> personPancake[c];
    }

    big = small = personPancake[0];

    for (int c = 0; c < 10; c++)
    {
        if (personPancake[c] > big)
        {
            big = personPancake[c];
        }

        if (personPancake[c] < small)
        {
            small = personPancake[c];
        }
    }

    cout << "Biggest: " << big << endl;
    cout << "Smallest: " << small << endl << endl;
}

This is the code that i have atm, I have figured out the smallest and biggest numbers as you can see. 这是我有atm的代码,正如您所看到的,我已经找出了最小和最大的数字。 I need help finding out the index of the element which holds the biggest and the smallest value. 我需要帮助找出包含最大和最小值的元素的索引。

You could setup two other variables to hold the current smallest and biggest indexes. 您可以设置其他两个变量来保存当前的最小和最大索引。 So in your if statements... 因此,在您的if语句中...

int biggestIndex, smallestIndex;

if (personPancake[c] > big)
    {
        biggestIndex = c;
        big = personPancake[c];
    }

    if (personPancake[c] < small)
    {
        smallestIndex = c;
        small = personPancake[c];
    }

Don't store the value of personPancake[c]. 不要存储personPancake [E]的值。 Store the index of c in big or small. 将c的索引存储为大或小。

Then change your comparison to use personPancake[big or small]. 然后将您的比较更改为使用personPancake [大或小]。

try 尝试

#include <iostream>

using namespace std;

int main()
{
int personPancake[10];
int small, big;
int indexsmall=0,indexbig=0;
for (int c = 0; c < 10; c++)
{
    cout << "Enter how many pancakes person " << c + 1 << " ate: ";
    cin >> personPancake[c];
}

big = small = personPancake[0];

for (int c = 0; c < 10; c++)
{
    if (personPancake[c] > big)
    {
        big = personPancake[c];
        indexbig=c;
    }

    if (personPancake[c] < small)
    {
        small = personPancake[c];
        indexsmall=c;
    }
}

cout << "Biggest: " << big << endl;
cout << "Index Biggest: " << indexbig << endl;
cout << "Smallest: " << small << endl << endl;
cout << "Index Smallest: " << indexsmall << endl << endl;

} }

You need to store c when you find the biggest and the smallest number the same way you did for big and small 您需要存储c当你发现的最大和最小的号码,你做的一样bigsmall

int bigIdx, smallIdx;

for (int c = 0; c < 10; c++)
    {
        if (personPancake[c] > big)
        {
            big = personPancake[c];
            bigIdx = c;
        }

        if (personPancake[c] < small)
        {
            small = personPancake[c];
            smallIdx = c;
        }
    }

Add variables that hold the index 添加保存索引的变量

int main()
{
    int personPancake[10];
    int small, big, smallIndex, bigIndex;

    for (int c = 0; c < 10; c++)
    {
        cout << "Enter how many pancakes person " << c + 1 << " ate: ";
        cin >> personPancake[c];
    }

    big = small = personPancake[0];
    bigIndex = smallIndex = 0;

    for (int c = 0; c < 10; c++)
    {
        if (personPancake[c] > big)
        {
            big = personPancake[c];
            bigIndex = c;
        }

        if (personPancake[c] < small)
        {
            small = personPancake[c];
            smallIndex = c;
        }
    }

    cout << "Biggest: " << big << endl;
    cout << "Smallest: " << small << endl;
    cout << "Biggest Index: " << bigIndex << endl;
    cout << "Smallest Index: " << smallIndex << endl << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM