Exercise 7
Info

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.

Learning Objectives

Reinforces concepts learned in today's meeting:

  • Function declarations
  • Using sizeof
  • Passing arrays to functions
  • Recursion

Part 1

  1. Log into an undergraduate cluster computer.

  2. 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.

  3. Navigate into cs220-sp24-public, type git 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.

  4. Type git pull to synchronize your local repo and working copy with the remote repo.

  5. 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

  1. Navigate into the exercises folder in your personal repo (cd ~/my220repo/exercises).

  2. Make a new directory for today’s exercise by typing mkdir ex07 and navigate into it.

  3. Copy the source file to the current directory using: cp ~/cs220-sp24-public/exercises/ex07/* ..

Tip

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

  1. Compile your newly-copied functions.c file by typing our usual compile command: gcc -std=c99 -Wall -Wextra -pedantic functions.c (or use gccc functions.c if you like). You will notice that the program cannot be compiled because the div function is called before it is defined.

  2. Edit functions.c to include a declaration of the div function before main, similar to examples in the lecture slides.

  3. 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 use gccc functions.c.)

  4. Test your program by executing ./a.out and reviewing the output.

Part 4

  1. 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 like float mult(float a, float b).

  2. In addition, implement a recursive fac function, which takes a non-negative integer and returns the factorial of it. If the input is negative, return 0. The function should have a prototype like int fac(int a) or maybe long fac(int a) since factorials get large very quickly.

  3. Uncomment the test lines in main in functions.c, compile and run the program to test your implementations.

Part 5

  1. 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) where low 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 array ra, or -1 if it is not found.

  2. 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.

  3. Compile and run your updated program to see if it works properly. Fix any errors you encounter.

  4. Add another function with this prototype int bsearch2(float ra[], int low, int high, float target, float results[], int size). The extra parameter results is an array of floats that holds the values inspected during the search for the target, and size is a variable that keeps track of how many values have been added to results.

  5. 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.

Reminder

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!