Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.

Similar presentations


Presentation on theme: "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing."— Presentation transcript:

1 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing Sentinel-Controlled Repetition Outline 7.1 Test-Driving the Class Average Application 7.2 Sentinel-Controlled Repetition 7.3 Creating the Class Average Application 7.4 Wrap-Up

2 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Use sentinel-controlled repetition to obtain an indefinite number of user inputs. –Convert values from one primitive C++ type to another. –Identify and prevent fatal logic errors.

3 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.1 Test-Driving the Class Average Application

4 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.1 Test-Driving the Class Average Application (Cont.) Figure 7.1 Running the completed Class Average application.

5 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.1 Test-Driving the Class Average Application (Cont.) Figure 7.2 Entering grades in the Class Average application.

6 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.1 Test-Driving the Class Average Application (Cont.) Figure 7.3 Error message displayed when no grades are entered.

7 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.1 Test-Driving the Class Average Application (Cont.) Figure 7.4 Class Average application after 10 grades and the sentinel value have been entered.

8 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.2 Sentinel-Controlled Repetition Sentinel-Controlled Repetition (also known as indefinite repetition) –Number of repetitions is not known before loop executes –Sentinel value (signal value, dummy value or flag value) Indicates that repetition should end Cannot be the same as an acceptable input value

9 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application

10 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Divide by zero –Usually results in a fatal logic error –Application could crash (bomb)

11 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Figure 7.6 Defining and initializing your application’s variables. Defining variables

12 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Figure 7.7 Getting the grade input. Prompting the user for and storing a grade

13 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Figure 7.8 Defining the while loop. while statement repeats until the sentinel value ( -1 ) is input

14 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Figure 7.9 Adding each grade to the total and incrementing the counter. Adding grade to total Incrementing gradeCount

15 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Figure 7.10 Prompting the user for and storing the next grade. Prompting the user for and storing the next grade

16 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Figure 7.11 Determining whether any grades were entered. Determining whether grades were entered Indicating that no grades were entered

17 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Implicit conversion (promotion) –Values are automatically converted to “larger” data types (types at top of the list in Fig. 7.12) when necessary –No loss of data Explicit conversion (casting) –Use unary cast operator static_cast –Prevents compiler warnings when converting to a “smaller” data type –Converting to a “smaller” data type may cause a loss of data

18 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.)

19 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Figure 7.13 Calculating the class average. Explicitly cast total to a double value to produce a floating-point result

20 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Figure 7.14 Formatting and displaying the class average. Format and display the average

21 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Creating the Class Average Application (Cont.) Figure 7.15 Completed Class Average application.

22 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ClassAverage.cpp (1 of 2) Define variables Prompt user for and store grade Repeat until the sentinel value is entered Add grade to the total and increment the grade counter

23 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ClassAverage.cpp (2 of 2) Prompt user for and store next grade Explicitly cast total to a double value to produce a floating-point result Format and display the class average If no grades have been entered, display error message and avoid division by zero


Download ppt "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing."

Similar presentations


Ads by Google