|
|
CS 2073, Spring 2006
Program 7
Arrays: Sorting and Randomizing
Week 8: Mar 6 - 10
Due (on time):
2006-03-24 23:59:59
Due (late):
2006-03-27 23:59:59
|
Program 7 must be emailed to:
nrwagner@cs.utsa.edu
following directions for: running
and submitting a C program, with deadlines:
- 2006-03-24 23:59:59 (that's Friday, 24 March 2006, 11:59:59 pm)
for full credit.
- 2006-03-27 23:59:59 (that's Monday, 27 March 2006, 11:59:59 pm)
for 75% credit.
|
Introduction:
For this assignment you are to write a program
that will do the following:
- read a sequence of non-negative integers, up to a -1,
- store the integers into an array,
- sort the integers in the array into increasing order, and
- randomize (that is, shuffle) the order of the integers in the array.
I will supply code for many of these steps,
and you have to stitch the code together.
Details: Here are details
of each of the items above:
- read integers, up to a -1:
This was the subject of the page which I called the
Loop patch example.
You should use one of the three "good" methods presented
at the end of the write-up.
- store the integers into an array:
The first example on the following page shows how to do
this: Loops and Arrays
- sort into increasing order:
Sorting with Bubblesort
- randomize (that is, shuffle) the integers:
In order to do this, create another array r of
doubles, in parallel to the array a of
ints. Put a random double
between 0 and 1 into each
of r's locations. Now here is the tricky
part: you are to use bubblesort to sort the array
r into increasing order. Bubblesort does
this by repeatedly interchanging adjacent elements that are
out of order. Whenever you do an interchange of elements
in the r array, do the same interchange
of the same two elements in the "shadow" a array.
As r is transformed into order,
a will be transformed into disorder.
When you are done, the a array will be
randomized.
Some example programs using random numbers are in the middle
of the list at:
example programs.
More Details:
Remember that you are supposed to read in the
integers that you process. These should all be non-negative,
and the end-of-input can be marked with a negative integer.
(You can input these numbers by just using copy and paste,
along with the "magic" little icon at the upper left corner
of the black box.)
Initially use the integers in the bubblesort example:
78 43 12 65 81 26 54 39 -1
You should:
- Print the original array a .
- Sort a into increasing order, using bubble sort.
- Print the sorted array a .
- Create an array r with the same number
of random doubles in it as integers in a .
- Print the array r (not all on one line).
- Randomize the array a, by sorting
r and making the same interchanges to
a .
- Print the array r, now in order (not all on one line).
- Print the randomized array a .
Next, exactly the same program should process the following different
list of numbers:
822 347 222 724 671 969 444 526 123 392 600 -1
Notice that the defined constant N in the programs
is the maximum size of the arrays, and should be bigger than
any array you intend to use.
What you should email:
Refer to the submissions directions and to
deadlines at the top of this page. The text file that you submit
should first have Your Name, the Course Number,
and the Program Number. The rest of the file
should have the following in it, in the order below, and clearly labeled,
including at the beginning the appropriate item letters:
a, b, c, etc.
|
Contents of email submission
for Program 7:
Last Name, First Name; Course Number; Program Number.
Do two separate runs of the same program, using the
two inputs above for the two runs.
- For the first set of input data print the
numbers in the array a three times and
the
array r twice, as described above.
- Do the same for the second set of input data.
|
Revision date: 2006-03-22.
(Please use ISO
8601, the International Standard.)