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:
  • 2005-10-17  23:59:59 (that's Monday, 17 October 2005, 11:59:59 pm) for full credit.
  • 2005-10-19  23:59:59 (that's Wednesday, 19 October 2005, 11:59:59 pm) for 75% credit.

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:


Details about insertion. Each time you read a number initially, you should insert it into the array. The trick is to insert into the proper place so that the array is always in in sorted order. This usually requires moving other array elements down one location.

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.

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:


Details about sequential search. In sequential search (also called linear search), you are looking at each array element to see if it is equal to some fixed element x. The algorithm is just a simple loop with i from 0 to n-1 inclusive. If you find that a[i] == x for some i, just return that value of i, and otherwise return -1, to indicate that the search was no successful.


Details about binary search. Another search algorithm is called binary search because it halves the items to search at each iteration. We will discuss binary search in class. Getting the code correct is really tricky, so I am giving you the code here. You should make use of this code as a separate function.


Details about printing the array. In order to print the array you should use a separate function similar to the binarysearch function above. The prototype could be:
    int printarray(int a[], iint n);.

I recommend printing all on one line for this simple program, since we won't need to use large arrays.


Summary of program.
  1. Read non-negative integers up to a -1.
  2. Insert these into an array as you read them so that they are always in increasing order.
  3. Print the numbers in their increasing (or sorted) order.
  4. Read non-negative numbers to look up up to a -1.
  5. Lookup each number using sequential search, reporting the result.
  6. Lookup the same number again using binary search, reporting the result.
  7. Keep looking up until you get to another -1.

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.


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 6:

Last Name, First Name; Course Number; Program Number.

  1. The C source for your program.
  2. A first run using the sample data above.
  3. A second run using data of your choosing.


Revision date: 2005-10-09. (Please use ISO 8601, the International Standard.)