Homework Assignment 2

Assigned: 22 January, 1997.
Due: 29 January, 1997.
New Due Date: 3 February, 1997.

Read: Chapter 4 in Structuring Techniques by Staugaard.

Goal: In this assignment you will write four C++ programs which do simple integer calculations, input and output. These are straight-line programs, without any sort of control structures such as if-statements or loops. Even so, they demonstrate several important mathematical properties of integer arithmetic, which you must be aware of for future programs.

In all these programs, proper input/output means that the user is clearly prompted for input, and the output is clearly labeled with its significance. Where the assignment requires written answers, put them as comments inside of the corresponding program. Put your name as a comment on the first line of your programs.

Program:

  1. Modulus.C: Write a program that inputs i and j and prints out i%j. The input and output of your program should follow the guidelines presented in the class text.
    Additional questions: What happens when j is zero? Why does this happen? What sort of error is this?
  2. CalcArea.C: Write a program that inputs a, b, c and d, integer dimensions in the following diagram, and prints out the area of the figure.
             
                            c
                         +-----+
                         |     |
                         |     |
                       b |     |
                         |     |
                 a       |     |  d
           +-------------+     |
           |                   |
           |                   |
           +-------------------+
    
    
  3. FencePost.C: A fence runs along an infinite line, with fence posts at integer locations. That is, there are posts at location 3, 4, 5, and so on. Given locations i and j, with j larger than i, how many posts are included between i and j, including i and j.
    Important note: The answer is not j-i.
  4. NChooseThree.C: Write a program which inputs integer n and outputs the integer n (n+1) (n+2) / 3. By changing the parentheses, show how three different answers are possible for a given n.
    Additional question: It is possible for the answer to be exactly correct using integer arithmetic, because n (n+1) (n+2) is always divisible by 3. In a comment in your code, argue that this is true. By argue, we mean give an informal but otherwise rigorous proof.
  5. NChooseTwo.C: A stair-step figure of size n is built up of 1-by-1 tiles. Write a programing which calculates and prints the total area of the figure for a given n. We show by example how the figure's shape depends on n. For n=3 the figure looks like:
                      +---+
                      |   |
                  +---+   |
                  |       |
              +---+       |
              |           | 
              +-----------+
    
    for n=4 the figure looks like:
                          +---+
                          |   |
                      +---+   |
                      |       |
                  +---+       |
                  |           |
              +---+           |
              |               | 
              +---------------+
    

    Hint: You will need to derive the formula for the area given n. Draw a diagonal from the bottom left corner to the upper right corner. The area is obviously the area of this triangle plus the area of the n little triangles cutoff above the diagonal. From this you get a formula, but you still might run into problems to with integer division. The previous problem discussed a similar formula, and how to avoid incorrect calculations.