CSCI 116

Project #5: Biggest and Smallest(revisited)

 

 

Purpose: We are again going to find the biggest and smallest of a set of numbers, but this time the numbers will be read from a file instead of the keyboard.

 

Program objective:  Your program is to open the file data.txt, read all the numbers in it, and print out the largest and smallest of them.

 

Instructions:  Write a program that opens the file data.txt.  Read the data from the file until there are no more numbers in it.  Close the file.  Next, print out the number of numbers read, and the biggest and the smallest numbers of all the numbers read.  After printing out the results, your program should exit normally.  Here is some sample output:

 

Reading numbers from the file data.txt

Read 10000 numbers.

The smallest number read was 1 and the largest number read was 145

Press any key to continue

 

To find the largest number, let’s think about reading them in one at a time.  The first number read is the largest number seen so far, since it’s the ONLY number seen so far.  Now, every time we get a new number we can compare it to the current largest number.  If it is bigger, it becomes the new largest number—otherwise, the previous largest number is still the biggest.  We do that until we have seen the last number, and at that point the biggest number is the largest of all of them.  We do a similar process for the smallest number.  Some sample files: data1, data2, data3, dat1a.

 

That should about cover it…have fun!