CS 16, Winter 2018

Programming Assignment 2

Due: Tuesday, January 30, 11:59pm
Worth: 50 points

PA2 can be done either in two-people teams (using pair programming) or individually - see programming policies. 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 in a file named sum.cpp to solve a variation (as specified below) of the problem described in Chapter 2, Programming Project 9:
    Write a program that first reads in how many whole numbers the user wants to sum [always 10 in the textbook's simpler version], then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately.

    First look at a typical session (user input is bold), then read detailed comments below:

    -bash-4.3$ ./sum
    How many numbers will you enter?
    6
    Enter 6 whole numbers while I count down.
    Entries left to go: 6
    19
    Entries left to go: 5
    -4
    Entries left to go: 4
    0
    Entries left to go: 3
    -6
    Entries left to go: 2
    3
    Entries left to go: 1
    -4
    The sum of 2 numbers greater than zero is 22.
    The sum of 3 numbers less than zero is -14.
    The sum of all 6 numbers is 8.
    Goodbye.

    • The program prints a string of text to the terminal before getting each piece of input from the user.
    • Each string printed by the program must include a newline at the end, but no other trailing whitespace (no whitespace at the end of the line).
    • If the user enters 0 or less in response to the first question, instead of executing the rest, the program simply must print "There is nothing to sum." Here is an example of that situation:
      How many numbers will you enter?
      0
      There is nothing to sum.
      Goodbye.
    • Otherwise the program prints the prompt that tells the user to enter the specified number of whole numbers. For the first example above, this number was 6, so the program printed "Enter 6 whole numbers while I count down."
    • Then the program enters a loop, and performs the following steps on each iteration:
      1. The countdown message is printed (i.e., "Entries left to go: 6").
      2. The next number that the user enters is read in, and of course stored in a variable.
      3. It is determined whether this number is greater than, equal to, or less than zero.
      4. Depending on the entered number's value, either the count and sum of positive or negative values are updated, or nothing is done if the value entered is zero.
    • After the loop ends (after the countdown goes to 0, and the user has entered all numbers), the program prints the results. Don't worry if sometimes the results include the phrase "1 numbers" (instead of "1 number") - you will learn a neat fix for that small quirk later in the course.
    • At the end, the program always prints "Goodbye."
    • A session should look exactly like one of the examples above (including whitespace and formatting), with possibly different inputs and numbers in the output.
  2. Compile and test your program at CSIL. Please don't just let the submit program test it for you. There are at least two good reasons to test it yourself: (1) You will learn much more by doing your own testing, and you will need this knowledge and honed testing skills in later courses. (2) The submit.cs system is very busy, and not only will your repeated submittals contribute to slowing down the system, but you also probably will waste lots of time waiting for failed results (and you might not be any closer to knowing why).
  3. Submit PA2 at https://submit.cs.ucsb.edu/, or use the following command from a CS terminal:
    ~submit/submit -p 937 sum.cpp
    Be sure to wait for the test results. If you score 50/50 and you've followed all of the other rules, then you'll earn full credit.

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