Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello Everyone,
I am stuck at finding the 100th least and 100th highest number from a text file containing 1000 random numbers. So please could anyone help me out with this issue.

Any kind of help is highly appreciated.

Regards
Abhishek
Posted

Read the file as String
Split with NewLine
Convert this string array to integer array
Sort the integer array
Items indexed 99 and intArray.Length - 100 will be your result.
 
Share this answer
 
Comments
Sufi Saint 19-Jul-11 5:44am    
Thanx a lot Prerak......
Prerak Patel 19-Jul-11 6:22am    
You are welcome.

  1. Open file and read line by line assuming that the each number is on a separate line.
  2. Convert the textual representation to a number. The number format to choose depends on you file content (long, int, float or double).
  3. After successful conversion add the number to a list of the corresponding type: e.g. List<double>
  4. Continue reading lines from your file until there are no more left
  5. Sort the List<double> numbers via numbers.Sort(). Since numbers already implement the IComparable interface the list will be sorted in ascending order.
  6. 100th least element will be at numbers[99] and the 100th highest element will be at numbers[numbers.Count - 99].
  7. Done!


Since the task was to find the least and highest number you'd also need to make sure that you only add a number to the list if it Contains it not. Depending on the content of your file this may very well leave you with less than a hundred numbers. In that case there is no solution to the given question. To also avoid the paradoxon that the nth highest may be smaller than the nth least you'd even have to check that there are at least 2n elements in the list. In your case the list should thus be at least 200 elements long. In case the paradox doesn't really matter you should still output a warning with your result.

Best Regards,

—MRB
 
Share this answer
 
Comments
Sufi Saint 21-Jul-11 6:55am    
Thanx a lot Mr.MRB. It helped me a lot
Manfred Rudolf Bihy 21-Jul-11 6:57am    
You're welcome!
0) Load the file.

1) Split the string on the delimiter (a space, comma, whatever) into an array of strings.

2) Loop through the array to find the highest value.
 
Share this answer
 
Create a DataTable object with an integer DataRow.
Add all the numbers to the DataTable.

Create a DataView object and add the table to the dataview. Then DataView.Sort("ColumnName") and you have al the values from low to high. Then you can find your 100 lowest and 100 highest numbers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900