You are on page 1of 3

CST 370 Spring 2017

Final Exam (Take-home projects)

Name: ___________________________________
Ashley Wallace

awalace@csumb.edu
Email: ___________________________________

Important Note:

• There are two projects, each worth 50 points, total 100 points
• This is take-home exam, open book, open notes, and open computer,
but you MUST work independently
Project #1 (50 points). Consider that a sorted list of integers (e.g., 1, 1, 2, 2, 5,
5, 5, 5, 5, 5, 5, 8, 8, 9, 10, 11, 11) provided to you.

The goal is to find the occurrences of an integer in the array using binary search.
For the above example, the occurrence of integer “5” should be 7.

a) (15 points) Design the algorithm and describe it in Pseudo code, including
necessary explanations so that the logic of your algorithm can be understood.

findCount(list[], size, num) //array, size of array, number to find


{
lower ← count(list, size, num, true) //find the first occurrence

if lower == -1 then return 0 //if first binary search returns false, 0 occurrences, stop looking

upper ← count(list, size, num, false) //find the last occurrence


return (upper – lower + 1) //return the difference between the upper and lower + 1.
}

count(list[], size, num, search) //array, size of array, number to find, upper/lower bool
{
low ← 0 //always starts at 0
high ← size – 1 //to deal with array start at 0
result ← -1 //init. To -1 so if no results will return false

while (low <= high)


{
mid ← (low + high) / 2
if(list[mid] == num)
{
result = mid // set the result to the most recent positive result
if(search) //if true, go low
high ← mid – 1 //move high down, so we search lower
else //if false, go high
low ← mid + 1 //move low up, so we search higher
}
else if (num < list[mid]) //if the number we are looking for is less than our current spot
high ← mid – 1 //search lower
else //if the number we are looking for is greater than our current spot
low ← mid + 1 //search higher
}
return result;
}

b) (20 points) Implement your algorithm in C++ and test your implementation
with a tester/driver. The sorted list is stored in an array. The test array may
be hard coded in the tester program, but the integer number to be searched
for should be input from user (on console). Special cases should be properly
handled.
c) (10 points) Give the running time of your algorithm in big O notation
(briefly explain your reasoning). If the running time of your algorithm is in
O(log n) complexity, you will be given credit for this part (10 points).
i Running time is O(log n). Starting with the count function, this is essentially recursive
going n/2 the first time, then n/4, n/8… so n/2^k. If n/2^k = 1 then n = 2^k or k = log2n,
in big o notation you ignore the 2 and just use O(log n).

The findCount function then is O(log n) + O (log n) since it runs the count function twice
or 2 * O(log n), and since in big O constants are dropped, the ending complexity is O(log
n).

d) (5 points) Make a video to give an overview of your algorithm and


implementation. Include the link to the video in your written document.
i https://www.youtube.com/watch?v=C_FXTYAOPKI&t=6s

Submission instruction: Zip your source programs and written document in a


single file named as ‘Final_Project1_yourlastname’. For the program, please
include only the source files needed to compile and run successfully.

You might also like