Tutorial 4 Wage Calculator Application Introducing Algorithms Pseudocode















































- Slides: 47

Tutorial 4 – Wage Calculator Application: Introducing Algorithms, Pseudocode and Program Control Outline 4. 1 4. 2 4. 3 4. 4 4. 5 4. 6 4. 7 4. 8 4. 9 4. 10 4. 11 Test-Driving the Wage Calculator Application Algorithms Pseudocode Control Statements if Selection Statement if…else Selection Statement Constructing the Wage Calculator Application Assignment Operators Formatting Numbers Using the Debugger: The Watch and Locals Windows 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. 1 Test Driving the Wage Calculator Application © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

4. 1 Figure 4. 1 Test Driving the Wage Calculator Application (Cont. ) Running the Wage Calculator application. © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

4. 1 Figure 4. 2 Test Driving the Wage Calculator Application (Cont. ) Entering data in the Wage Calculator application. • Enter 10 at the Enter hourly wage: prompt. © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

4. 1 Figure 4. 3 Test Driving the Wage Calculator Application (Cont. ) Displaying calculated wages. • Enter 45 at the Enter hours worked this week: prompt © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

4. 1 Figure 4. 4 Test Driving the Wage Calculator Application (Cont. ) Displaying calculated wages. • Inputting floating-point values and returning a result © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

4. 2 Algorithms • 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 © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

4. 3 Pseudocode • Pseudocode – Informal language to develop algorithms – Resembles everyday English • Not a programming language – Only describes executable statements • Example Assign 0 to the counter is equal to: counter = 0; © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

4. 4 Control Statements • Sequence, selection, repetition – Sequence • Executed one after another in the order in which they are written – Selection • if, if…else, switch – if: single selection statement – if…else: double selection statement – switch: multiple selection statement – Repetition • while, do…while, for • Single Entry / Single Exit – Stacking / nesting © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

4. 4 Control 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.

4. 4 Figure 4. 5 Control Statements (Cont. ) Sequence statement activity diagram. • UML symbols – Action state symbols, diamonds, small circles, transition arrows • UML notes – Similar to comments © 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 • 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.

4. 5 if Selection Statement (Cont. ) • 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 ==, !=, >=, <= (such as = =, > = etc. ) – Reversing the symbols in the !=, >= and <= is a syntax error (such as =!, => and =<) – Using the assignment operator (=) when the equality operator (==) is intended © 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.

4. 5 Figure 4. 7 if Selection Statement (Cont. ) if single-selection statement UML activity diagram. • 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.

4. 6 Figure 4. 8 if…else Selection Statement (Cont. ) if…else double-selection statement UML activity diagram. © 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. 7 Constructing the Wage Calculator Application © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

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

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

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

4. 7 Figure 4. 17 Constructing the Wage Calculator Application (Cont. ) if…else statement that calculates gross wages. 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. 7 Figure 4. 18 Constructing the Wage Calculator Application (Cont. ) Displaying gross wages. Displaying output © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

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

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

4. 9 Formatting 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 <iomanip> 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 <iomanip> © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

4. 9 Formatting 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 © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Wage. Calculator. 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.

Wage. Calculator. 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.

End else part of if…else Wage. Calculator. cpp (3 of 3) Format result as a dollar amount 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 © Copyright 1992– 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

4. 10 Using 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.

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

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

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

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

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

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

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

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