Objectives
- Write some C++ functions
- More practice with the g++ compiler, gdb, and emacs
- A very brief intro to makefiles
- Running tests
Setup
Update the source code for your labs repo by running the following commands in the terminal:
$ cd labs
$ git pull
This should create a new directory called functions
along with a few files inside it as follows:
$ tree
.
├── functions
│ ├── functions.cpp
│ ├── functions.h
│ ├── functions_test.cpp
│ ├── main.cpp
│ └── makefile
├── makefile
└── test.cpp
Change into the functions
directory to begin the lab.
1. Rounding with <cmath>
Use emacs to open the file main.cpp
. You’ll see the usual int main
function and a directive to include functions.h
- do not delete this code.
In main
, write a complete C++ program which behaves exactly as follows, where bold text indicates user input:
$ ./funky enter num: 2.4 round(2.4) = 2 $ ./funky enter num: 2.6 round(2.6) = 3 $
You’ll need to use the round
function from the <cmath>
library.
Compile your code as usual (running g++ main.cpp
, then executing ./a.out
). Fix any compile-time errors, and debug any logic errors before proceeding.
2. Your own rounding function
Older versions of the standard library didn’t include a round
function, but they did include a floor
function, which truncates. Using this, you should be able to write your own version of round without needing if
statements.
Hint: it might be helpful to work the logic out on paper before you start coding.
Open the files functions.cpp
and functions.h
. Copy the function header for my_round
from functions.h
to functions.cpp
. Then, without using the built in round() function
, implement my_round
in functions.cpp
. You will need to include the <cmath>
library in functions.cpp
as well.
Note: the original instructions said not to include
cmath
, but this was a mistake! You still needcmath
for thefloor
function.
Call my_round
from main
instead of round
. This time, you’ll need to compile a little differently: instead of running the command g++
, run the command make
(with no arguments). This will process the instructions in the makefile
and build an executable named main
. Run it as usual with ./main
.
3. Testing
There is a third file in this directory called test_functions.cpp
. Leave it alone! Instead, cd up a level so that you’re at the top-level labs directory:
$ cd ..
Now, compile the test
program by running the following command:
$ make lab=functions
Note: this instruction was edited on January 22nd to reflect changes to the test makefile. Originally it said to run
make
followed by./test
, but now you must specify which lab you want to test.
This will build and run the test program.
If all goes well, you should see the following output:
Running main() from gtest_main.cc [==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from Functions [ RUN ] Functions.MyRound [ OK ] Functions.MyRound (0 ms) [----------] 1 test from Functions (0 ms total) [----------] Global test environment tear-down [==========] 1 test from 1 test case ran. (0 ms total) [ PASSED ] 1 test.
If instead you see the following:
[ FAILED ] 1 test, listed below: [ FAILED ] Functions.MyRound
This means that the tests are working properly, but you have a logic error in your my_round
function.
Lab Assessment
When you are satisfied with your my_round
function, add and commit your changes to your labs repo, then git push
to submit. Your instructor will be building and running the exact same tests to determine whether you passed the lab.
January 26 update: If you haven’t run the steps to fix your labs configuration, now is a good time. Follow the instructions here .