CS 16, Winter 2018

Programming Assignment 3

Due: Friday, February 9, 11:59pm
Worth: 100 points

This assignment can be done either in two-people teams (using pair programming) or individually. If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.

  1. Write a C++ program to print a variable number of rows of '*' characters arranged in the form of one to four side-by-side triangles. The user chooses the number of triangles and the triangle size.
    Here is a sample run in which the user asks for 4 triangles of size 5:
    -bash-4.3$ ./triangles
    enter number and size of triangles
    4 5
    *     ***** *****     *
    **    ****   ****    **
    ***   ***     ***   ***
    ****  **       **  ****
    ***** *         * *****
    -bash-4.3$ 
    • The source code file must be named triangles.cpp and we suggest you create a directory named ~/cs16/p3 to store it in.
    • Your program must make certain the user enters a number of triangles between 1 and 4, and a size greater than 0 in response to the prompt. If a faulty value is entered, the program does not print triangles and does not exit, but instead it calmly informs the user about the mistake(s) and prompts for the number of triangles and size again. (See "sample runs" link below.)
    • We highly recommend that you do not try to solve all of the problems at once! Partial credit will be available. Here is a sensible order to solve the various problems:
      1. Work on interacting with the user first. Make sure your program will gracefully handle faulty input values (but you don't have to worry about the user entering non-numbers). Print "number must be between 1 and 4" if the user enters a number of triangles outside that range, and/or print "size must be greater than 0" if the user enters a size less than 1. After printing the error message(s), the program must allow the user to re-enter the data, and this interaction must continue until the user gets it right.
      2. Work on the leftmost triangle next, without worrying about the other triangles. This much of the work will earn you partial credit, because your program will pass tests for just one triangle.
      3. Then work on the other triangles one at a time, from the left. You can earn more partial credit this way, as tests will be done for just 2 and for just 3 triangles. The triangles must be separated by one space at their widest parts, but realize that other spaces are also printed to make the triangles line up like the examples.
    • Here are some more sample runs that demonstrate proper behavior. Your results must match the solution's results exactly or it will fail the submit.cs tests. In particular, there must be no trailing spaces on any line of output (i.e., no spaces at the end of the line).
  2. Compile and test your program at CSIL before submitting it. Try several different inputs, including faulty user input to make sure your program works as it should. See if you can test it completely enough that your program will pass all of the submit.cs tests on the first try. By the way, two of the tests are "blind" (you won't be shown the correct output): one that asks for really large triangles, and one that repeatedly enters faulty data. Both blind tests are included to make sure your program is looping properly.
  3. Submit PA3 at https://submit.cs.ucsb.edu/, or use the following command from a CS terminal:
    ~submit/submit -p 948 triangles.cpp
    Be sure to wait for the test results. If you score 100/100 and you've followed all of the other rules, then you'll earn full credit.

Updated 1/30/2018, by C. Michael Costanzo