## Lab Exercise 4

### Introduction

Create a directory on your USB device for this lab, say, lab04, and launch IDLE.

This week, you do not need to submit your shell window at the end of the session. If you close it accidentally along the way, worry not!

You do need to submit a responses.txt file today. Download this template file and use it to record any answers or predictions asked for in the exercises.

### Task 1: Computing a Letter Grade

You might like to be able to compute your grade in this course, or explore "what if?" scenarios. A typical run might look like this:

```    Lab average         : 100
Program average     : 100
Midterm exam average: 80
Final exam grade    : 80
-------------------------
Letter grade : A
```

Or this:

```    Lab average         : 95
Program average     : 95
Midterm exam average: 70
Final exam grade    : 85
-------------------------
Letter grade : B
```

Step 1. Download this partially-completed program and add the statements necessary to produce the output above.

You can find the weights for the various components of the grade in the Course Evaluation section of the syllabus. You can simply multiply each grade by its corresponding weight and add.

Step 2. Run your completed program for a student with these grades: lab average : 100, program average : 95, midterm exam average : 85, and final exam grade : 82. Copy the result from the shell window and paste it in your responses file.

### Task 2: Computing an Average Grade

In order to use the program from Task 1, we need to be able to compute average grades for each. A typical run might look like this:

```    Enter number of programs: 3
Enter score on program 1: 12
Enter score on program 2: 18
Enter score on program 3: 20
----------------------------
Average score   : 16.666666666666668
```

Or this:

```    Enter number of programs: 5
Enter score on program 1: 15
Enter score on program 2: 17
Enter score on program 3: 20
Enter score on program 4: 19
Enter score on program 5: 20
----------------------------
Average score   : 18.2
```

Step 1. Download this program. It looks like it might do the job, but it produces incorrect output.

Step 2. Before running the program, read the code. Identify any errors you think will prevent it from producing the correct output. In your responses file, record each error and what its effect on the output will be.

Step 3. Run the code using the input data from the examples above. See if your predictions are correct. Note any discrepancies in your responses file.

Step 4. Fix the code. Run the corrected program on the input data from the examples above. Copy the resulting output from the shell window and paste it in your responses file.

### Task 3: Controlling a Loop with a Sentinel Variable

Sometimes, we don't know exactly how many items we need to enter as input. For example, we might be reading the scores from a file. In such cases, we need a different kind of loop than the one you saw in Task 2.

One alternative to a fixed-count for-loop is a sentinel loop. A sentinel is a person or thing that watches, or stands as if watching. The word originally referred to a soldier who stood guard and challenged all comers, to prevent a surprise attack. (Here is a dictionary definition, if you'd like to read more.)

In programming, it refers to a value that indicates the end of a sequence of legal values. The basic algorithm for a sentinel loop is:

1. Read a value.
2. If it is not the sentinel value,
• process the value,
• read another value, and
• go back to Step 2.
3. Perform the rest of the job.

You may recognize Step 2 this as a while-loop!

If we use a sentinel loop to compute average program grades, a typical run might look like this:

```    Enter program scores one at a time.
Enter -1 to signal that you are done.
Enter program score: 16
Enter program score: 20
Enter program score: 20
Enter program score: 20
Enter program score: -1
----------------------------
Average score   : 19.0
```

Or this:

```    Enter program scores one at a time.
Enter -1 to signal that you are done.
Enter program score: 12
Enter program score: 18
Enter program score: 20
Enter program score: -1
----------------------------
Average score   : 16.666666666666668
```

Step 1. Download this partially-completed program and add the statements necessary to produce the output above.

When you read a legal program score, the process the value step is quite simple: add it to a running total. Don't forget to also keep track of how many legal scores you read. You need that to compute the average!

Step 2. Run your completed program for a student with these program grades: 20, 18, 20, 20, 21, 17, 17, 19, 19. (Yes, that is a 21. I sometimes offer extra credit on an assignment.) Copy the result from the shell window and paste it in your responses file.

### Finishing Up

Make sure that your program files are complete and saved. Save your responses.txt file.

Submit your files for grading on the electronic submission system, at lab04 -- More Practice with Flow of Control.