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:
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.
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 +-------------+ | | | | | +-------------------+
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.
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.
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:
+---+ | | +---+ | | | +---+ | | | +---+ | | | +---------------+