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:
- File I/O (fopen, fclose, fprintf, fscanf, ferror)
- Math functions (pow & exp)
- emacs, git, gcc
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, 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. -
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/ex06
– you should see compound.c and input.txt inside, along with inputBad1.txt, inputBad2.txt and outputGood.txt.
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 ex06
and navigate into it. -
Copy the template files to the current directory using:
cp ~/cs220-sp24-public/exercises/ex06/* .
.
Part 3
The primary files that you just copied to your ~/my220repo/ex06 directory are named compound.c and input.txt. compound.c is an incomplete program that you must complete and test yourself.
-
Open the file for editing using
emacs compound.c
(orvim compound.c
, if you usevim
.) -
Read the source code and the comments. Your instructions are in the comments. You will find it handy to use this website for looking up formulas to solve the problem.
-
Modify the source files and test by:
- Compiling with
gcc -Wall -Wextra -std=c99 -pedantic compound.c -o compound -lm
or if you prefer to take advantage of the alias:gccc compound.c -o compound -lm
. - Running with
./compound input.txt
(change up to test with inputBad1.txt and inputBad2.txt also.)
- Compiling with
The gcc
command we are using today includes the extra argument -lm
which tells gcc
to include the m
(math) library when linking. Later this week, we will talk more about the relationship between header (.h
) files, libraries, the compiler, and the linker.
The interest rates in the input.txt file are expressed as fractions, not percents. So 0.3 means 30%, not 0.3%.
When the “compound” program runs successfully, it places its output in a new file called output.txt. You will not see the output at the terminal. (You might see output at the terminal if your program fails, but you should not see any if it succeeds.) To check the contents of the file output.txt, use a command like cat output.txt
or less output.txt
. In the instructors’ solution, the first two lines of output.txt are:
107.00 107.23 107.25
580.25 582.74 582.98
Unix Tip of the day - diff
We gave you expected output file outputGood.txt, which is what your program should output when run with input.txt as input. Once you’ve run your code successfully, you can use the unix diff
command to see if your results in output.txt match the expected results in outputGood.txt as follows: diff output.txt outputGood.txt
.
No output from running this command means they are exactly the same. Otherwise, you will see lines with < or > at the start, indicating which lines differ, and how they appear in one file or the other. Another way to compare them is to open both in emacs
- either in side by side terminal windows, or within emacs using the ctrl-x 4 f
command (type 2nd filename when prompted). Use ctrl-x o
(other) to toggle between the file buffers. Use ctrl-x 1
with the cursor in the window you want to keep when done looking at both.
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!