© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.

Slides:



Advertisements
Similar presentations
4 Control Statements: Part 1.
Advertisements

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 Control structures  Algorithm & flowchart  If statements  While statements.
Lecture 2 Introduction to C Programming
Introduction to C Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
 2006 Pearson Education, Inc. All rights reserved Introduction.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Introduction to C Programming
 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.
Structured Program Development in C
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
 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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Control Statements: Part I Chapter 4 Control Statements: Part I Chapter 4.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 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.
© 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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 7 Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Branching statements.
The Selection Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 4 Control Statements: Part I
Program Control using Java - Theory
Chapter 3: Introduction to Problem Solving and Control Statements
MSIS 655 Advanced Business Applications Programming
3 Control Statements:.
Presentation transcript:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and Program Control Outline 4.1 Test-Driving the Wage Calculator Application 4.2 Algorithms 4.3 Pseudocode 4.4 Control Statements 4.5 if Selection Statement 4.6 if…else Selection Statement 4.7 Constructing the Wage Calculator Application 4.8 Assignment Operators 4.9 Formatting Numbers 4.10 Using the Debugger: The Watch and Locals Windows 4.11 Wrap-Up

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Use basic problem-solving techniques. –Use control statements. –Use pseudocode as an application development tool. –Use the if and if…else selection statements to choose between alternative actions. –Use the assignment operators. –Define constants to contain values that do not change as an application executes. –Use the debugger’s Watch window to evaluate expressions. –Use the debugger’s Locals window to change variable values during program execution.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.1Test Driving the Wage Calculator Application

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.1Test Driving the Wage Calculator Application (Cont.) Inputting floating-point values and returning a result –Enter 12.5 at the Enter hourly wage: prompt. –Enter 50.5 at the Enter hours worked this week: prompt Figure 4.4 Running the Wage Calculator application.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Algorithms –The actions to be executed –The order in which these actions are to be executed Program Control –Task of executing statements in correct order 4.2Algorithms

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Pseudo code –Informal language to develop algorithms –Resembles everyday English Not a programming language –Only describes executable statements Examples Assign 0 to the counter is equal to: counter = 0; If student’s grade is greater than or equal to 60 Display “Passed” 4.3Pseudocode

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.4Control Statements Sequence, Selection, Repetition –Sequential Execution within a Block of Code Within a block of code Instructions are executed one after another in the order in which they are written Blocks of Code have a Single Entry / Exit defined by curly brackets {} A modern program is defined by an orderly Stacking and Nesting of blocks of code. –Conditional Statements (Selection) if, if…else, switch –if : single selection statement –if…else : double selection statement –switch : multiple selection statement –Looping Statements (Repetition) while, do…while, for

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.4Control Statements (Cont.) Unified Modeling Language (UML) –Activity Diagram Models the workflow of a portion of a software system –Symbols in UML Action-state symbols –Rectangles with left and right sides curved out »Represent an action about to occur Diamonds –Represent a decision to be made Small circles –Represent initial and final state of workflow Transition arrows

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.5 Sequence statement activity diagram. UML symbols –Action state symbols, diamonds, small circles, transition arrows UML notes –Similar to comments 4.4Control Statements (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.5 if Selection Statement if statement performs action based on condition –Conditions are boolean expressions (true / false value) Statement executes when boolean expression is true Conditional expression is inside parenthesis. Conditions are formed with equality operators and relational operators Example pseudocode If student’s grade is greater than or equal to 60 Display “Passed” Corresponding C++ code

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Relational operators –Greater than ( >= ), greater ( > ), less than ( <= ), less ( < ) Equality operators –Equal to ( == ), not equal to ( != ) Common Errors –It is a syntax error to add spaces in between the symbols ==, !=, >=, = etc.) –Reversing the symbols in the !=, >= and and =< ) –Using the assignment operator ( = ) when the equality operator ( == ) is intended. Microsoft Visual C++ Studio 2010 does not warn you about this error! 4.5 if Selection Statement (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.5 if Selection Statement (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.7 if single-selection statement UML activity diagram. 4.5 if Selection Statement (Cont.) UML diamond symbol –Decision symbol Action / decision model of programming

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.6 if…else Selection Statement if…else statement –Performs if action if condition statement is true –Performs separate else action if condition statement is false Nested if…else statements –Can check for multiple conditions

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.8 if…else double-selection statement UML activity diagram. 4.6 if…else Selection Statement (Cont.) if ( grade >= 60 ) { cout << "Passed"; } else { cout << "Failed"; }

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.6 if…else Selection Statement (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.6 if…else Selection Statement (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.6 if…else Selection Statement (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.6 if…else Selection Statement (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.7Constructing the Wage Calculator Application

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.7Constructing the Wage Calculator Application (Cont.) Figure 4.13 main function (containing only a return statement). Initial main function

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.14 Assigning user input to variables. 4.7Constructing the Wage Calculator Application (Cont.) Define variables to store user input Prompt user for hourly rage and input result into the hourlyWage variable Prompt user for hours worked and input result into the hoursWorked variable

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Constants –Capitalize names to make them stand out –Variable that cannot be changed after definition –Defined by preceding data type with const keyword Figure 4.15 Creating a constant. 4.7Constructing the Wage Calculator Application (Cont.) Constant definition

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Primitive type double –Used to represent floating-point values Figure 4.16 Defining a variable of the double type. Define double variable wages 4.7Constructing the Wage Calculator Application (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.17 if…else statement that calculates gross wages. 4.7Constructing the Wage Calculator Application (Cont.) if…else statement to calculate wages Determine whether the hours worked were less than the hour limit

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.7Constructing the Wage Calculator Application (Cont.) Figure 4.18 Displaying gross wages. Displaying output

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.19 Updated application displaying unformatted number. Displaying unformatted number 4.7Constructing the Wage Calculator Application (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.8Assignment Operators

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.8Assignment Operators (Cont.) Addition assignment operator ( += ) makes it unnecessary to include the wages variable in both the left and right operands Figure 4.21 Using the addition assignment operator in a calculation. Addition assignment operator

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.9Formatting Numbers Stream manipulators –fixed Indicates floating-point values should be displayed in fixed-point notation –setprecision Indicates the number of digits displayed to the right of the decimal point Parameterized stream manipulators –Require header file –setprecision is a parameterized stream manipulator –Default precision 6 digits to the right of the decimal point Nonparameterized stream manipulators –fixed Does not require

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.9Formatting Numbers (Cont.) Using the setprecision manipulator without including the header file is a syntax error Figure 4.22 Including the header file. The header file declares the setprecision stream manipulator

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.9Formatting Numbers (Cont.) Figure 4.23 Using the fixed and setprecision stream manipulators to display the gross wages with two digits of precision to the right of the decimal point. The fixed and setprecision stream manipulators cause cout to display numeric values accurate to two decimal places This iostream manipulator statement is typically placed before the cout statement(s) for which it is intended.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.9Formatting Numbers (Cont.) Figure 4.24 Completed application displaying formatted wages. Displaying formatted number

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. WageCalculator.cpp (1 of 3) Define double variables to store fractional values

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. WageCalculator.cpp (2 of 3) The const keyword specifies that HOUR_LIMIT is a constant Variable to store gross wages Begin if…else statement

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. WageCalculator.cpp (3 of 3) Begin else part of if…else statement. else part executes when condition in line 33 evaluates to false Assign left operand the value of adding left and right operands End else part of if…else Format result as a dollar amount

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.10Using the Debugger: The Watch and Locals Windows Figure 4.26 Setting breakpoints at lines 23 and 36. Breakpoint at line 23 Margin indicator bar Breakpoint at line 36

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.27 Entering hourly wage before breakpoint is reached. 4.10Using the Debugger: The Watch and Locals Windows (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.28 Program execution suspended when debugger reaches the breakpoint at line Using the Debugger: The Watch and Locals Windows (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.10Using the Debugger: The Watch and Locals Windows (Cont.) The Locals window allows new values to be assigned to variables Uninitialized variables are assigned random values Figure 4.29 Examining variable hourlyWage. Value of the hourlyWage variable displayed

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.30 Examining the values of expressions. 4.10Using the Debugger: The Watch and Locals Windows (Cont.) Evaluating an arithmetic expression Evaluating a bool expression

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.10Using the Debugger: The Watch and Locals Windows (Cont.) Variables modified since the last breakpoint are displayed in red Figure 4.31 Displaying the value of the variables in the Wage Calculator application. Values of the hoursWorked and HOUR_LIMIT variables displayed in red

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Modifying values inside the Locals window Figure 4.32 Modifying values. Value modified in the debugger 4.10Using the Debugger: The Watch and Locals Windows (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.33 Setting a breakpoint at line 54 prevents the application from exiting immediately after displaying its result. Breakpoint at line Using the Debugger: The Watch and Locals Windows (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.10Using the Debugger: The Watch and Locals Windows (Cont.) Output reflects the modified values from the Locals window Figure 4.34 Output displayed after modifying the hoursWorked variable.