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

  • Context: Comp Sci 
  • Thread starter Thread starter Litcyb
  • Start date Start date
  • Tags Tags
    C++ Numbers
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming task where participants are trying to calculate the three largest numbers from a user-input sequence, using a while-loop. The focus includes coding techniques, variable management, and logic for tracking multiple maximum values.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to find the maximum number but struggles with how to also find the second and third largest numbers.
  • Another participant suggests storing at least three values and shifting them when a new maximum is found, but criticizes the initial code for not compiling and lacking clarity.
  • A participant later provides the full code, indicating that they had previously omitted parts for brevity, and acknowledges the need for variable declarations.
  • Formatting suggestions are made to improve code readability, along with a recommendation to consider using a do-while loop instead of a while loop.
  • Another participant points out that the variable 'num' should be saved before it is overwritten when a new maximum is found, hinting at a solution for tracking multiple maximums.
  • Several participants express agreement on the redundancy of the 'count' variable, suggesting it may not be necessary.

Areas of Agreement / Disagreement

Participants generally agree on the need to track multiple maximum values and the redundancy of the 'count' variable. However, there is no consensus on the best approach to implement the solution, as various coding strategies are proposed and discussed.

Contextual Notes

Some limitations in the initial code are noted, including missing variable declarations and unclear logic, which may affect the ability to compile and run the code correctly.

Who May Find This Useful

Readers interested in C++ programming, particularly those working on similar homework assignments involving loops and variable management, may find this discussion beneficial.

Litcyb
Messages
34
Reaction score
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)
{
count<<"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
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.
 
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;

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

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

count++;

}

count << endl;

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

return 0;

}
 
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   Reactions: 1 person
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   Reactions: 1 person
thank you~~~!
 
I got it and yes, there's no need why to have count there!
 
dont forget to thank borek via the thanks button
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K