How to count how many times the largest number appear

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Snip
    New Member
    • Oct 2014
    • 8

    How to count how many times the largest number appear

    The user input some numbers and i have to count how many times the largest number that he puts appears. I know how to get the largest, but how to count how many times it appear?

    im a beginner in programminng.

    When I was trying I did this code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
        
    int i,j=0;
    float n,s,w;
    
    for(i=1;i<=5;i++){
                      printf("Primeiro valor: ");
                      scanf("%f",&n);
                      if(i==1) s = n;
                      if(i==1) w = n;
                      if(n>s){ s=n; j++; printf("%d\n",j); }
                      if(n<w) { w=n;  }
                      if(n<s && n>w) { }
                      }
          
    
    
    printf("\n%d\n",5-j);
    printf("MAX: %f, MIN: %f\n\n",s,w);
    
                      
    system("pause");
    
    }
    In fact I just need the idea of how do I do that.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Maybe every time you use printf on it you could increment a counter.

    Comment

    • Snip
      New Member
      • Oct 2014
      • 8

      #3
      But i will just know what is the largest after the loop, can u give me an example?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You have s as your largest number. When you define s, set a to zero.

        Inside the loop after the scanf compare n to s. If n>a, then n is your new largest number to set s to n.

        You don't need to know which iteration of the loop your are on because every time the user enters a number and n <s you set s to the n.

        Unfortunately, in this code you only see the value the user currently entered. The previous values are lost as the variable n is reused. Further, you don't know which value is the largest until all the values have been entered.

        You might consider have the user enter the values and have your loop store the values in an array. After the loop ends, the array has all the values entered and you can now process the array. Usng a loop like to have already, you can find the largest number in the array. Once you know that, you can process the array in another loop to see how many array elements ae equal to the largest number.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Line 13: here you check if the latest number is the new maximum. Suppose the new number is not a new maximum, but instead is another copy of the old maximum. If that is the case, then increment a counter. Should you find a new maximum, then you want to reset that counter (because you no longer care how many times you saw the old maximum value).

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I really like @weaknessforcat s response.

            If you store what the user enters into an array you can keep track of how many times a user enters something by checking the value at the index of the number they enter.

            However, it would be really sweet if you took it one step further and implemented a Hash Table that stored the number entered as a Key and the number of times it was entered as the Value!

            Try @weaknessforcat s' recommendation first because it is a lot easier to understand than using structures etc.

            -Frinny
            Last edited by Frinavale; Oct 20 '14, 03:53 PM.

            Comment

            Working...