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 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.
13.1 Test-Driving the Salary Survey Application Applic a tion Req uirements 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.
13.1 Test-Driving the Salary Survey Application Figure 13.1 Running the completed Salary Survey application. Figure 13.2 sales figure. Salary Survey application displaying a salary and prompting for the next Displaying the total salary
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.
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
13.2 Introducing Arrays (Cont.) Figure 13.5 Array unitssold, consisting of 13 elements.
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
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 ];
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.
Figure 13.7 13.3 Declaring and Initializing Arrays (Cont.) 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.
13.3 Declaring and Initializing Arrays (Cont.) Figure 13.9 Completed Sum Array application output. Total value of array elements
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 Pseud oc od e for the Salary Survey a pplic ation.
13.4 Constructing the Salary Survey Application (Cont.) Figure 13.11 Declaring a function prototype for displaytotals. Declaring the displaytotals function prototype
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
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
13.4 Constructing the Salary Survey Application (Cont.) Figure 13.14 salary. Using the fixed and setprecision stream manipulators to display the Formatting floating-point values
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
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
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
13.4 Constructing the Salary Survey Application (Cont.) Figure 13.19 Updating the count of salaries in resultarray. Incrementing the appropriate element of resultarray
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
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
13.4 Constructing the Salary Survey Application (Cont.) Figure 13.22 displaytotals function definition Defining the displaytotals function.
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
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
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
13.4 Constructing the Salary Survey Application (Cont.) Figure 13.26 Sample input and output for the Salary Survey application.
13.4 Constructing the Salary Survey Application (Cont.) Figure 13.27 Total of salaries in each range displayed upon exiting.
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
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 while statement processes user input and prompts for the next salary Increment the appropriate element of resultarray SalarySurvey.cpp (2 of 4) Calculate the salary and its corresponding index in resultarray
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
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 Display a table header for header varies i from 2 to 9 SalarySurvey.cpp (4 of 4) 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