Calculate 3 Largest Numbers in C++ with a while-loop

  • Comp Sci
  • Thread starter Litcyb
  • Start date
  • Tags
    C++ Numbers
In summary, the conversation discusses how to calculate the 3 largest numbers of a sequence in C++. The suggested solution is to use three variables and "shift" them to store the previous largest numbers. The conversation also mentions some issues with the provided code, such as the use of a superfluous variable and not using a do-while loop.
  • #1
Litcyb
36
0
C++-- while-loop

Homework Statement



calculate the 3 largest number of a sequence

Homework Equations


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

The Attempt at a Solution


i managed to calculate the the maximum of the series of numbers, i don't 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.
 
Physics news on Phys.org
  • #2
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.
 
  • #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;

}
 
  • #4
Put your code in [noparse]
Code:
[/noparse] tags for better formatting:

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

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).
 
  • Like
Likes 1 person
  • #5
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.
 
  • Like
Likes 1 person
  • #6
thank you~~~!
 
  • #7
I got it and yes, there's no need why to have count there!
 
  • #8
dont forget to thank borek via the thanks button
 

1. How can I use a while-loop in C++ to calculate the three largest numbers?

In order to calculate the three largest numbers in C++ using a while-loop, you will need to declare three variables to store the largest numbers. Then, use a while-loop to iterate through the list of numbers and compare each number to the three variables. If a number is larger than any of the variables, it will replace the previous value. Once the loop is complete, the three variables will contain the three largest numbers.

2. What is the syntax for a while-loop in C++?

The syntax for a while-loop in C++ is as follows:

while (condition) {  // code to be executed}

The code inside the while-loop will continue to execute as long as the condition is true. Once the condition becomes false, the loop will stop.

3. Can I use a while-loop to calculate the three largest numbers in a list of any size?

Yes, a while-loop can be used to calculate the three largest numbers in a list of any size. The code inside the loop will continue to execute until the condition is no longer met, regardless of the size of the list.

4. Is it possible to use a while-loop to calculate the three largest numbers in a list of numbers that are not in order?

Yes, a while-loop can be used to calculate the three largest numbers in a list of numbers that are not in order. The loop will compare each number to the three variables and update them accordingly, regardless of their initial order in the list.

5. Are there any advantages to using a while-loop over other types of loops for this calculation?

While there are no specific advantages to using a while-loop over other types of loops for this calculation, it is a simple and straightforward way to iterate through a list and compare values. Depending on the specific scenario, other types of loops such as for-loops or do-while loops may also be suitable for this calculation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
505
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top