This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions if you get stuck. Aim to complete as much as possible during our meeting, and submit on Gradescope to check your solution. You are encouraged to work at home to complete what you do not get through today.
Reinforces concepts learned in today's meeting:
- Function declarations
- Using
sizeof
- Passing arrays to functions
- Recursion
Part 1
-
Log into an undergraduate cluster computer.
-
Confirm that your personal working git repository and the public course git repository are both cloned. If this is not the case, ask for assistance. You may need to redo steps from week 1 exercises.
-
Navigate into
cs220-sp24-public
, typegit status
and confirm you have not modified any files or accidentally committed to the public repo. Ask for help if you have added or committed files there and need to undo. -
Type
git pull
to synchronize your local repo and working copy with the remote repo. -
Confirm that you can see the template files for today’s exercise by typing
ls exercises/ex07
– you should see functions.c inside.
Part 2
-
Navigate into the exercises folder in your personal repo (
cd ~/my220repo/exercises
). -
Make a new directory for today’s exercise by typing
mkdir ex07
and navigate into it. -
Copy the source file to the current directory using:
cp ~/cs220-sp24-public/exercises/ex07/* .
.
You should be familiar with this process of logging into ugrad, pulling to update the public repo, making a new directory in your private repo, and copying exercise files from the public repo into your private one by now. We won't necessarily include explicit instructions going forward.
Part 3
-
Compile your newly-copied functions.c file by typing our usual compile command:
gcc -std=c99 -Wall -Wextra -pedantic functions.c
(or usegccc functions.c
if you like). You will notice that the program cannot be compiled because the div function is called before it is defined. -
Edit functions.c to include a declaration of the
div
function beforemain
, similar to examples in the lecture slides. -
Once you have made your changes, re-compile the file by re-typing the compile command:
gcc -std=c99 -Wall -Wextra -pedantic functions.c
(or usegccc functions.c
.) -
Test your program by executing
./a.out
and reviewing the output.
Part 4
-
Now write your own function to declare and implement a
mult
function, which takes two floating point values and returns the result of their product. The function should have a prototype likefloat mult(float a, float b)
. -
In addition, implement a recursive
fac
function, which takes a non-negative integer and returns the factorial of it. If the input is negative, return0
. The function should have a prototype likeint fac(int a)
or maybelong fac(int a)
since factorials get large very quickly. -
Uncomment the test lines in
main
in functions.c, compile and run the program to test your implementations.
Part 5
-
Write a function declaration and definition to do a (recursive) binary search on an array of floats. The prototype of your method should be
int bsearch(float ra[], int low, int high, float target)
wherelow
is the inclusive low index of the range to search,high
is the inclusive high index of the range to search,target
is the value to find, and the function returns the location of the target in arrayra
, or -1 if it is not found. -
Uncomment the corresponding test lines in
main
in functions.c. Notice that we initialized the array to be searched with values in ascending order! Feel free to add more tests to be sure your function works properly in different cases. -
Compile and run your updated program to see if it works properly. Fix any errors you encounter.
-
Add another function with this prototype
int bsearch2(float ra[], int low, int high, float target, float results[], int size)
. The extra parameterresults
is an array of floats that holds the values inspected during the search for the target, andsize
is a variable that keeps track of how many values have been added toresults
. -
Uncomment the corresponding test lines in
main
in functions.c, compile and run the program to test your implementations. Print the results array after the search. Why can you print the updated results array in main, but not get the updated number of values added to it? Discuss with classmates.
Remember to add and commit to your local repo copy as your work. Push to your remote repo when finished. Also submit to Gradescope to check your program results and use exit
to logout from your ugrad account when finished. If you continue to work on the program after class, make sure to keep your repo updated as well!