tutorial 13 – salary survey application: introducing one-dimensional arrays

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

Upload: saxon

Post on 04-Jan-2016

37 views

Category:

Documents


0 download

DESCRIPTION

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. Objectives. - PowerPoint PPT Presentation

TRANSCRIPT

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

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

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

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

13.1 Test-Driving the Salary Survey Application

Application Requirements A company pays its sales staff on a commission basis. Each member of the sales staff receives $200 per week, plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000, a total of $650. Write an application (using an array) to determine how many of the sales staff earned salaries in each of the following ranges (assuming that each person’s salary is truncated to an integer amount): $200–299, $300–399, $400–499, $500–599, $600–699, $700–799, $800–899, $900–999 and over $999.

© 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 nextsales figure.

Displaying the total salary

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

© 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

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

© 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

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

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 ];

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

• Use a const int to declare array sizes makes your code more clear and allows you to change it quickly in the future.

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

© 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

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

13.4 Constructing the Salary Survey Application

Prompt the user for the first sales figure While the sales figure is greater than or equal to zero Calculate the salary based on commission Increment the number of salaries within that range Prompt the user for the next sales figure Display the distribution of salaries

Figure 13.10 Pseudocode for the Salary Survey application.

© 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

© 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 ints.

Defining and initializing double variable sales

Declaring int array resultArray and initializing its elements to 0

© 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

© 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 thesalary.

Formatting floating-point values

© 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

© 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

© 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 thecompiler warning.

Explicitly converting the result to an int to prevent a warning

© 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

© 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

© 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

© 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

© 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

© 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

© 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

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

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

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

1 // Tutorial 13: SalarySurvey.cpp

2 // Application that takes information about employee salaries and

3 // uses an array to keep track of the number of employees in each

4 // salary range.

5 #include <iostream> // required to perform C++ stream I/O

6 #include <iomanip> // required for parameterized stream manipulators

7

8 using namespace std; // for accessing C++ Standard Library members

9

10 void displayTotals( int [] ); // function prototype

11

12 // function main begins program execution

13 int main()

14 {

15 // define variable and declare an array, initialize values to 0

16 double sales = 0; // sales that user inputs

17 int resultArray[ 11 ] = { 0 }; // count of salary ranges

18

19 // prompt user for and input sales

20 cout << "\nEnter sales (-1 to end): ";

21 cin >> sales; // get sales

SalarySurvey.cpp (1 of 4)

Declare the displayTotals function

Define and initialize double sales

Declare int array resultArray and initialize its elements to 0

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

22

23 // display floating-point values as currency

24 cout << fixed << setprecision( 2 );

25

26 // repeat until user enters sentinel value

27 while ( sales >= 0 )

28 {

29 // calculate salary and index

30 double salary = 200 + 0.09 * sales;

31 int index = static_cast< int >( salary / 100 );

32

33 // update statistics

34 if ( index >= 10 )

35 {

36 resultArray[ 10 ]++;

37 } // end if

38 else

39 {

40 resultArray[ index ]++;

41 } // end else

42

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

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

43 // display salary

44 cout << "Total salary: $" << salary << endl;

45

46 // prompt user for and input next sales value

47 cout << "\nEnter sales (-1 to end): ";

48 cin >> sales; // get sales

49 } // end while

50

51 // display salary ranges and totals

52 displayTotals( resultArray );

53

54 return 0; // indicates successful termination

55

56 } // end function main

57

58 // display salary ranges and totals

59 void displayTotals( int printArray[] )

60 {

61 // define local variables

62 int lowerBound; // current salary range's lower bound

63 int upperBound; // current salary range's upper bound

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

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

64

65 // display a table header

66 cout << "\nSalary Range:\tTotal:" << endl;

67

68 // iterate over each salary range between $200 and $999

69 for ( int i = 2; i <= 9; i++ )

70 {

71 lowerBound = i * 100; // set the current lower bound

72 upperBound = lowerBound + 99; // set the current upper bound

73

74 // display range and total number of salaries in that range

75 cout << "$" << lowerBound << "-" << upperBound << "\t"

76 << printArray[ i ] << endl;

77 } // end for

78

79 // display range and salary count for $1000 and greater

80 cout << "$1000+\t\t" << printArray[ 10 ] << endl << endl;

81

82 } // end function displayTotals

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