Homework Assignment 3

Assigned: Monday,Feb 5.
Due: Monday, Feb 12.

Read: 3.3 and 3.4.

Do: 3.8.9, 3.8.10

Program:

  1. Write the program CountUp.C which prompts for an integer called bingo and prints on separate lines the integers 1 through bingo in sequence, and then prints "Bingo!". Your program must work for any integer, exiting with a message if the user tries a negative integer. For example:
    Enter bingo: 4
    1
    2
    3
    4
    Bingo!
    

  2. Write the program BoxOfStars.C which prompts for two integers called starCount and lineSize and prints starCount asterisks (*) on several lines if necessary, so that each line has at most lineSize asterisks on it. Use the minimum number of lines necessary. Your program should work for all integer inputs, exiting with a message if the user inputs a non-positive lineSize or a negative starCount. For example:
    Enter starCount: 12
    Enter lineSize: 5
    *****
    *****
    **
    

  3. Write the program TriangleOfStars.C which prompts for the integer called baseSize and prints asterisks (*) baseSize on the first line, baseSize-1 on the second line, and so on, until one asterisk on the baseSize-th line. Again, check and exit if the user inputs a negative integer.

    Extra Credit: print out the number of stars printed. For example:

    Enter baseSize: 4
    ****
    ***
    **
    *
    Extra Credit: 10 stars printed.
    

  4. In Homework 2, Program 6 you wrote a program to determine the relative sizes of two fractions. To accomplish this task, you needed to write down in computer language the steps well-known to every child of how to manipulate fractions. As simple as this task is, the statment of the algorithm, that is, the exact steps stated in mathematics which accomplish this task, can be a surprising challenge.

    In this program we will solve the problem of placing an integer in reduced form. Recall that a fraction i/j is in reduced form if there are no common divisors in i and j. So 2/5 is in reduced form, but 2/6 is not. The fraction 2/6 is equal to 1/3, and 1/3 is in reduced form. The algorithm for this problem was known 2500 year ago and is today called the Euclidean Algorithm. It is a considerable challenge for someone to come up with this algorithm all by themselves!

    Write the program Euclidean.C which accepts two integers called i and j and finds the greatest common divisor of the two. This is what they would each have to be divided by to place i/j in reduced form. Check that i is not negative and j is neither negative nor zero, and exit with a message to the user if this is not true. For example:

    Enter integer i: 85
    Enter integer j: 153
    The lowest common divisor is 17