CS107 - Lab 8
C++ Searching and Sorting

First part: Due Monday 04/05
Second part: Due Friday 04/09

Overview:

In this assignment you will implement in C++ the searching algorithms that you learnt in the first part of the semester. Using these as examples this assignment introduces the technique of abstraction in problem solving using the idea of a function as a medium. The readings and exercises for this assignment are taken from section 7.1-7.7 of your text.


Searching

Drag a copy of csci107 - Invitation (OSX) to the desktop, and then close the csci107 folder. The button C++ Compiler is used for this assignment.

1. Write a C++ program that reads from the user a list of numbers ended by the value 0, stores them in an array and outputs the largest value and its location.

2. Write a C+ program that reads from the user a list of numbers ended by the value 0 and stores them in an array. Then it asks the user for a target value, searches for the target in the array using sequential search, and outputs whether it is found or not and the location at which it is found. Then it asks the user whether it wants to repeat; when the user presses 'n' (for NO) the program should terminate.

3. Same as above, but use binary search to search for the target value. Assume that the user inputs the values in increasing order.

Function abstraction

Open example8.cpp from the Invitation -> EXAMPLES folder on your desktop. Notice here that the program has two parts, a main program and a function definition named swap.

When the main program begins to run, it first reads a number from the user, and then it outputs square (number); This is a function call statement. That is, the main program is calling upon the function square to perform a service for it - namely to compute the square of a variable and return it.

Now look at the function definition square, which begins with the line void square(int n) { and ends with the single right brace }. This definition has statements that are executed only when the function is called, and not before. The parameter n is a placeholder for the argument that will be provided by the function call (in this case, number). When the function's statements are executed, the result is assigned to the argument n (rather than the parameter number).

4. Re-write your programs for find largest, sequential search and binary search using functions. Write the following functions:

//function: reads values from the user ended by a 0, stores them into array a[],
//and sets n to be the number of values read up to and excluding 0
void getArray(int a[], int&n); 

//input: an array a[] storing n numbers
//function: prints the n values stored in array a[]
void printArray(int a[], int n);

//input: an array a[]  storing n numbers
//function: finds the largest number in a[] 
//return value:  the index of the position of teh largest elemennt in a[] 
int findLargest(int a[], int n);


//input: an array a[]  storing n numbers
//function: searches for target in the array a[] using sequential search
//return value: the index of the position where target is found; -1 if not found 
int seqSearch(int a[], int n, int target);

//input: an array a[]  storing n numbers in increasing order; 
//function: searches for target in the array a[] using binary search
//return value: the index of the position where target is found; -1 if not found 
int binSearch(int a[], int n, int target);

Write all of the above functions into a single .cpp program. The main function should read an array, find the largest number, then repeatedly ask for a target and search the array using sequential search; when the user is bored with sequential search and exits the loop, the program should then ask the user for another array, this time in sorted order, and repeatedly ask for a target and search for it using binary search.

void main() {

int a[100], n;
char again = 'y'; 

//read array using readArray(..) function 
//call FindLargest(...) to find location of  largest element
// print largest element 


//search using sequential search
while (again == 'y') {
         //ask for target
	 //search for target using sequential sort function
	 //print whether target was found or not and the location
	 //ask if again
}

//get a new array in sorted order and search using binay search
//read array   using readArray(..) function 
again = 'y';
while (again == 'y') {
         //ask for target
	 //search for target using binary sort function
	 //print whether target was found or not and the location
	 //ask if again
}

//done
}

The program should be appropriately indented and include comments for each function. The first line in your program should contain the names of the authors.

Submit your completed program to the csci107 -> Drop Box.



To submit a file electronically, you should first rename it so that you are identified as the author (e.g., give it a name like lab-x-ltoma). Then drag the file to the csci107 -> Drop Box folder. Be careful not to drag an entire folder into the Drop Box; only a single file at a time can be submitted.

Once you are finished in the lab, be sure to drag the CS107 icon to the Trash - this step disconnects you from the server and prevents someone else (who may use this iMac later in the day) from accidentally accessing files in your personal folder.