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

Slides:



Advertisements
Similar presentations
4 Control Statements: Part 1.
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
 2006 Pearson Education, Inc. All rights reserved Introduction.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Chapter 7 Introduction to High-Level Language Programming.
Control Structures in C++ while, do/while, for switch, break, continue.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
Lecture 3 Structured Program Development in C
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
計算機程式語言 Lecture 03-1 國立臺灣大學生物機電系 3 3 Assignment, Formatting, and Interactive Input.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order.
Chapter 3: Assignment, Formatting, and Interactive Input.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
1 Lecture 3 Control Structures else/if and while.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) Fax: (757) Office: Gosnold 217b Subject Overview.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
Control Structures Sequential execution Transfer of control
while Repetition Structure
for Repetition Structures
Chapter 4- part 2 Control Statements: Loops 1
Adopted from Pearson Education slides
Chapter 4 Control Statements: Loops 1
3-4-5 Introduction.
Let’s all Repeat Together
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
3-4-5 Introduction.
C++ for Engineers and Scientists Second Edition
Presentation 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 << “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

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<< 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., ) Dr. Ming Zhang

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

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 e e-3 Dr. Ming Zhang

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

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

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