Homework Assignment 4 Solutions

Solutions:
  1. CountUp.C: 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!
    

    Solution

  2. BoxOfStars.C: 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
    *****
    *****
    **
    

    Solution

  3. LineOfStars.C: Write the program LineOfStars.C which prompts for the integer called lineSize and prints lineSize asterisks (*) on the line. Check and exit if the user inputs a negative integer.
    Solution: See TriangleOfStars.C

  4. TriangleOfStars.C: 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.
    
    Hint: The recursive function which you wrote for LineOfStars is very useful here.
    Solution