Homework 1: Conditionals and Loops

This homework is to be done individually. You may ask your instructor for help, but do not ask other students, tutors, or mentors for assistance.

This homework assignment consists of two questions. You must do BOTH parts of the assignment for full credit. The assignment is worth 25 points.

See the "Grading" section below for specific criteria for this assignment. As with all projects and homework, the style guidelines apply to this assignment, and points may be deducted for poor style, readability, and testing. Feedback will be given using a version of this checklist.

Assigning Grades

I generally use numbers and percentages to score student work, but the College requires these numbers to be translated into letter grades. While I may adjust this conversion scheme at the end of the term, this is the most commonly occuring way that I translate numerical scores to letter grades:

Percentage Letter Grade
90 - 100 A
80 - 89 B
70 - 79 C
60 - 69 D
0 - 59 F

and I add modifiers (+ and −) based on the last digit of the score, as follows:

Last Digit Modifier
8 - 9 +
3 – 7 [None]
0 - 2 -

An F is only an F, however. (There is no F+ ; or F−.) Also note that 100 is an A+, despite the fact that it ends in a "0".

Write a program grade.c that asks the user for a numerical value, and prints the appropriate letter grade. You can assume that the input will be an integer, but you need to check that the value is not greater than 100 or less than 0. If improper values are entered, print a message and quit.

Your program should be organized so as to minimize the number of if statements. Note that there is a similar problem in King, Chapter 5, programming project 10, which contains a hint about how to handle at least one selection statement.

Greatest Common Denominator

Write a program that asks the user for two integers. These may be entered on the same line, separated by a space, or as separate lines. Then, your program should calculate the greatest common denominator (GCD).

For example:

Enter two integers: 12 28
Their greatest common denominator is: 4

As our book points out, this can be solved using Euclid's algorithm:

  1. Let m and n be variables containing the two numbers
  2. If n is 0, then stop since m contains the GCD
  3. Otherwise, compute the remainder when m is divided by n
  4. Copy n into m and then copy the remainder into n
  5. Repeat steps 2 to 4 as necessary until n is 0 and you're done.

If you are trying to find the gcd of 0 and 0, the result is undefined, but you may either report that 0 is the gcd or print a message that the gcd is undefined.

When testing, make sure you try at least one case in which you have prime numbers and one that includes a 0.

Grading

I highly recommend that for this (and all coding projects) that you make a testing plan before you start to write code. You do not need to plan to test all possible inputs to the two programs, but you should have a plan that will test major sections of your code and demonstrate your problem solving skills. Include this plan when you submit your code and testing transcript.

This homework assignment is worth 25 points. You should turn in TWO different programs, one for each half of the assignment.

Assigning Grades (12 points)

Greatest Common Denominator (13 points)