CSCI 159 Quiz 3

Name:

The quiz is open notes, open book, and you are permitted to use your linux accounts to write/check practice code if desired. For example, you could use the following to edit, compile, and check code in a quest1.cpp file:
    gedit quest1.cpp &
    g++ quest1.cpp -o quest1x
    ./quest1x
There are three questions and you have 60 minutes to complete the quiz.
The quiz must be completed as a completely individual exercise: you cannot request or accept help from any other source (human or ai) other than the course instructor, and you cannot give help to anyone else for the quiz.
If you finish early you can leave or stay and work quietly while others finish their quizzes.

Question 1: functions and arrays

Function copy takes as parameters two arrays, source and dest, an their sizes, S and D (the arrays may be of different sizes).

Write the copy function so that it finds the smaller of S and D, and copies that many elements from source into dest. For example, if source is size 10 and dest is size 8 then copy the first 8 elements of source into dest.

void copy(float source[], float dest[], int S, int D)

Question 2: searching arrays

(i) Briefly describe the circumstances under which we could use binary search to search an array, any why we would prefer to use binary search where possible.
(ii) If we had to use linear search, and didn't know anything about the order of data values currently in the array, would it make more sense to search from front to back, back to front, or doesn't matter? Justify your answer.

Question 3: sorting arrays

We looked at bubblesort as one sorting algorithm, but there are many others.

(i) Implement the xSort function below, using the sorting algorithm given in the comments. You can assume a suitable swap(x,y) function has been provided.
// for each array position, p, from 0 to size-2
//     go through all array positions >= p to find which one holds the largest value,
//     then swap the contents of that position and position p
void xSort(double arr[], int size)

(ii) Does this algorithm correctly sort the values from smallest to largest? Explain why or why not.