Exercise 5
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, but if you cannot complete it in class you are encouraged to work at home to complete what you did not get through in class. Once finished, submit your work to Gradescope as a single zip file. The workflow of submitting an exercise is similar to that of a homework assignment.

Objectives

Reinforces the following concepts:

  • Conditionals and loops
  • Arrays and strings
  • Characters and ASCII table
  • Compound assignment / increment
  • emacs, git, gcc, zip

Part 1

Log into an undergraduate cluster computer. Confirm that your personal working git repository and the public course git repository are both cloned. Specifically, your home directory should have:

  1. A subdirectory named my220repo, cloned from your personal private github repo.
    Note

    Remember in the previous exercise, we renamed the ugrad copy of your private repo from 2024-spring-student-JHED to my220repo for brevity.

  2. Another subdirectory cs220-sp24-public, cloned from the public, coursewide repo.

If this is not the case, ask for assistance. You may need to redo steps from earlier exercises.

Part 2

  1. Type cd cs220-sp24-public to navigate into your working copy of the remote, coursewide public repo.

  2. Type git status and confirm you have not modified any files or accidentally committed to the public repo. That is, you should not see any files categorized as “Changes to be committed”, “Changes not staged for commit” or “Untracked.” You should also not see the message “Your branch is ahead of ‘origin/master’ by X commit(s).” If you do, please speak with an instructor or CA for a fix.

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

  4. Confirm that you can see the template (a.k.a. starter) files for today’s exercise by typing ls exercises/ex05 – you should see three .c source files listed.

Part 3

  1. We created a .bashrc unix shell file that contains several alias and environment settings that will make your ugrad server lives better. Along with this, we have a .bash_profile configuration file which will get the .bashrc settings into place when you log in remotely. While you are still in the cs220-sp24-public directory under ex05, type ls -a to see these (hidden) files.

  2. Assuming you do not already have a customized .bashrc file in your home directory (ask for help if you already have one!), type cp .bashrc ~ to copy our version of the file to your home directory. Next, type cp .bash_profile ~ to copy the other file to your home directory. Then type source ~/.bashrc to run it. Normally this configuration file will automatically run every time you log into a ugrad unix server, but for now we want it to take effect in this session.

  3. Type cat ~/.bashrc to see the contents of that file. It sets your default EDITOR to be emacs (feel free to change it to vim if you prefer, then re-type the source command above), and it defines two aliases we can use as shortcuts when compiling our programs. The first one, gccc (note the extra ‘c’!) is our standard gcc command with all the required flags already included. To use it, just type gccc myfile.c to compile a file. The lines beginning with a hashtag are comments in this unix shell scripting language. The second alias will be useful when we start using C++.

Part 4

  1. Use cd ~/my220repo to move to your working copy of your personal repo. Type git pull to sync any updates you may have made from another copy.

  2. Use cd exercises to move to your personal exercises folder. (If you have not made one yet, type mkdir exercises first.)

  3. Make a new directory for today’s exercise by typing mkdir ex05.

  4. Change to the new directory with cd ex05.

  5. Copy the three template source files to the current directory using: cp ~/cs220-sp24-public/exercises/ex05/*.c . (Don’t forget the space-then-dot at the end of the line above! Also, remember to insert any subdirectories you may have created into the path above.)

Part 5

The three files that you just copied to your ~/cs220/ex05 directory are named count1.c, count2.c and count3.c. Each of the files is an incomplete program that you must complete and test yourself. Starting with count1.c:

  1. Open the file for editing using emacs count1.c or vim count1.c.

  2. Read the source code and the comments. Your instructions are in the comments!

  3. Modify the source files and test by:
    • Compiling with gcc -Wall -Wextra -std=c99 -pedantic count1.c -o count1 (or by simply typing gccc count1.c -o count1 to use your new alias!) The extra -o count1 portion at the end of the line is asking for the executable created by gcc to be named count1 instead of the usual a.out.
    • Running with ./count1.

  4. Once you have a (partial) program that compiles and runs, you should remember to commit it to your repo to create that snapshot, and push before exiting this session:
    $ git status   # (good habit, see if anything important needs to be `git add`ed for tracking)
    $ git add count1.c   # (you'll likely need to add this file now!)
    $ git commit -m "ex05 count1 working version"
    $ git push  # (when ready to update remote repo)
    
  5. Do the above steps for the other two files: count2.c and count3.c. We don’t expect you to complete them all during class time, but they are excellent practice for understanding basic C syntax, working with characters and arrays, and coding common algorithms.
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!