Looping

Lab Assignment

The assignment contains two parts
  1. Odd 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 odd 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 odd numbers is:  47
    
    You are not allowed to use functions, arrays or other constructs that we have not yet studied.

    Hints: You need to use iterate-and-keep-track idiom. If the number is odd, it has a non-zero remainder of the division by 2. while or do-while would be a good construct to use. Consider a single number in each iteration. In the while-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. Make sure to initialize this variable. In the loop, with an if, check if the number is odd 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 figure size and paint character and then print figures of user-supplied size and character as shown below. Here is an example dialog. User input is show in bold
    Input figure size: 7
    Input paint character: +  
    
    +++++++
    +++++++
    +++++++
    +++++++
    +++++++
    +++++++
    +++++++
    
    +++++++
    +     +
    +     +
    +     +
    +     +
    +     +
    +++++++
    
    +
     +
      +
       +
        +
         +
          +
    
          +
         +
        +
       +
      +
     +
    +
    
    
    +++++++
    ++   ++
    + + + +
    +  +  +
    + + + +
    ++   ++
    +++++++
    
    
    
    Note that your program has to work for an arbitrary integer, not just 7 and with an arbitrary character, not just +. You can use either while or for looping constructs.

    Hints: For each figure, you need to use two nested loops: the outer loop will iterate over rows (of characters) and the inner loop will print out the characters 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. The figure order is in the increasing difficulty.

    Consider the numbers of row and columns in a table. These numbers will be reflected by the loop variables:

    To code the second figure (hollow square), consider using a multiway if with the following logic:
      If it is the first line - then print a character
      else if it is the last line - then print a character
      else if it is the first column - then print a character
      else if it is the last column - then print a character
      else print a space
    

    In the third figure (the first diagonal line), you print a character when the row index is equal to the column index. That is, the loop variable of the outer loop should be present in the loop-expression of the inner loop.

    In the forth figure (the second diagonal line), you print a character when the column index is the same as the figure size minus the row index minus one. To put another way: row plus column equals figure size minus one.

    The last figure (the square with the cross inside) is the combination of the previous three. That is, the paint character is printed when the condition of any of the previous three figures is satisfied.

Milestone: implement part 1 (sum) of the assignment.

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.