Skip to content Skip to sidebar Skip to footer

Advice With A File Program Asking For The Largest Number

the assignment this time around deals with using files. 'Assume that a file containing a series of integers is named numbers.dat and exists on the computer's disk. Design a program

Solution 1:

Imagine you have a sheet of paper with hundreds of numbers on it. Using nothing but your brain and eyes, read those numbers and find the largest one.

How would you do this?

Now, how would you tell the computer to do it the same way?

Solution 2:

use a variable to store the current number and assume that it is largest. As you go through the file compare the stored number to current number from file, if number from file is greater store it in the variable else keep on reading the file. Repeat this until you reach end of the file.

 largest = 0;
 count = 0while ((num =input.readline()) != EOF) {
      count++;
      if (largest < num) {
           largest = num;
      }
   }

Post a Comment for "Advice With A File Program Asking For The Largest Number"