Need a hint

I have been working on the following problem the entire weekend:

Write a program that repeatedly asks for positive integers (values greater than or equal to 1). The user
may type 0 to quit the program. The user will enter at least one value before typing 0 to quit. After the
user types 0 to quit, your program should report how often the largest number was entered.

My teacher said that he wrote this program using only a while loop and two if statements, I understand how to use the while loop. However I am not sure how to display the largest value of all the numbers or print how often that number was entered. I also understand that all the numbers entered must be stored in order to compare them. I would greatly appreciate help or some hints on this.

Thank you
Create two variables, one to store the current max value and one to store the number of times it's entered ( initialize both to 0 )
When a number is entered use those variables to keep track of the max value
I made two variables, however I am having trouble making an if statement that will use a variable to keep track of the max value.

pseudo code:

1
2
3
4
5
6
7
8
9
int maxValue = 0;

while( user does not enter 0 )
{
  if value user just entered is greater than maxValue 
    then set maxValue to that value
}

print out maxValue
I have everything right except that I cannot seem to write an if statement to act as a counter for the number of times that the largest value occurs
1
2
3
4
5
6
7
8
9
10
11
12
int MaxValue = 0
int MaxCount = 0


while(etc..)
{
    if...etc
   
    if( number == maxValue)
        MaxCount++
}


Is that what you need?
Last edited on
I tried that, it did not work for all cases of numbers I was testing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main()
{
    int maxValue=0;
    int r=0;
    int n;
    cout<<"Enter a positive integer (0 to quit) :";
    cin>>n;
    while(n !=0)
    {    
        if(n>maxValue)
        {
            maxValue=n;
        }
         cout<<"Enter a positive integer (0 to quit) :";
        cin>>n;
        cout<<"Got it."<<endl;
        
        if(n == maxValue)
        {
           r++;
        }
       
    }
    cout<<"Done."<<endl;
    cout<<"Largest input:"<<maxValue<<"."<<endl;
    cout<<"Number of "<<maxValue<<"'s:"<<r<<endl;



This is what I have
The logic here has 2 steps:

1) If the user inputs a number equal to the maximum, then you count it

2) If the user inputs a number higher than the maximum, that number is your new maximum, and you start the count over.

What Disch said, you've got to reset the value of r to 0 when n>maxValue.
Topic archived. No new replies allowed.