UNIT 1: REVIEW OF C++ PROGRAMMING FROM CTEC1239 (Chapters 1 to 6 in textbook; weeks 1 to 3) Upon successful completion of this unit, students must be able to: 1. Apply basic problem-solving techniques. Use the Software Engineering Method Requirements - this is usually given to you as the problem statement Analysis - What information is the solution to provide? (outputs) - What am I given? (inputs) - make sure that units and precision (decimal places) are correct Design (including refinements) Implementation (coding) Testing lather rinse repeat ... 2. Develop algorithms through the process of top-down, stepwise refinement. See #1. It is important to breakdown and refine your design so that, ideally, each of your design steps translates into few lines of C++ code, or into a C++ function. At each stage of the SEM, changes can be made to previous stages. For example, if in doing design, you require some more information, you can re-visit the analysis stage, and the information that you discover there may lead to changes in the requirements. 3. Use the if ... else and switch selection/decision structures to choose alternative actions. You can do everything using if ... else if ... else structures. When you are typing in an if statement, it is useful to type in an empty else statement for future maintenance. Note that the following structure: if ( ... ) if ( ... ) { { ... ... } } else if ( ... ) is actually else { { ... if ( ... ) } { else ... { } ... else } { ... } } Switch statements sometimes lead to more "elegant" solutions. 4. Use the while, do ... while and for iteration/repetition structures to repeatedly execute statements in a program. You can do all iteration with one of these loop structures, with creative use of break and continue (see below #7). 5. Use counter-controlled repetition and sentinel-controlled repetition. Counter-controlled repetition uses an int variable (typically), and will continue looping until the value of that variable reaches a certain value. Depending on how the loop is constructed, the variable is either incremented or decremented, either before or after its value is tested, either before or after processing inside the loop occurs. Sentinel-controlled repetition relies on user input (any form of external input, actually, for example, reading from a file), to determine when to stop looping. The "stop looping" value is called a "sentinel". 6. Use the mathematical, assignment, logical and relational operators. Mathematical: (), +, -, *, /, % (modulus, i.e., integer remainder) Logical: && (and), || (or), ! (not) Relational: == (equality), != (non-equality), <, >, <=, >= Use parenthesis to force the compiler to evaluate your expressions in the order that *you* want. 7. Use the break and continue program control statements. break is used in the middle of a loop to exit the loop; break is used at the end of a case in a switch statement to stop processing that case; continue is used in the middle of a loop to restart the loop (which may or may not cause a counter to be updated) Usually, in a loop, break and/or continue are used inside an if or else statement. 8. Construct programs modularly from pieces called functions. Functions are good because you can test them in isolation, and use "programming by contract" preconditions and postconditions to simplify your code. You can also assemble a library of functions that you can re-use in subsequent programs. It saves time to use code (in the form of functions) that has already been written and tested. 9. Create user-defined functions. You can use the SEM to design functions: each function is like a mini-program, with its own inputs and outputs. Like pre-defined functions, which are declared in header (#include) files, user-defined functions must be declared before they are called. That way, the compiler knows about the function, and can help you by checking parameters and return types to make sure that the function is being called correctly. 10. Pass information between functions. By default, functions pass information by value, which means that any arguments passed to a function are copied and the function works with the copies. Any values that are returned are also copied. You can also pass by reference, which allows a function to change the value of an existing variable. C++ makes this easy using reference variables. Reference variables are also useful when you want to return more than one value from a function. Later we will use data structures and pointers as well as reference variables to do "pass by reference."