 Download Presentation Tutorial 13 – Salary Survey Application: Introducing One-Dimensional Arrays

# Tutorial 13 – Salary Survey Application: Introducing One-Dimensional Arrays - PowerPoint PPT Presentation Download Presentation ## Tutorial 13 – Salary Survey Application: Introducing One-Dimensional Arrays

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Tutorial 13 – Salary Survey Application: Introducing One-Dimensional Arrays Outline13.1 Test-Driving the Salary Survey Application13.2 Introducing Arrays13.3 Declaring and Initializing Arrays13.4 Constructing the Salary Survey Application13.5 Wrap-Up

2. 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. 13.1 Test-Driving the Salary Survey Application

4. Displaying the total salary 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.

5. 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. 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. 13.2 Introducing Arrays (Cont.) Figure 13.5　 Array unitsSold, consisting of 13 elements.

8. 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. 13.3 Declaring 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. Creating and initializing an array of ints 13.3 Declaring and Initializing Arrays (Cont.) Figure 13.6　Defining and initializing an array in main. • Use a constint to declare array sizes makes your code more clear and allows you to change it quickly in the future.

11. Retrieving the value of each element and adding it to the total one at a time 13.3 Declaring and Initializing Arrays (Cont.) Figure 13.7　Summing the values of an array’s elements. Figure 13.8　Displaying the sum of the values of an array’s elements.

12. Total value of array elements 13.3 Declaring and Initializing Arrays (Cont.) Figure 13.9　Completed Sum Array application output.

13. 13.4 Constructing the Salary Survey Application

14. Declaring the displayTotals function prototype 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.11　Declaring a function prototype for displayTotals.

15. Defining and initializing double variable sales Declaring int array resultArray and initializing its elements to 0 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.12　Defining a variable to store sales and creating an array of ints.

16. Prompting the user for and inputting a salesperson’s total sales 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.13　Retrieving user input.

17. Formatting floating-point values 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.14　Using the fixed and setprecision stream manipulators to display the salary.

18. while statement will process user input and prompt the user for the next salary 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.15　while statement repeats until user enters an invalid value.

19. Warning due to possible loss of data Calculating the salary and its corresponding index inresultArray 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.16　Calculating salary. Figure 13.17　Warning issued by the compiler when assigning a double value to an int variable. • Warnings do not prevent the application from compiling (syntax errors), but might indicate logic errors

20. Explicitly converting the result to an int to prevent a warning 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.

21. Incrementing the appropriate element of resultArray 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.19　Updating the count of salaries in resultArray.

22. Prompting user for next sales value 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.20　Displaying the salary and prompting the user for the next sales figure.

23. Using pass-be-reference when passing resultArray to displayTotal 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.21　Passing resultArray by reference to the displayTotals function. • Arrays are passed-by-reference • Arguments that are passed-by-reference give the callee direct access to the argument in the caller

24. displayTotals function definition 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.22　Defining the displayTotals function.

25. Defining local variables to store the upper and lower bounds for each salary range Displaying a table header 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.23　Defining variables and displaying a table header in displayTotals.

26. for statement varies i from 2 to 9 Displaying the salary range and number of salaries in that range Assigning the upper and lower bounds for the current iteration 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.24　Displaying the number of salaries in each salary range.

27. Displaying the total for the final salary range 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.25　Display the range and salary count for \$1000 and greater.

28. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.26　Sample input and output for the Salary Survey application.

29. 13.4 Constructing the Salary Survey Application (Cont.) Figure 13.27　Total of salaries in each range displayed upon exiting.

30. Declare the displayTotals function Define and initialize double sales Declare int array resultArray and initialize its elements to 0 SalarySurvey.cpp (1 of 4)

31. while statement processes user input and prompts for the next salary Calculate the salary and its corresponding index in resultArray Increment the appropriate element ofresultArray SalarySurvey.cpp (2 of 4)

32. Use pass-by-reference when passing resultArray to displayTotals Define local variables to store the upper and lower bounds for each salary range SalarySurvey.cpp (3 of 4)

33. 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 SalarySurvey.cpp (4 of 4)