top-down stepwise refinement (l11) * top-down stepwise refinement * cast operator * promotion...

Download Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator

If you can't read please download the document

Upload: georgiana-greene

Post on 18-Jan-2018

231 views

Category:

Documents


0 download

DESCRIPTION

Example of Top-Down Refinement (1) * Top Determine the class average for the quiz * First Refinement Initialize variable Input, sum, and count the quiz grades Calculate and print the class average Dr. Ming Zhang

TRANSCRIPT

Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator * Nonparameterized Stream Manipulator Dr. Ming Zhang Top-Down Stepwise Refinement * The top is a single statement that conveys the overall function of the program. * Then we divided the top into a series of smaller tasks and list these in the order in which they need to be performed. This results the first refinement. * To proceed to the next level of refinement until no further level refinement could be taken. Dr. Ming Zhang Example of Top-Down Refinement (1) * Top Determine the class average for the quiz * First Refinement Initialize variable Input, sum, and count the quiz grades Calculate and print the class average Dr. Ming Zhang Example of Top-Down Refinement (2) Second Refinement Initialize total to zero Initialize counter to zero Input the first grade (possible the sentinel) while the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter input the next grade (possible the sentinel) Dr. Ming Zhang Example of Top-Down Refinement (3) Second Refinement If the counter is not equal to zero Set the average to the total divided by the counter Print the average else Print No grade were entered Dr. Ming Zhang Example of Top-Down Refinement (4) Programming (Fig.2.9) cout > grade; while ( grade != -1) { total = total + grade; gradeCounter = gradeCounter + 1; cout > grade; } Dr. Ming Zhang Cast Operator static_cast * Cast Operator static_cast creates a temporary floating-point copy of its operand in parentheses - total. * Using a cast operator in this manner is called explicit conversion. * The value stored in total is still an integer. * The calculation now consists of a floating- point value (the temporary float version of total) Dr. Ming Zhang Promotion * The C++ compiler only knows to evaluate expressions in which the data types of the operands are identical. * To ensure that the operands are of the same type, the compiler performs an operation called promotion(also called implicit conversion) on selection operands. * For example, in an expression containing the data types int and float, int operands are promoted to float. Dr. Ming Zhang Examples of Promotion int total, gradeCounter; float average; average = static_cast (total)/gradeCounter; gradeCounter is promoted to float Dr. Ming Zhang Unary Operator * Unary Operator An operator takes only one operand. * Example of Unary Operator +: +1, +4, +5 -: -1, -4, -5 static_cast : static_cast (total) static_cast (average) Dr. Ming Zhang Parameterized Stream Manipulator * Preprocessor / Programs that use parameterized stream manipulator must contain the preprocessor /. * Parameterized Stream Manipulator setprecision (2) setiosflags(ios::fixed| ios::showpoint) Dr. Ming Zhang setprecision ( ) * cout