Homework Assignment 3

Assigned: Wednesday, Jan 29.
Due: Monday, Feb 10.

Read:

Program:
Write and debug the following four programs. The names of the programs are given. Use the PostIt script to submit your work.

  1. LargerOfTwo.C: Write a program which inputs two integers and prints the larger of the two.

  2. MiddleOfThree.C: Write a program which inputs three integers and prints the middle value among the three.

  3. NearestMultiple.C: Write a program which inputs two integers and prints the largest multiple of the second which is smaller than the first, and the smallest multiple of the second which as at least as large as the first. That is, input n and d, then find i and j=i+1 such so that (i*d) < n <= (j*d) and print (i*d) and (j*d).

    What conditions on n and d are necessary for this problem to be solvable? For extra credit, use if statements to prevent bad inputs.

  4. CompareFractions.C: Write a program that accepts four integers, i, j, k, n and prints which of the relations is correct: i/j < k/n, or i/j == k/n, or i/j > k/n For this problem, do not use floating point arithmetic. You should determine the equality or inequality using only integer arithmetic.

    Do this problem in 2 steps:

    1. Assume that i, j, k, n are positive.
    2. Deal with the troubles of allowing any integer values for i, j, k and n.