CS302 -- Lab 2 -- Binary Search


What you are to submit

You are to submit the file src/lab_2_solution.cpp.

When we grade this, we will copy src/lab_2_driver.cpp, include/lab_2.hpp and makefile from the lab directory. We will then compile your src/lab_2_solution.cpp with:

UNIX> make
It should compile with no warnings, and it will put its executable into bin/lab_2. The gradescript works on this file.

To start the lab

On a lab machine, get yourself to a fresh directory and copy/paste the following:
mkdir bin
cp -r /home/jplank/cs302/Labs/Lab2/src .
cp -r /home/jplank/cs302/Labs/Lab2/include .
cp /home/jplank/cs302/Labs/Lab2/makefile .
chmod 0400 include/lab_2.hpp 
chmod 0400 src/lab_2_driver.cpp 
You should now see the following (obviously, the username and dates will be different).
UNIX> ls -l
total 4
drwxr-xr-x. 2 jplank jplank   6 Aug 22 12:26 bin
drwxr-xr-x. 2 jplank jplank  23 Aug 22 12:26 include
-rw-r--r--. 1 jplank jplank 229 Aug 22 12:26 makefile
drwxr-xr-x. 2 jplank jplank  56 Aug 22 12:26 src
UNIX> ls -l bin
total 0
UNIX> ls -l include
total 4
-r--------. 1 jplank jplank 1689 Aug 22 12:26 lab_2.hpp
UNIX> ls -l src
total 8
-r--------. 1 jplank jplank 3385 Aug 22 12:26 lab_2_driver.cpp
-rw-r--r--. 1 jplank jplank  221 Aug 22 12:26 lab_2_solution.cpp
UNIX> 
Go ahead and compile a skeleton version of your lab program:
UNIX> make
g++ -Wall -Wextra -std=c++11 -Iinclude -o bin/lab_2  src/lab_2_driver.cpp src/lab_2_solution.cpp
UNIX> 
This program will run, but it's not correct. You'll have to change it to be correct. However, going through these steps means that you have everything set up correctly.

How this lab works

I have written an include file in include/lab_2.hpp, and a driver file in src/lab_2_driver.cpp. You are not allowed to modify these files (which is why you do the chmod commands above).

Now, the driver reads two integers on standard input -- a seed, and a selector. The seed is an integer, which seeds a random number generator. The selector is a number from 0 to 2. The selector tells the drive what kind of problem that it is going to make you solve:

Here are the prototypes defined in the include file:

/* You implement these */

unsigned long long whats_my_number_ll();
double whats_my_number_d();
unsigned int optimal();

/* These are implemented for you in src/lab_2_driver.cpp */

bool am_i_greater_ll(unsigned long long v);
bool am_i_greater_d(double v);
double func(unsigned int x);


Starting out

After you have copied the files and compiled, you'll have a running executable in bin/lab_2. It won't be correct. However, it will tell you the correct answer. Try it out:
UNIX> echo 0 0 | bin/lab_2     # When the selector is 0, it prints your answer and then the correct answer
0 959407e5562c640b Incorrect
UNIX> echo 0 1 | bin/lab_2     # When the selector is 1, it prints the correct answer only
0.5761091880 Incorrect
UNIX> echo 0 1 | bin/lab_2 P                            # If you add the command-line argument "P", then when
0.5761091880 Incorrect                                  # the selector is 1, it prints out some extra information.
The number (to 10 digits) is    0.5761091880            # This is to help you develop your code.
Your answer (to 10 digits) is   0.0000000000
Difference between the numbers: 0.5761091880
UNIX> echo 0 2 | bin/lab_2     # When the selector is 2, it prints your answer and then the correct answer
0 3417165405 Incorrect
UNIX> 
Work on the three different programs one at a time. Use the binary search lecture/lecture notes to help you. As I said in class, binary searches are conceptually simple, but require attention to detail, and the details can get you. So, while you are developing your code, make sure you print out what's going on in your search.

Gradescripts

You call the gradescript with a number. It will divide that number by three (integer division) to generate the seed, and it will take that number modulo three to generate the selector. The gradeall script simply calls the gradescripts with numbers from 1 to 100. It gradescripts should finish in a few seconds. If they take longer, then you are very likely not implementing binary search correctly.