1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++- while-loop

  1. Oct 31, 2013 #1
    C++-- while-loop

    1. The problem statement, all variables and given/known data

    calculate the 3 largest number of a sequence

    2. Relevant equations
    you ask the user to input a sequence of numbers, and enter the value 0 to stop entering numbers.
    c++

    3. The attempt at a solution
    i managed to calculate the the maximum of the series of numbers, i dont know how i can calculate the 2nd max and the 3rd max number. please help.

    cin>>value;
    while(count > 0)
    {
    cin>> input;

    if (input==0)
    {
    cout<<"sequence ended"<<endl;
    break;
    }
    else if(input > value)
    {
    input= value;
    }

    count++;

    this will generate the Maximum, how can i calculate the second max and 3rd max.
     
  2. jcsd
  3. Oct 31, 2013 #2

    Borek

    User Avatar

    Staff: Mentor

    You need to store at least three values in variables and - each time you find a new maximum - "shift" them, storing the previous second largest as a third largest, and previous largest as the second largest.

    However, at the moment your code is not doing anything - it doesn't compile, it doesn't run, it even doesn't look like a code. You don't declare your variables, you don't initialize them, and in some cases you don't need them. It makes it very hard to discuss what you wrote, as it can be wrong and can be right, depending on the pieces we don't see.
     
  4. Oct 31, 2013 #3
    I did write the whole code and declared all my variables. i just didnt want to post it fully here for some reason or the other. I apologize. here is the whole code.


    #include<iostream>
    using namespace std;
    int main()

    {
    int count;
    int num;
    int x;


    count=1;

    cout<<"please enter your sequence of numbers "<<endl;
    cin>>num;
    while(count > 0)
    {
    cin>> x;

    if(x==0)
    {
    cout<<"sequence ended"<<endl;
    break;
    }
    else if(x > num)
    {
    num = x;
    }

    count++;

    }

    cout << endl;

    cout<<"The maximum number of this sequence is "<<num<< endl;

    return 0;

    }
     
  5. Oct 31, 2013 #4

    Borek

    User Avatar

    Staff: Mentor

    Put your code in [noparse]
    Code (Text):
     
    [/noparse] tags for better formatting:

    Code (Text):
    #include<iostream>
    using namespace std;
    int main()

    {
        int count;
        int num;
        int x;
     
       
        count=1;
       
        cout<<"please enter your sequence of numbers "<<endl;
        cin>>num;
        while(count > 0)
        {
            cin>> x;
           
            if(x==0)
            {
                cout<<"sequence ended"<<endl;
                break;
            }
            else if(x > num)
            {
            num = x;
            }
           
       count++;
           
        }
           
        cout << endl;
       
    cout<<"The maximum number of this sequence is "<<num<< endl;

        return 0;
       
    }
    Do you plan to use count for anything? If not, it is superfluous. Why don't you use do{}while() loop?

    You have one variable for storing the maximum value. I already suggested one of the ways of dealing with the problem - declare and use three variables, like max1, max2 and max3 (or, to follow your 'naming scheme', num1, num2 and num3).
     
  6. Oct 31, 2013 #5

    jedishrfu

    Staff: Mentor

    it seems you're real close. Look at the line num=x

    At this point x is the largest number and num is the second largest so can you save num before you change it?

    Having figured that out it shouldn't be a problem to extend it for three numbers.
     
  7. Oct 31, 2013 #6
    thank you~~~!!!
     
  8. Oct 31, 2013 #7
    I got it and yes, there's no need why to have count there!
     
  9. Oct 31, 2013 #8

    jedishrfu

    Staff: Mentor

    dont forget to thank borek via the thanks button
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C++- while-loop
Loading...