Looping

Lab Assignment

The assignment contains two parts
  1. Positive sum. Study hovering. As you work on this project, demonstrate to the lab instructor looking up the value of a variable by hovering over it. Create a project titled Lab3_Sum with a single file sum.cpp. Your program should ask the user for a sequence of numbers until the user inputs 0 (zero) and then the program should print the sum of positive numbers that the user input. Here is an example dialog:
    Input sequence of integers (zero to stop):  -4 3 -20 10 45 100 -1 0
    The sum of positive numbers is: 158 
    
    You are not allowed to use functions, arrays or other constructs that we have not yet studied.

    Hint: while or do-while would be a good construct to use. Consider a single number in each iteration. In thewhile-expression check if this number is not zero. If not -- proceed, if yes -- stop evaluation and print out the result. Declare a variable where your program will collect the sum of positive numbers. Make sure to initialize this variable. In the loop, with an if, check if the number is positive and, if yes, add it to the sum. Look at this example of selecting the maximum number. It is very similar to what you need to implement.

  2. Figures. Create a project titled Lab3_Figures with a single file figures.cpp. Your program should ask the user for a number and then print a square and two triangles out of stars whose size is equal to the number input. Here is an example dialog:
    Input number: 4
    
    ****
    ****
    ****
    ****
    
    ****
    ***
    **
    *
    
    ****
     ***
      **
       *
    
    Note that your program has to work for an arbitrary integer, not just 4. You can use either while or for looping constructs.

    Hint: For each figure, you need to use two nested loops: the outer loop will iterate over rows (of stars) and the inner loop will print out the the stars in a single row. The programming structure for all figures is similar. Study the code for the odometer program. This program has nested loops which are very similar to what you need to implement.

    I suggest you code and debug the first figure printout and then proceed to the next. For the second figure, the number of stars printed in the inner loop is the same as the row number. That is, the loop variable of the outer loop should be present in the loop-expression in the inner loop. The third figure code is similar to the first two: the total number of characters is the same in each row. However, at some point, the character changes from space to star.

Make sure you program adheres to proper programming style. Submit your project to the subversion repository. Do not forget to verify your submission on the web.