Input sequence of integers (zero to stop): -4 3 -20 10 45 100 -1 0 The sum of odd numbers is: 47You 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.
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:
column index is red row index is green |
column 0 | column 1 | column 2 | column 3 | column 4 |
row 0 | 00 | 01 | 02 | 03 | 04 |
row 1 | 10 | 11 | 12 | 13 | 14 |
row 2 | 20 | 21 | 22 | 23 | 24 |
row 3 | 30 | 31 | 32 | 33 | 34 |
row 4 | 40 | 41 | 42 | 43 | 44 |
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.