English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Write a program that finds the two largest and two smallest integers in a file that’s filled with numerical values. The input should be read from a file numbers.dat, and the output should be displayed to the screen as well as written to a file named stats.dat.

The format of the input is as follows:

n1 n2 n3 n4 …

The format of the output is as follow:

The file contains ??? numbers
The two largest values in the list are: … …
The two smallest values in the list are: …

2007-03-25 05:46:22 · 1 answers · asked by meme n 1 in Computers & Internet Programming & Design

1 answers

2 options: 1 read the file and count the number of inputs, allocate an array for all the inputs, read the file again into the array, sort the array from small to big take two smallest and two largest numbers (simple ,but slow).
2. create two arrays of 2 largest and smallest numbers, read the first 2 numbers and put them in the arrays (once in the largest array in acceding order and once in smallest in descending order), for every new number check if larger the one (or two) of the numbers in the largest array if so keep it and drop the smaller number, if not larger than one of them check if smaller than the numbers in the smallest array, if so keep it there and drop the larger number of them. (More complicated but will run faster).

2007-03-25 07:28:11 · answer #1 · answered by eyal b 4 · 0 0

fedest.com, questions and answers