Bill Fisher
Bill Fisher

Reputation: 29

C++: Problems with finding greatest value in an array

Thanks to each one of you for your help in advance.

Problem One:

I was writing a program today that should display the highest value out of 10 values that the user would input. However, whenever I test the program and input values, it seems to display more than one "greatest value". I'm thinking it may have to do with my overuse of the cout and cin functions. The program seems to go down the list of values the user inputs and selects the highest ones going down the list.

For example:

2, 4, 5, 1, 7, 8, 3, 2, 9

It would select 5, 8, and 9 as the highest values.

Problem Two:

I was wondering if there was a way to simplify the code I have written below (in reference to the cout and cin functions). Such as, making a while function and having a function like:

 while (something)
      cout << "Please input the number of pancakes person " << x << "has ate.\n"
      cin >> something // Either x or a pancakes function. I tried both.
      count++ // Sorry, I would just assume that the count function would be used in this scenario.

Problem 3:

In regards to the greatest value output in my program, could it be that I messed up something with the array?

Code:

#include <iostream>

using namespace std;

int main()
{
    int pancakes [10];
    int x;
    int largestValue = 0;

    cout << "Please input the number of pancakes person 1 has ate.\n";
    cin >> pancakes[0];
    cout << "Please input the number of pancakes person 2 has ate.\n";
    cin >> pancakes[1];
    cout << "Please input the number of pancakes person 3 has ate.\n";
    cin >> pancakes[2];
    cout << "Please input the number of pancakes person 4 has ate.\n";
    cin >> pancakes[3];
    cout << "Please input the number of pancakes person 5 has ate.\n";
    cin >> pancakes[4];
    cout << "Please input the number of pancakes person 6 has ate.\n";
    cin >> pancakes[5];
    cout << "Please input the number of pancakes person 7 has ate.\n";
    cin >> pancakes[6];
    cout << "Please input the number of pancakes person 8 has ate.\n";
    cin >> pancakes[7];
    cout << "Please input the number of pancakes person 9 has ate.\n";
    cin >> pancakes[8];
    cout << "Please input the number of pancakes person 10 has ate.\n";
    cin >> pancakes[9];

    for (int x = 0; x < 10; x++)
        if (pancakes[x] > largestValue)
    {
            largestValue = pancakes[x];
        cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
    }

    return 0;

}

Upvotes: 0

Views: 147

Answers (4)

bweber
bweber

Reputation: 4092

The problem is that you print each time a new max-value is selected in this piece of code:

for (int x = 0; x < 10; x++) {
    if (pancakes[x] > largestValue) {
        largestValue = pancakes[x];
    }
    cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
}

Instead it should be:

for (int x = 0; x < 10; x++) {
    if (pancakes[x] > largestValue) {
        largestValue = pancakes[x];
    }
}
cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";

By the way: I thik you either have to use "ate" (without "have") or "have eaten".

Regarding the other problem of not hard-coding the number of people: First of all I would use an std::vector instead of an array. Then you ask, how many people there are. Now you can create a for-loop with that many iterations. In each iteration the user is asked, how many pancakes person X ate and you append this number to the end of the vector, using std::vector::push_back. Finally you you just have to select the largest number the way you already do. Just let the upper bound of your for-loop be < vector.size().

Upvotes: 3

poko
poko

Reputation: 575

#include <iostream>

using namespace std;

int main()
{
    int pancakes [10];
    int x;
    int largestValue = 0;

    for (int x = 0; x < 10; x++) {
        cout << "Please input the number of pancakes person " << (x + 1) << " has ate.\n";
        cin >> pancakes[x];
    }

    for (int x = 0; x < 10; x++)
        if (pancakes[x] > largestValue)
            largestValue = pancakes[x];

    cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";

    return 0;
}

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92321

for (int x = 0; x < 10; x++)
    if (pancakes[x] > largestValue)
{
        largestValue = pancakes[x];
    cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
}

You display the result as soon as you find one larger than you have seen before. You should wait until you have checked all the values.

for (int x = 0; x < 10; x++)
    if (pancakes[x] > largestValue)
    {
        largestValue = pancakes[x];
    }

cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";

Upvotes: 1

J&#233;r&#244;me
J&#233;r&#244;me

Reputation: 8066

The cout statement should be outside of the loop. Your current cout is printing each time you enter the if statement (each time you find a new largestValue).

for (int x = 0; x < 10; x++) {
    if (pancakes[x] > largestValue)
    {
        largestValue = pancakes[x];
    }
}

cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";

Upvotes: 0

Related Questions