Upcoming SlideShare
×

# Chapter 7

1,984 views
1,888 views

Published on

C++ - 7 edition - Solution

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,984
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
68
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Chapter 7

1. 1. Arrays and Vectors:Solutions 7 Now go, write it before them in a table, and note it in a book. —Isaiah 30:8 Begin at the beginning, … and go on till you come to the end: then stop. —Lewis Carroll To go beyond is as wrong as to fall short. —Confucius Objectives In this chapter you’ll learn: ■ To use the array data structure to represent a set of related data items. ■ To use arrays to store, sort and search lists and tables of values. ■ To declare arrays, initialize arrays and refer to the individual elements of arrays. ■ To pass arrays to functions. ■ Basic searching and sorting techniques. ■ To declare and manipulate multidimensional arrays. ■ To use C++ Standard Library class template vector. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
2. 2. 2 Chapter 7 Arrays and Vectors: Solutions Student Solution Exercises 7.10 (Salesperson Salary Ranges) Use a one-dimensional array to solve the following problem. A company pays its salespeople on a commission basis. The salespeople each receive \$200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who grosses \$5000 in sales in a week receives \$200 plus 9 percent of \$5000, or a total of \$650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the follow- ing ranges (assume that each salesperson’s salary is truncated to an integer amount): a) \$200–299 b) \$300–399 c) \$400–499 d) \$500–599 e) \$600–699 f) \$700–799 g) \$800–899 h) \$900–999 i) \$1000 and over ANS: 1 // Exercise 7.10 Solution: Ex07_10.cpp 2 #include <iostream> 3 #include <iomanip> 4 using namespace std; 5 6 void wages( int [] ); // function prototype 7 void display( const int [] ); // function prototype 8 9 int main()10 {11 int salaries[ 11 ] = {}; // array to hold salaries1213 cout << fixed << showpoint;14 wages( salaries ); // calculate wages15 display( salaries ); // display ranges of wages16 } // end main1718 // function that asks user to input gross sales19 // and calculates employee salary based on input20 void wages( int money[] )21 {22 double sales; // holds employee gross sales23 double i = 0.09; // 9%, used for calculating salary2425 // prompt user for gross sales and store it in sales26 cout << "Enter employee gross sales (-1 to end): ";27 cin >> sales;2829 // calculate salary based on sales30 // and prompt user for another employee sales amount31 while ( sales != -1 )32 {33 double salary = 200.0 + sales * i;34 cout << setprecision( 2 ) << "Employee Commission is \$" © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
3. 3. Student Solution Exercises 335 << salary << n;3637 int x = static_cast< int > ( salary ) / 100;38 money[ ( x < 10 ? x : 10 ) ]++;3940 cout << "nEnter employee gross sales (-1 to end): ";41 cin >> sales;42 } // end while43 } // end function wages4445 // function that displays table of salary ranges46 // and number of employees in each range47 void display( const int dollars[] )48 {49 // display table of ranges and employees in each range50 cout << "Employees in the range:";5152 for ( int i = 2; i < 10; i++ )53 cout << "n\$" << i << "00-\$" << i << "99 : " << dollars[ i ];5455 cout << "nOver \$1000: " << dollars[ 10 ] << endl;56 } // end function displayEnter employee gross sales (-1 to end): 10000Employee Commission is \$1100.00Enter employee gross sales (-1 to end): 4235Employee Commission is \$581.15Enter employee gross sales (-1 to end): 600Employee Commission is \$254.00Enter employee gross sales (-1 to end): 12500Employee Commission is \$1325.00Enter employee gross sales (-1 to end): -1Employees in the range:\$200-\$299 : 1\$300-\$399 : 0\$400-\$499 : 0\$500-\$599 : 1\$600-\$699 : 0\$700-\$799 : 0\$800-\$899 : 0\$900-\$999 : 0Over \$1000: 2 7.13 Write single statements that perform the following one-dimensional array operations: a) Initialize the 10 elements of integer array counts to zero. ANS: int counts[ 10 ] = {}; b) Add 1 to each of the 15 elements of integer array bonus. ANS: © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
5. 5. Student Solution Exercises 533 // if number is not a duplicate, enter it in array34 if ( !duplicate )35 {36 a[ subscript++ ] = value;37 ++i;38 } // end if39 else40 cout << "Duplicate number.n";41 } // end if42 else43 cout << "Invalid number.n";44 } // end for4546 cout << "nThe nonduplicate values are:n";4748 // display array of nonduplicates49 for ( int i = 0; i < SIZE; i++ )50 cout << a[ i ] << ;5152 cout << endl;53 } // end mainEnter 20 integers between 10 and 100:105Invalid number.2030405060708090100110Invalid number.10Duplicate number.11223344556677889945The nonduplicate values are:10 20 30 40 50 60 70 80 90 100 11 22 33 44 55 66 77 88 99 45 © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.