简体   繁体   中英

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

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]. Store the index of c in big or small.

Then change your comparison to use personPancake[big or small].

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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