473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to count how many times the largest number appear

8
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(){
  4.  
  5. int i,j=0;
  6. float n,s,w;
  7.  
  8. for(i=1;i<=5;i++){
  9.                   printf("Primeiro valor: ");
  10.                   scanf("%f",&n);
  11.                   if(i==1) s = n;
  12.                   if(i==1) w = n;
  13.                   if(n>s){ s=n; j++; printf("%d\n",j); }
  14.                   if(n<w) { w=n;  }
  15.                   if(n<s && n>w) { }
  16.                   }
  17.  
  18.  
  19.  
  20. printf("\n%d\n",5-j);
  21. printf("MAX: %f, MIN: %f\n\n",s,w);
  22.  
  23.  
  24. system("pause");
  25.  
  26. }
  27.  
In fact I just need the idea of how do I do that.
Oct 19 '14 #1
5 1585
weaknessforcats
9,208 Expert Mod 8TB
Maybe every time you use printf on it you could increment a counter.
Oct 19 '14 #2
Snip
8
But i will just know what is the largest after the loop, can u give me an example?
Oct 20 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Oct 20 '14 #4
donbock
2,426 Expert 2GB
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).
Oct 20 '14 #5
Frinavale
9,735 Expert Mod 8TB
I really like @weaknessforcats 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 @weaknessforcats' recommendation first because it is a lot easier to understand than using structures etc.

-Frinny
Oct 20 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the...
6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
17
by: keith | last post by:
I am trying to get a exact count of different distinct entries in an Access column. At first, I was trying to work with three columns, but I've narrowed it down to one to simplify it. I've searched...
1
by: Dixie | last post by:
I have 4 fields (byte) which can each contain a number between 1 and 40 odd.. The numbers in each of the fields represents the same information that can occur in one of 4 places. In each set of...
3
by: Advo | last post by:
I've made a small search function as of here: if (stristr($bleh,$search_for)) { if(preg_match('/<title>(*)<\/title>/i', $bleh, $matches)) { $name = str_replace("./","/",$name); echo "<li><a...
10
Thekid
by: Thekid | last post by:
I'm wondering how you'd enter a lot of numbers and have the script print out the ones that were repeated the most. Something like this: (numbers entered)...
3
by: sukatoa | last post by:
For example: Cat Dog Cow Rat Cat Cat Rat Cow
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.