Comp 1572, Winter 2002
Pencil-and-Paper Assignment 3
Assigned Monday, January 28, 2002
Due Monday, February 4, 2002 (At the beginning of class)

============================================================================
Solutions
============================================================================

1.  // sort the array a in increasing order
    void bubble_sort(int a[], const int len)
    {
        int i, j, temp;
        bool already_sorted;

        for (i=0; i < len-1; i++) {
            already_sorted = true; // start off assuming we're done sorting
            for (j = 0;j < len - i - 1; j++) {
                if (a[j] > a[j+1]) {
                    already_sorted = false; // it wasn't sorted after all
                    temp = a[j+1];
                    a[j+1] = a[j];
                    a[j] = temp;
                }
            }
            if (already_sorted)
                return; // we made it through a pass
                        // without swapping - it's sorted
        }
    }

2.  // sort the array a in increasing order
    // Make n passes through the list.  In each pass, compare adjacent pairs
    // all the way along the array, switching if they're out of orders
    void bubble_sort(vector  &a)
    {
        int i, j, temp;

        for (i=0; i < a.size(); i++) {
            for (j = 0; j < a.size() - i - 1; j++) {
                if (a.at(j) > a.at(j+1)) {
                    temp = a.at(j+1);
                    a.at(j+1) = a.at(j);
                    a.at(j) = temp;
                }
            }
        }
     }

3.  //Search through array b between element index low, and index high
    // to locate the index of searchkey, a value we hope to find in b.  If it is
    // not found, return -1
    int binarySearch(const int b[], int searchkey, int low, int high)
    {
        int middle;

        if (low > high)
            return(-1);
        else {
            middle = (low+high)/2;
            if (searchkey == b[middle])      // found it!
                return middle;
            else if (searchkey < b[middle])  // it's in the lower half
                high = middle -1;
            else
                low = middle + 1;
            return(binarySearch(b, searchkey, low, high));
        }
    }

4.  Consider a 3-by-4 integer array t
    a) int t[3][4];
    b) 12
    c) t[1][0], t[1][1], t[1][2], t[1][3];
    d) t[0][1] = 0;
    e) for (int i=0; i<3; i++)
           for (int j=0, j< 4; j++)
               t[i][j] = 0;
    f) int sum=0;
       for (int j = 0; j < 4; j++)
           sum += t[i][j];
    g) the programmer probably meant t[1][2] = 47;
5.
12345
678910
1112131415
6.  #include 
    using namespace std;

    int main()
    {
        const int SIZE=20;
        int a[SIZE] = {0};  // initialize all to 0
        bool duplicate;     // keep track of whether this one is a dup.
        int new_index = 0;  // keep track of which new element we're on
        int input_number;     // most recently input number
        int i, j;


        cout << "Enter 20 integers between 10 and 100: \n";
        for (i=0; i < SIZE; i++) {
            cin >> input_number; // Trust them to stay within the bounds
            duplicate = false;  // begin by assuming it's a new number
	    j = 0;
            while (j < new_index && duplicate == false) {
                // do a linear search for it, and stop if you find it
                if (input_number == a[j])   // oh, it's not new after all!
                    duplicate = true;
                j++;
            }
            if (!duplicate) {
                a[new_index++] = input_number;
                cout << "Found a new one: " << input_number << endl;
            }
        }
        // print them all out:
        for (i = 0; i < new_index; i++)
            cout << a[i] << ' ';
        cout << endl;
        return  0;
    }

7.  a) unsigned int values[SIZE] = {2, 4, 6, 8, 10};
    b) unsigned int *vPtr;
    c) for (i=0; i < SIZE; i++)
           cout << values[i] << ' ';
    d) vPtr = values;
       -or-
       vPtr = &values[0];
    e) for (i=0; i < SIZE; i++)
           cout << *(vPtr+i) << ' ';
    f) for (i=0; i < SIZE; i++)
           cout << *(values+i) << ' ';
    g) for (i=0; i < SIZE; i++)
           cout << vPtr[i] << ' ';
    h) values[4], *(values+4), vPtr[4], *(vtr+4);
    i) Memory location 1002506 holds the values 8
    j) if vPtr points to values[4], then it holds the address 1002508, and
       vPtr -= 4 refers to memory location 1002500 which holds the value 2.

8.  I understand the algorithm 100%.

9.  The solutions are on page 790.