Loopy Conditions
- Summary
- You will write three programs using loops, conditionals, or both.
- Objectives
- Reinforce the basic operations and control structures of the C programming language.
Problem 1: Translating Grades
While individual faculty may give numerical grades to evaluate student work, the College requires these numbers to be translated into letter grades. Suppose one instructor uses the following translation table
| Percentage | Letter Grade |
|---|---|
| 0–60 | F |
| 61–70 | D |
| 71–80 | C |
| 81–90 | B |
| 91–100 | A |
and adds modifiers (+ and −) based on the last digit of the score, as follows:
| Last Digit | Modifier |
|---|---|
| 1–3 | − |
| 4–7 | [None] |
| 8–0 | + |
An F is only an F, however. (There is no F+ or F−.)
Write a program grade.c that begins with an initialized
variable for the percentage grade, and prints the appropriate letter
grade.
Your program should be organized so as to minimize the number
of if statements.
Problem 2: Triangulation
Write a program that prints a triangle of a given number of lines.
* * * * * * * *********
* * * *****
*
* *
* *
* *
* *
* *
* *
* *
* *
*******************
Write a program triangle.c that begins with an
initialized variable for the number of lines and produces the
appropriate output.
Problem 3: Ancient Computing
In Book 7 of Euclid's Elements (ca 300 BC), Proposition 1 and Proposition 2 detail a method for finding the greatest common divisor of two positive integers.
In the Art of Computer Programming (Volume 1, p. 2), Donald E. Knuth describes it this way:
Given two positive integers m and n, find their greatest common divisor, that is, the largest positive integer that evenly divides both m and n.
- [Find remainder.] Divide m by n and let r be the remainder. (We will have 0 ≤ r< n.)
- [Is it zero?] If r = 0, the algorithm terminates; n is the answer.
- [Reduce.] Set m ← n, n ← r, and go back to step 1.
For example, GCD(66,24) = 6 because
- 66 ÷ 24 has a remainder of 18,
- 24 ÷ 18 has a remainder of 6, and
- 18 ÷ 6 has a remainder of 0.
Write a program euclid.c that uses Euclid's algorithm to
iteratively calculate the greatest common divisor of two
pre-initialized values. That is, it must use a loop, not recursion.
General Notes
Your testing should cover an appropriate range of "input" values and your program's output should make it easy to assess correctness.
Grading
In addition to the general grading guidelines for evaluation, the assignment is worth 25 points.
-
Comments on Program Format, Comments, Readability, etc.
(Points not given, but points can be deducted.) -
[7 points] Translating Grades
- [2 points] Produces correct letter grade
- [3 points] Produces correct modifier
-
[2 points] Minimizes
iforcasestatements
-
[5 points] Triangulation
- [1 points] Prints specified number of lines
- [2 points] Prints correct spacing left of asterisks on a row
- [2 points] Prints correct spacing between asterisks on a row
-
[8 points] Ancient Computing
- [1 points] Calculates the remainder of dividend and divisor
- [1 points] Terminates when remainder is zero
- [2 points] Correctly updates values
- [2 points] Repeats steps with an appropriate loop
- [2 point] Prints input and GCD of input
-
[5 points] Testing
- Test Plan with a numbered listing of the circumstances that can reasonably arise in each problem
- Listing of test cases to be considered, with the expected outcome
- Listing of actual test runs
- Statement of why the program is correct
A five point penalty will apply to any submission that includes personally identifying information anywhere other than the references/honesty file.
