Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator."— Presentation transcript:

1 Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator * Nonparameterized Stream Manipulator Dr. Ming Zhang

2 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

3 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

4 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

5 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

6 Example of Top-Down Refinement (4) Programming (Fig.2.9)...... cout << “Enter grade -1 to end: “; cin >> grade; while ( grade != -1) { total = total + grade; gradeCounter = gradeCounter + 1; cout << “ Enter grade, -1 to end: “; cin >> grade; }.............. Dr. Ming Zhang

7 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

8 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

9 Examples of Promotion...... int total, gradeCounter; float average;....... average = static_cast (total)/gradeCounter;...... gradeCounter is promoted to float Dr. Ming Zhang

10 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

11 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

12 setprecision ( ) * cout<< setprecision(2)<< average; indicate that float variable average is to be printed with TWO digits of precision to the right of the decimal point (e.g., 92.37) * cout<< setprecision(3)<< average; indicate that float variable average is to be printed with THREE digits of precision to the right of the decimal point (e.g., 92.374) Dr. Ming Zhang

13 setiosflags(ios::fixed|iso::showpoint) * The stream manipulator setiosflags(ios::fixed|iso::showpoint) sets two output formatting options, namely ios::fixed and iso::showpoint. * The vertical bar character ( | ) separates multiple options in a setiosflags call. Dr. Ming Zhang

14 ios::fixed * The option ios::fixed causes a floating- point value to be output in so-called fixed-point format (such as 92.34), not the scientific notation (such as 9.23e+1). * Fixed-Point Format Scientific Notation 234.56 2.3456e+2 0.00479 4.79e-3 Dr. Ming Zhang

15 iso::showpoint * The option iso::showpoint forces the decimal point and trailing zero to print even if the value is a whole number amount (such as 88.00). * Without the option iso::showpoint, such a value prints in C++ as 88 without the trailing zero and without the decimal point. * The printed value is rounded to indicated number of decimal positions, although the value in the memory remains unaltered. Dr. Ming Zhang

16 Question 1 Exercise/Home Work for Lecture 11 * Write a C++ program to calculate and prints the sum of the integers from 1 to 20. Using the while structure to loop through the calculations and increment statements. The loop should terminate when the value of i (increment variable) becomes 21. Dr. Ming Zhang

17 Question 2 Home Work/Exercise for Lecture 11 Write a C++ program that uses the statements as follows to calculate x raised to the y power. The program should have a while repetition control structure. a) Input integer variable x and y with cin and >>. b) Initialize integer variable i and power to 1. c) Multiple variable power by x and assign the result to power. d) increment variable i by 1. e) Test i to see if it is less than or equal to y f) Output integer variable power with cout and <<. Dr. Ming Zhang


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

Similar presentations


Ads by Google