|
CS 2073, Fall 2005 Program 6 Creating a Sorted List Week 7: Oct 10-14 Due (on time): 2005-10-17 23:59:59 Due (late): 2005-10-19 23:59:59 |
|
Program 6 must be emailed to:
nrwagner@cs.utsa.edu following directions for: running and submitting a C program, with deadlines:
|
Introduction: For this assignment, you should read non-negative integers one-at-a-time until you get to a negative integer. You can assume that this is a -1 if you want.) For this part, you can model your program after some of the code at the beginning of the link Loop array examples. This program reads integers up to -1 and stores the result in an array. Your program should do something similar, except that where the given program has s[n] = score;, you will need a loop that will put the new value into the proper place in the array (and at the same time will move some array elements into a location one bigger.)
As the numbers are read, each one should be inserted into an array in increasing or sorted order, so that when you get done you have a sorted array.
Then you should read additional numbers that are to be looked up in the array to see if each one is present or not. Enter another -1 to indicate the end of the program. If the number is present, you should give its location in the array, and if it is not present, you should just say so. A possible interactive session with this program is given towards the end of this write-up.
The lookup should be done in two different ways:
Suppose the array has n elements in it, using indices 0, 1, ..., n-2, n-1, and the elements in the array are: a[0], a[1], ..., a[n-2], a[n-1]. The idea is to process the array from the high end to the low end, that is from index n-1 down to index 0. Suppose the new element is x. In case x < a[n-1], we move element a[n-1] down one location to a[n]. We keep moving elements down one until we find an element a[i] with x > a[i]. We then insert x into location a[i] and we are done.
An illustration may help. Suppose we have already inserted 5 numbers into an array, in such a way that they end up in increasing order: suppose we inserted 47, 23, 59, 14, and 31. Suppose also we have used a variable n to count them, so that when we are done, n == 5. Now insert the element x == 38. (Below, red is for the elements moved down one, green is for the inserted element, and black are unchanged elements.
Index: 0 1 2 3 4 5 Array element: a[0] a[1] a[2] a[3] a[4] a[5] Old contents: 14 23 31 47 59 none (n == 5) New contents: 14 23 31 38 47 59 (n == 6)
So you need to write a loop that will do this. If the
loop went from 4 downto 0, we could use a for loop:
for (i = 4, i >= 0; i--) { ... }
At each stage you will check if
x < a[i]. Finally, when this is no
longer true, you insert x and break out of the loop.
This loop will be buried inside the loop that reads
the initial list of numbers.
There are annoying special cases:
int binarysearch(int a[], int newitem, int n) {
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = (low + high)/2;
if (a[mid] == newitem)
return mid;
else if (a[mid] < newitem)
low = mid + 1;
else /* a[mid] > newitem */
high = mid - 1;
}
return -1;
}
I recommend printing all on one line for this simple program, since we won't need to use large arrays.
Here is a sample run of the program. User input is in boldface. Your results don't have to look exactly like this, but the numbers should be the same.
Enter integers, followed by -1: 47 23 59 14 31 38 -1 Index i : 0 1 2 3 4 5 array a[i]: 14 23 31 38 47 59 Enter integer to look up, or -1 to stop --->14 location using linear search: 0 location using binary search: 0 Enter integer to look up, or -1 to stop --->59 location using linear search: 5 location using binary search: 5 Enter integer to look up, or -1 to stop --->31 location using linear search: 2 location using binary search: 2 Enter integer to look up, or -1 to stop --->23 location using linear search: 1 location using binary search: 1 Enter integer to look up, or -1 to stop --->40 location using linear search: -1 location using binary search: -1 Enter integer to look up, or -1 to stop --->10 location using linear search: -1 location using binary search: -1 Enter integer to look up, or -1 to stop --->75 location using linear search: -1 location using binary search: -1 Enter integer to look up, or -1 to stop --->-1
|
Contents of email submission
for Program 6: Last Name, First Name; Course Number; Program Number.
|