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. Figure 7.2 Entering grades in the 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.3 Error message displayed when no grades are entered. Figure 7.4 Class Average application after 10 grades and the sentinel value have been entered.

6 © 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 (also known as specific value, signal value, dummy value or flag value) Indicates that repetition should end Cannot be the same as an acceptable input value

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

8 © 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)

9 © 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 Figure 7.7 Getting the grade input. Prompting the user for and storing a grade

10 © 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 Figure 7.9 Adding each grade to the total and incrementing the counter. Adding grade to total Incrementing gradeCount

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.10 Prompting the user for and storing the next grade. Prompting the user for and storing the next grade Determining whether grades were entered Indicating that no grades were entered Figure 7.11 Determining whether any grades were entered.

12 © 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 (higher on the list) when necessary - Week 04a Fig. 7.12 –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 In the following example we cast an integer (lower) to a double (higher) in order to avoid round-off error. Notice this is the opposite direction from what has been previously discussed for casting.

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.13 Calculating the class average. Explicitly cast total to a double value to produce a floating-point result Integer division yields an integer result. Here we want a more accurate answer so we cast the numerator to a floating-point number

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.14 Formatting and displaying the class average. Format and display the average Figure 7.15 Completed Class Average application.

15 © 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

16 © 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

17 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 6 − Car Payment Calculator Application. Turn in annotated source file with your own comments. Answer and Turn-in Tutorial 6 Questions 6.1 to 6.5 and Tutorial 7 Questions 7.1 to 7.5. Always write the question followed by the answer. Remember to highlight the answer. Exercises 6.11, 6.12, and the Programming Challenge 6.17. Due next Wednesday


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