473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find the top 10 highest and lowest numbers from a text file

1 New Member
Hi guys,

I have a question, all help is truly appreciated. I have a list of numbers with decimal point saved in a text file (grade.txt) like this:

80.2
85.7
57.2
90.0
92.9
74.0
76.5
...
I would like to write a small program to display the top 10 highest and lowest grade points. I started learning C 3 weeks ago and right now I have no idea how to solve this problem. Someone please help.
Jan 3 '14 #1
3 1741
dealsoftheweek
2 New Member
just sort the array and display first 3 and last 3 elements.
Jan 3 '14 #2
Nepomuk
3,112 Recognized Expert Specialist
Hi Kevinzzz and welcome to bytes.com!

There are several steps to solving that problem. First of all, you'll have to read the values into your program. Then you should sort them as suggested by dealsoftheweek. And finally you have to output the first and last 10 entries from the sorted array/list/whatever of saved values.

Personally I'd start in the middle: Create an array/list/whatever of values in the source code for testing (you would then call them "hardcoded") and try to sort them. There are many sorting algorithms available, Wikipedia lists a few of the most common. It's probably a good idea to start with a simple one, e.g. selection sort, and then possibly replacing it later on (if you so desire).

To check that the sorting worked you can output the first and last 10 entries. Especially if you're using an array that shouldn't be very difficult.

When you have that working, check on how to read values from text files. What you will get will probably be of the type char*, so find out how to turn them into float or double values. (Or int values if you like - depends on how accurate you need the algorithm to be.) As soon as you have those values, use the sorting and extracting algorithms you created before.

Each of these steps should be represented through (at least) one method/function. That way you can put them together relatively easily.

If you get stuck at any point you're welcome to ask here of course. If so, please show us what you have written so far and where the problem lies.
Jan 3 '14 #3
donbock
2,426 Recognized Expert Top Contributor
The following questions explore some details of exactly what you wish to accomplish.
  1. Suppose the largest value occurs four times in the text file. Do you want that value to occur four times or only once in the output list?
  2. Is the number of decimal digits in the input file a fixed value that you can hard-code into your program? If so then it will be easy to output values that look exactly like the input values. Otherwise you will either have to settle for outputting some values that look different than the input value (89.50 instead of 89.5) or you have to do something a little tricky to keep track of the number of decimal digits for each input value.
  3. If the number of decimal digits can vary, then do you have any reason to distinguish between 75.4 and 75.40 (for example) when sorting or outputting the values?
  4. Are you allowed to use standard library features to sort the values, or does your assignment forbid that?
  5. Might the number of input values be so much larger than the number of output values (20) that sorting the entire input list in memory is inefficient?
  6. What should happen if there are less than 20 values in the input list? (Highest and lowest output lists overlap)
  7. What should happen if there are less than 10 values in the input list? (Can't fully populate the output lists)
Jan 4 '14 #4

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

Similar topics

6
30958
by: sea | last post by:
I have text files in the following format: 123,34, ,345,890, 123,23 .. .. .. As you can see, the problem is that (1) the commas can occur in
6
10090
by: Kifah Abbad | last post by:
hi guys, First thanks for the support in my last question posted here. Now i have been trying today to read about finding a way to solve this (simple?) problem....which is...i have a string...
0
1081
by: mfilpot | last post by:
I am working on a prime number generator for sschool, but I jsut can't seem to figure out how to write the contents of the variable MeSSage into a new text file, Can someone help me? the script...
5
3824
by: kid | last post by:
hi guys i need help, my situation: i need to find EOF in my text file and when it is not present it will display a prompt.
5
8322
by: neeludhiman | last post by:
Hi All, Can someone please help me with the code in C / C++ to find a string in an input text file and replace it with another string in output text file. The catch is that white spaces in the...
30
5211
by: =?Utf-8?B?VGhvbWFzVDIy?= | last post by:
Hello, I have an array that holds 4 values ( they contain random numbers). I would like to find the lowest value (if there is a tie i would like to find one of the tie.) then remove that value....
9
5088
by: Salve =?iso-8859-1?Q?H=E5kedal?= | last post by:
What is the best regular expression for finding urls in plain text files? (By urls I mean http://www.something.com, but also www.something.com, or salve@somewhere.com) Salve
1
1661
by: Kris tarun | last post by:
I have a table of a retail store which has almost 13000 customers. and i want to write a query for this.. Group products based on their sales patterns. Highest, lowest, and median values. Use...
0
1205
by: CrazyWebster | last post by:
I'm creating a diving competion program that 1. keeps score for up to 16 divers. 2. Each diver has 8 dives. 3. There are 8 judges scoring each dives w/scores ranging from 0-10 w/one...
3
75901
zmbd
by: zmbd | last post by:
It seems that in VBA some the most fundamentally basic commands for file control are the hardest to find any information for, for example, the basic File operation commands, such as DIR(), CHDRIVE(),...
0
7198
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
7271
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
7319
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...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.