CSC 161 Schedule Readings Labs & Projects Homework Deadlines Resources

Loopy Conditions

Summary
You will write three programs using loops, conditionals, or both.
Objectives
Reinforce the basic operations and control structures off 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.

  1. [Find remainder.] Divide m by n and let r be the remainder. (We will have 0 ≤ r< n.)
  2. [Is it zero?] If r = 0, the algorithm terminates; n is the answer.
  3. [Reduce.] Set m ← n, n ← r, and go back to step 1.

For example, GCD(66,24) = 6 because

Write a program euclid.c that uses Euclid's algorithm to iteratively calculate the greatest common divisor of two pre-initialized unsigned values.

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.

A five point penalty will apply to any submission that includes personally identifying information anywhere other than the references/honesty file.