Exercise 4
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 homework assignments.

Caution

If your prior programming background is not in Java or C++, this exercise may take you a little longer to complete.

Learning Objectives

This exercise will help you practice your Unix/Linux and git commands, the text editor emacs (or vim), and will also introduce you to the basic data types, I/O statements, and simple control structures in C.

Part 1

Log into your ugrad Linux account using Terminal/PuTTY depending on what computer you are using. Navigate (using cd) into the cs220-sp24-public repo you should have cloned already (see Exercise 3-B). Do a git pull command to update your copy of the repo. You should now see an ex04 subdirectory if you do an ls exercises command.

Next go back to the enclosing directory (using cd .. command) and rename your private repository from 2024-spring-student-JHED to my220repo by typing mv 2024-spring-student-JHED my220repo, where JHED is your own unique JHED ID. This will make it easier to navigate to your private repo directory and makes your linux commands shorter. (Note that the repo name on github will not change.)

Then, you need to copy the file ex04/gpa_simple.c file into your repo. You could do this a few different ways. Our suggestion is to go to your home directory first, assuming both the cs220-sp24-public and your personal my220repo folders reside in your home directory. (If you made a subdirectory for this course, start there.) Try using tab completion as you type the cp command so that you do not have to type every single letter.

$ cd
$ cp cs220-sp24-public/exercises/ex04/gpa_simple.c my220repo
$ cd my220repo # (copy of your personal repo created with Exercise 3-A, and then renamed)
$ ls

Now you should see the gpa_simple.c file along with whatever else is in your repo.

So right now, gpa_simple.c is at the top level of your personal repo (my220repo). But we would like your personal repo to have a little bit more structure, since you will be using it for all exercises and most homework this semester. So, we suggest that you make a subfolder named exercises, and another one inside exercises named ex04. We will then put gpa_simple.c there. First, let us confirm that you are still at the top of your personal repo. Do this by typing the pwd command and see what it reports. It should look something like this:

$ pwd
/home/YOUR_LOGIN/my220repo

If it does not look like that, then you will need to navigate to your my220repo. Now, from the top of your personal repo (i.e., inside my220repo folder), type:

$ mkdir exercises
$ cd exercises
$ mkdir ex04
$ cd ex04
$ mv ../../gpa_simple.c .      # (note the space then dot at the end!)

This last command moves gpa_simple.c from two levels higher up, to the current folder (the ex04 subfolder you just created). Now it will not clutter up the top level of your repo.

A Tip for later

You may wish to make a similar structure for homework assignments: a homework folder with a subfolder named hw0. If you have not started working on hw0 yet, create that structure first. If you've already worked on hw0 in the top level my220repo folder and used git to add and commit the files, you'll want to use git mv <source> <destination> to rename them, then commit and push again. Feel free to ask for help in office hours or on Piazza.

Ultimately (after you create your structure for homework, specific instructions not covered here), your directory structure might look something like this:

/home/YOUR_LOGIN
├── /cs220-sp24-public
|   └── exercises
|       ├── ex02
|       │   └── hello_world.c
|       └── ex04
|           └── gpa_simple.c
└── /my220repo
    ├── exercises
    │   ├── ex02
    │   │   └── hello_world.c
    │   └── ex04
    │       └── gpa_simple.c
    └── homework 
        └── hw0
            ├── one.c
            ├── two.c
            └── three.c

Part 2

Now, back to the exercise. Remember that you need to add files you want to track in your repo. Hopefully, you are still in subdirectory my220repo/exercises/ex04. If not return there, then execute:

$ git pull       # (good habit every time you start to work)
$ git add gpa_simple.c

Now you should enter an editor (emacs or vim) to read and edit this file. It contains a problem description, sample run, and pseudocode for writing a program to compute a GPA. Your job is to write the C code for it, applying all the basic concepts we have discussed in class. You will need char, int and float data types. You will need to read some input with scanf, and this can be tricky, so we have a link to a hint below (or ask for help). You will also need to create output with printf. Lastly, you will need some selection statements and a loop.

Remember to save frequently, and also compile frequently. If you open a second shell window to log into the ugrad server, you can use one for editing, and one for compiling, rather than going in and out of the editor. Here is a reminder of our compile and execute commands:

$ gcc -std=c99 -Wall -Wextra -pedantic gpa_simple.c
$ ./a.out
Tip

If you are coming from a Python background, you will be most comfortable using a while loop and if/else statements in C. However, this problem has a good set-up for a switch statement, so if you do not have time in class today, try to write the grade conversion to point values with a switch statement later for practice.

This FAQ will help you figure out some of the subtleties in scanning characters in c with respect to skipping newlines: https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1352443831&id=1043284392

Hint

try using " %c%f" rather than just "%c%f" as your scanf format string (note the space)!

Once you have a (partial) program that compiles and runs, you should remember to commit it to your repo to create that snapshot:

$ git status   # (good habit, see if anything important needs to be `git add`ed for tracking)
$ git add gpa_simple.c   # (you'll likely need to add this file now!)
$ git commit -m "ex04 gpa working version"

You can then continue to work on the program development. Before you leave your work session, remember to commit one more time, and then push to synchronize with the remote repo:

$ git commit -m "gpa done for now"
$ git push
Reminder

Remember to submit to Gradescope to check your program results (see Exercise 3-A for submission instructions - same as homework), 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!