1
h13
CS16 S17
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone")

h13: Homework 13: Array Search and Sort; Vectors

ready? assigned due points
true Thu 05/25 12:30PM Tue 05/30 12:30PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE. There is NO MAKEUP for missed assignments.
In place of that, we drop the two lowest scores (if you have zeros, those are the two lowest scores.)


PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!

You may print out your answers on a separate sheet(s) and attach them to this page when you turn in your homework.

1.(20 pts) Write a C++ program that creates a 2-dimensional array of 10x5 elements that are populated by random numbers between 0 and 49 (hint: use the rand() function and re-use code from the multi-dimensional array demonstration in class). The program should then ask the user for a number between 0 and 49 and searches the array for that number. If it finds it, it returns both indicies of the 2-D array, otherwise, it returns a message that says “I did not find this number.” Re-run your program multiple times to make sure that it works (it should show you different answers each time).

2.(5 pts) Run the search program that you made above 20 times. Everytime enter the same number between 0 and 49 to be searched. Keep track of how many times the number was found amongst the random values of the 2-D array. What is the “found ratio” (that is, the number of times found divided by the total number of times you ran the program)?

3.(10 pts) Write a C++ program that creates an array of 100 elements that are populated by random numbers between 0 and 999 (hint: you can re-use a lot of code from the first problem!) Run the ascending sort algorithm on it - use the one demonstrated in class (and found on our website under the “demos” section). Measure how long it took to run the sort. To make such a measure, use a Linux OS tool called time that measures how long a program takes to execute. To use time, do the following:

step a. Compile your program. Let us assume that the executable (object) file is called Prob3.

step b. At the Linux prompt (%), type: time ./Prob3 and hit return.

step c. The program will be executed and, at the bottom, you will see a print out of 3 lines showing execution times (use the first entry - “real” - for this homework).

Now modify the program so that the array size is 1,000 elements and re-run the sort algorithm. Again, measure how long it took to run the sort - was it noticeably longer? If so, by what (approximate) factor?

One more time, modify the program for an array size of 10,000 elements, re-run and time the sort and answer the same questions.

4.(5 pts) Write a function called swapFrontBack that takes as input a vector of integers. The function should swap the first element in the vector with the last element in the vector. The function should check if the vector is empty to prevent errors.