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 13 – Salary Survey Application: Introducing.

Similar presentations


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

1 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing One- Dimensional Arrays Outline 13.1 Test-Driving the Salary Survey Application 13.2 Introducing Arrays 13.3 Declaring and Initializing Arrays 13.4 Constructing the Salary Survey Application 13.5 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: –Create and initialize arrays to store groups of related values. –Store information in an array. –Access individual elements of an array.

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

4 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.1 Test-Driving the Salary Survey Application Figure 13.1 Running the completed Salary Survey application. Figure 13.2 Salary Survey application displaying a salary and prompting for the next sales figure. Displaying the total salary

5 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.1 Test-Driving the Salary Survey Application (Cont.) Figure 13.3 Entering several sales figures in the Salary Survey application. Figure 13.4 Displaying the distribution of salaries.

6 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.2 Introducing Arrays Arrays are groups of variables with the same type –Use the array name and an index (position number) to refer to individual elements –Indices range from 0 to one less than the number of elements –C++ will not prevent an application from attempting to access an element outside of an array’s valid range Attempting to access a nonexistent element may result in a logic error

7 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.2 Introducing Arrays (Cont.) Figure 13.5 Array unitsSold, consisting of 13 elements.

8 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.3 Declaring and Initializing Arrays Declare an array by using square brackets [] after the array name Array initializer lists –Are comma-separated lists enclosed by braces {} –If no array size is specified within the square brackets, the size of the initializer list is used –Using an initializer list with more elements than the array is a syntax error –Using an initializer list with fewer elements than the array causes unintialized elements to be set to 0

9 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.3Declaring and Initializing Arrays (Cont.) Array initializer lists Syntax error Initializing every element to 0 If no initializer is provided values will be random. int salesPerDay[ 13 ];

10 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.3 Declaring and Initializing Arrays (Cont.) Figure 13.6 Defining and initializing an array in main. Creating and initializing an array of int s Use a const int to declare array sizes makes your code more clear and allows you to change it quickly in the future.

11 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.3 Declaring and Initializing Arrays (Cont.) Figure 13.7 Summing the values of an array’s elements. Retrieving the value of each element and adding it to the total one at a time Figure 13.8 Displaying the sum of the values of an array’s elements.

12 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.3 Declaring and Initializing Arrays (Cont.) Figure 13.9 Completed Sum Array application output. Total value of array elements

13 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application

14 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.11 Declaring a function prototype for displayTotals. Declaring the displayTotals function prototype

15 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.12 Defining a variable to store sales and creating an array of int s. Defining and initializing double variable sales Declaring int array resultArray and initializing its elements to 0

16 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.13 Retrieving user input. Prompting the user for and inputting a salesperson’s total sales

17 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.14 Using the fixed and setprecision stream manipulators to display the salary. Formatting floating-point values

18 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.15 while statement repeats until user enters an invalid value. while statement will process user input and prompt the user for the next salary

19 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.16 Calculating salary. Calculating the salary and its corresponding index in resultArray Figure 13.17 Warning issued by the compiler when assigning a double value to an int variable. Warning due to possible loss of data Warnings do not prevent the application from compiling (syntax errors), but might indicate logic errors

20 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.18 Explicitly converting the floating-point result to an int to prevent the compiler warning. Explicitly converting the result to an int to prevent a warning

21 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.19 Updating the count of salaries in resultArray. Incrementing the appropriate element of resultArray

22 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.20 Displaying the salary and prompting the user for the next sales figure. Prompting user for next sales value

23 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.21 Passing resultArray by reference to the displayTotals function. Using pass-be-reference when passing resultArray to displayTotal Arrays are passed-by-reference Arguments that are passed-by-reference give the callee direct access to the argument in the caller

24 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.22 Defining the displayTotals function. displayTotals function definition

25 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.23 Defining variables and displaying a table header in displayTotals. Defining local variables to store the upper and lower bounds for each salary range Displaying a table header

26 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.24 Displaying the number of salaries in each salary range. for statement varies i from 2 to 9 Assigning the upper and lower bounds for the current iteration Displaying the salary range and number of salaries in that range

27 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.25 Display the range and salary count for $1000 and greater. Displaying the total for the final salary range

28 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.26 Sample input and output for the Salary Survey application.

29 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.27 Total of salaries in each range displayed upon exiting.

30 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. SalarySurvey.cpp (1 of 4) Declare the displayTotals function Define and initialize double sales Declare int array resultArray and initialize its elements to 0

31 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. SalarySurvey.cpp (2 of 4) while statement processes user input and prompts for the next salary Calculate the salary and its corresponding index in resultArray Increment the appropriate element of resultArray

32 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. SalarySurvey.cpp (3 of 4) Use pass-by-reference when passing resultArray to displayTotals Define local variables to store the upper and lower bounds for each salary range

33 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. SalarySurvey.cpp (4 of 4) Display a table header for header varies i from 2 to 9 Assign the lower and upper bounds for the current iteration Display the salary range and number of salaries in that range Display the count for the final salary range


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

Similar presentations


Ads by Google