## Re:need help with Sorting and files

On 15 May 2002 01:30:19 -0700, messwiththebest2...@hotmail.com

##### Quote

(Alistair) wrote:

>I have been asked to design and implement a program to sort some

>numerical data, the data is real numbers of the form 63x.xx i.e all

>numbers are under 639 and have two decimal places.

>In each set of data to be sorted there may be up to 35 numbers but

>never less than ten.

>The numbers to be sorted will arrive in the form of a ‘file of

>real’.

>The output from your program will be displayed on the screen as well

>as saved to a new file.

>To test your program you will need to create a file of numbers that

>conforms to the input requirements of the program.

>The random number function returns an integer value between 0 and 1

>less than the parameter passed to it e.g.

>Random(999) >>>>> 0 to 998

>To convert this number to a real, you may divide it by (say) 100, this

>would give a real value between 0.00 and 9.98

Generating and sorting random numbers with Turbo Pascal v6.0.

By the way, numbers of the form 63x.xx includes 639.99 which

is NOT smaller than 639.

To construct your test program, do it in small steps. I suggest

you first learn to generate random integers in the range of 0 to 99.

After you have learned that, just divide that by 100 and get 0.00 to

0.99. That gives you the decimal fraction for the random real number.

Now generate the integer to go with it, in your case that would be 0

to 639. Add the two together and store in an array of real.

Display the array as Writeln(realnum:3:2).

There are many ways to sort the array but, with so few numbers, speed

is not a factor. Just use a way you understand. The following finds

the smallest number in the remainder of the array each iteration. The

sort is in ascending order:

For j := 1 to {length of array - 1} Do

Begin

smallest := a[j]; {where "a" is an array of real}

For k := j + 1 to {length of array} Do

Begin

if a[k] < smallest then smallest := a[k];

End;

{If smallest <> a[j] then swap positions of these 2 array

elements.}

End;

I leave the disk writing and reading to you.