Title: Chapter 7
1Chapter 7 Arrays
27.1 Arrays Hold Multiple values
- Unlike regular variables, arrays can hold
multiple values.
3Figure 7-1
int count
Enough memory for 1 int
12345
float price
Enough memory for 1 float
56.981
char letter
Enough memory for 1 char
A
4Figure 7-2
5Table 7-1
67.2 Accessing Array elements
- The individual elements of an array are assigned
unique subscripts. These subscripts are used to
access the elements.
7Program 7-1
- // This program asks the user for the number of
hours worked - // by 6 employees. It uses a 6-element int array
to store the - // values.
- include ltiostream.hgt
- void main(void)
-
- short hours6
- cout ltlt "Enter the hours worked by six
employees " - cin gtgt hours0
- cin gtgt hours1
- cin gtgt hours2
- cin gtgt hours3
8Program continues
- cin gtgt hours4
- cin gtgt hours5
- cout ltlt "The hours you entered are"
- cout ltlt " " ltlt hours0
- cout ltlt " " ltlt hours1
- cout ltlt " " ltlt hours2
- cout ltlt " " ltlt hours3
- cout ltlt " " ltlt hours4
- cout ltlt " " ltlt hours5 ltlt endl
-
9Program Output with Example Input
- Enter the hours worked by six employees 20 12 40
30 30 15 Enter - The hours you entered are 20 12 40 30 30 15
10Figure 7-7
11Program 7-2
- // This program asks the user for the number of
hours worked - // by 6 employees. It uses a 6-element short
array to store the - // values.
- include ltiostream.hgt
- void main(void)
-
- short hours6
-
- cout ltlt "Enter the hours worked by six
employees " - for (int count 0 count lt 6 count)
- cin gtgt hourscount
- cout ltlt "The hours you entered are"
- for (count 0 count lt 6 count)
- cout ltlt " " ltlt hourscount
- cout ltlt endl
-
12Program Output with Example Input
- Enter the hours worked by six employees 20 12 40
30 30 15 Enter - The hours you entered are 20 12 40 30 30 15
13Program 7-3
- // This program asks the user for the number of
hours worked - // by 6 employees. It uses a 6-element short
array to store the - // values.
- includeltiostream.hgt
- void main(void)
-
- short hours6
- cout ltlt "Enter the hours worked by six
employees.\n" - for (int count 1 count lt 6 count)
-
- cout ltlt "Employee " ltlt count ltlt " "
- cin gtgt hourscount - 1
-
- cout ltlt "The hours you entered are\n"
14Program continues
- for (count 1 count lt 6 count)
-
- cout ltlt "Employee " ltlt count ltlt " "
- cout ltlt hourscount - 1 ltlt endl
-
-
15Program Output with Example Input
- Enter the hours worked by six employees.
- Employee 1 20 Enter
- Employee 2 12 Enter
- Employee 3 40 Enter
- Employee 4 30 Enter
- Employee 5 30 Enter
- Employee 6 15 Enter
- The hours you entered are
- Employee 1 20
- Employee 2 12
- Employee 3 40
- Employee 4 30
- Employee 5 30
- Employee 6 15
167.3 No Bounds Checking in C
- C gives you the freedom to store data past an
arrays boundaries.
17Program 7-4
- // This program unsafely accesses an area of
memory by writing - // values beyond an array's boundary.
- // WARNING If you compile and run this program,
it could cause - // the computer to crash.
- include ltiostream.hgt
- void main(void)
-
- short values3 // An array of 3 short
integers. - cout ltlt "I will store 5 numbers in a 3 element
array!\n" - for (int count 0 count lt 5 count)
- valuescount 100
- cout ltlt "If you see this message, it means the
computer\n" - cout ltlt "has not crashed! Here are the
numbers\n" - for (int count 0 count lt 5 count)
- cout ltlt valuescount ltlt endl
-
18Figure 7-8
197.4 Array Initialization
- Arrays may be initialized when they are declared.
20Program 7-5
- // This program displays the number of days in
each month. - // It uses a 12-element int array.
- include ltiostream.hgt
- void main(void)
-
- int days12
- days0 31 // January
- days1 28 // February
- days2 31 // March
- days3 30 // April
- days4 31 // May
- days5 30 // June
- days6 31 // July
21Program continues
- days7 31 // August
- days8 30 // September
- days9 31 // October
- days10 30 // November
- days11 31 // December
- for (int count 0 count lt 12 count)
-
- cout ltlt "Month " ltlt (count 1) ltlt " has "
- cout ltlt dayscount ltlt " days.\n"
-
-
22Program Output
- Month 1 has 31 days.
- Month 2 has 28 days.
- Month 3 has 31 days.
- Month 4 has 30 days.
- Month 5 has 31 days.
- Month 6 has 30 days.
- Month 7 has 31 days.
- Month 8 has 31 days.
- Month 9 has 30 days.
- Month 10 has 31 days.
- Month 11 has 30 days.
- Month 12 has 31 days.
23Program 7-6
- // This program displays the number of days in
each month. - // It uses a 12-element int array.
- include ltiostream.hgt
- void main(void)
-
- int days12 31, 28, 31, 30,
- 31, 30, 31, 31,
- 30, 31, 30, 31
- for (int count 0 count lt 12 count)
-
- cout ltlt "Month " ltlt (count 1) ltlt " has "
- cout ltlt dayscount ltlt " days.\n"
-
-
24Program Output
- Month 1 has 31 days.
- Month 2 has 28 days.
- Month 3 has 31 days.
- Month 4 has 30 days.
- Month 5 has 31 days.
- Month 6 has 30 days.
- Month 7 has 31 days.
- Month 8 has 31 days.
- Month 9 has 30 days.
- Month 10 has 31 days.
- Month 11 has 30 days.
- Month 12 has 31 days.
25Program 7-7
- // This program uses an array of ten characters
to store the - // first ten letters of the alphabet. The ASCII
codes of the - // characters are displayed.
- include ltiostream.hgt
- void main(void)
-
- char letters10 'A', 'B', 'C', 'D', 'E',
- 'F', 'G', 'H', 'I', 'J'
- cout ltlt "Character" ltlt "\t" ltlt "ASCII Code\n"
- cout ltlt "--------" ltlt "\t" ltlt "----------\n"
- for (int count 0 count lt 10 count)
-
- cout ltlt letterscount ltlt "\t\t"
- cout ltlt int(letterscount) ltlt endl
-
-
26Program Output
- Character ASCII Code
- --------- ----------
- A 65
- B 66
- C 67
- D 68
- E 69
- F 70
- G 71
- H 72
- I 73
- J 74
27Partial Array Initialization
- When an array is being initialized, C does not
require a value for every element. - int numbers7 1, 2, 4, 8
28Program 7-8
- // This program has a partially initialized
array. - include ltiostream.hgt
- void main(void)
-
- int numbers7 1, 2, 4, 8 // Initialize the
- // first 4
elements. - cout ltlt "Here are the contents of the array\n"
- for (int index 0 index lt 7 index)
- cout ltlt numbersindex ltlt endl
-
29Program Output
- Here are the contents of the array
- 1
- 2
- 4
- 8
- 0
- 0
- 0
30Implicit Array Sizing
- It is possible to declare an array without
specifying its size, as long as you provide an
initialization list. - float ratings 1.0, 1.5, 2.0, 2.5, 3.0
31Initializing With Strings
- When initializing a character array with a
string, simply enclose the string in quotation
marks - char name Warren
32Figure 7-11
33Program 7-9
- // This program displays the contents of two char
arrays. - include ltiostream.hgt
- void main(void)
-
- char name1 "Holly"
- char name2 'W', 'a', 'r', 'r', 'e', 'n',
'\0' - cout ltlt name1 ltlt endl
- cout ltlt name2 ltlt endl
-
34Program Output
357.5 Processing Array Contents
- Individual array elements are processed like any
other type of variable.
36Program 7-10
- // This program stores, in an array, the hours
worked by 5 - // employees who all make the same hourly wage.
- include ltiostream.hgt
- void main(void)
-
- int hours5
- float payRate
- cout ltlt "Enter the hours worked by 5 employees
who all\n" - cout ltlt "earn the same hourly rate.\n"
- for (int index 0 index lt 5 index)
-
- cout ltlt "Employee " ltlt (index 1) ltlt " "
- cin gtgt hoursindex
-
37Program continues
- cout ltlt "Enter the hourly pay rate for all the
employees " - cin gtgt payRate
- cout ltlt "Here is the gross pay for each
employee\n" - cout.precision(2)
- cout.setf(iosfixed iosshowpoint)
- for (index 0 index lt 5 index)
-
- float grossPay hoursindex payRate
- cout ltlt "Employee " ltlt (index 1)
- cout ltlt " " ltlt grossPay ltlt endl
-
-
-
38Program Output with Example Input
- Enter the hours worked by 5 employees who all
- earn the same hourly rate.
- Employee 1 5 Enter
- Employee 2 10 Enter
- Employee 3 15 Enter
- Employee 4 20 Enter
- Employee 5 40 Enter
- Enter the hourly pay rate for all the employees
12.75 Enter - Here is the gross pay for each employee
- Employee 1 63.75
- Employee 2 127.50
- Employee 3 191.25
- Employee 4 255.00
- Employee 5 510.00
39Program 7-11
- // This program stores, in an array, the hours
worked by 5 - // employees who all make the same hourly wage.
It then - // displays the gross pay, including any
overtime. - include ltiostream.hgt
- // Constant for defining the array size
- void main(void)
-
- int hours5
- float payRate
- cout ltlt "Enter the hours worked by 5 employees
who all\n" - cout ltlt "earn the same hourly rate.\n"
- for (int index 0 index lt 5 index)
-
- cout ltlt "Employee " ltlt (index 1) ltlt " "
- cin gtgt hoursindex
40Program continues
- cout ltlt "Enter the hourly pay rate for all the
employees " - cin gtgt payRate
- cout ltlt "Here is the gross pay for each
employee\n" - cout.precision(2)
- cout.setf(iosfixed iosshowpoint)
- for (index 0 index lt 5 index)
-
- float grossPay, overTime
- if (hoursindex gt 40)
-
- // Calculate pay for 40 hours.
- grossPay 40 payRate
- // Calculate overtime pay.
- overTime (hoursindex - 40) 1.5
payRate - // Add regular pay and overtime pay.
- grossPay overTime
-
41Program continues
- else
- grossPay hoursindex payRate
- cout ltlt "Employee " ltlt (index 1)
- cout ltlt " " ltlt grossPay ltlt endl
-
-
-
42Program Output with Example Input
- Enter the hours worked by 5 employees who all
- earn the same hourly rate.
- Employee 1 10 Enter
- Employee 2 20 Enter
- Employee 3 50 Enter
- Employee 4 40 Enter
- Employee 5 60 Enter
- Enter the hourly pay rate for all the employees
12.75 Enter - Here is the gross pay for each employee
- Employee 1 127.50
- Employee 2 255.00
- Employee 3 701.25
- Employee 4 510.00
- Employee 5 892.50
437.6 Focus on Software Engineering Parallel
Arrays
- By using he same subscript, you can build
relationships between data stored in two or more
arrays.
44Program 7-12
- // This program stores, in two arrays, the hours
worked by 5 - // employees, and their hourly pay rates.
- include ltiostream.hgt
- // Constant for defining the array size
- const int numEmps 5
- void main(void)
-
- int hoursnumEmps
- float payRatenumEmps
- cout ltlt "Enter the hours worked by ltlt numEmps
- ltlt employees and their\n"
- cout ltlt "hourly rates.\n"
- for (int index 0 index lt numEmps index)
-
- cout ltlt "hours worked by employee " ltlt (index
1) - cout ltlt " "
45Program continues
- cin gtgt hoursindex
- cout ltlt "Hourly pay rate for employee "
- cout ltlt (index 1) ltlt " "
- cin gtgt payRateindex
-
- cout ltlt "Here is the gross pay for each
employee\n" - cout.precision(2)
- cout.setf(iosfixed iosshowpoint)
- for (index 0 index lt numEmps index)
-
- float grossPay hoursindex payRateindex
- cout ltlt "Employee " ltlt (index 1)
- cout ltlt " " ltlt grossPay ltlt endl
-
-
46Program Output with Example Input
- Enter the hours worked by 5 employees and their
hourly rates. - hours worked by employee 1 10 Enter
- Hourly pay rate for employee 1 9.75 Enter
- hours worked by employee 2 15 Enter
- Hourly pay rate for employee 2 8.62 Enter
- hours worked by employee 3 20 Enter
- Hourly pay rate for employee 3 10.50 Enter
- hours worked by employee 4 40 Enter
- Hourly pay rate for employee 4 18.75 Enter
- hours worked by employee 5 40 Enter
- Hourly pay rate for employee 5 15.65 Enter
- Here is the gross pay for each employee
- Employee 1 97.50
- Employee 2 129.30
- Employee 3 210.00
477.7 Thou Shalt Not Assign
- You cannot use the assignment operator to copy
one arrays contents to another. - for (int count0 count lt 4 count)
- newValcount oldValcount
48Table 7-2
497.8 Printing the Contents of an Array
- To display the contents of an array, you must use
a loop to display the contents of each
element.int array5 10, 20, 30, 40, 50
for (int count 0 count lt 5 count) cout
ltlt arraycount ltlt endl
507.9 Arrays As Function Arguments
- To pass an array as an argument to a function,
pass the name of the array.
51Program 7-13
- // This program demonstrates that an array
element is passed - // to a function like any other variable.
- include ltiostream.hgt
- void ShowValue(int) // Function prototype
- void main(void)
-
- int collection8 5, 10, 15, 20, 25, 30, 35,
40 - for (int Cycle 0 Cycle lt 8 Cycle)
- ShowValue(collectionCycle)
-
52Program continues
- //
- // Definition of function showValue.
- // This function accepts an integer argument.
- // The value of the argument is displayed.
- //
- void ShowValue(int Num)
-
- cout ltlt Num ltlt " "
-
53Program Output
54Program 7-14
- // This program demonstrates an array being
passed to a function. - include ltiostream.hgt
- void showValues(int ) // Function prototype
- void main(void)
-
- int collection8 5, 10, 15, 20, 25, 30, 35,
40 - showValues(collection) // Passing address of
array collection -
- //
- // Definition of function showValues.
- // This function accepts an array of 8 integers
- // as its argument. The contents of the array
- // is displayed.
- //
- void showValues(int nums)
55Program Output
56Program 7-15
- // This program demonstrates an array being
passed to a function. - include ltiostream.hgt
- void showValues(int ) // Function prototype
- void main(void)
-
- int set18 5, 10, 15, 20, 25, 30, 35, 40
- int set28 2, 4, 6, 8, 10, 12, 14, 16
- showValues(set1)
- cout ltlt endl
- showValues(set2)
-
- //
- // Definition of function showValues.
- // This function accepts an array of 8 integers
- // as its argument. The contents of the array
- // is displayed.
57Program Output
- 5 10 15 20 25 30 35 40
- 2 4 6 8 10 12 14 16
58Program 7-16
- // This program uses a function that can display
the contents - // of an integer array of any size.
- include ltiostream.hgt
- void showValues(int , int) // Function
prototype - void main(void)
-
- int set18 5, 10, 15, 20, 25, 30, 35, 40
- int set24 2, 4, 6, 8
- int set312 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12 - showValues(set1, 8)
- cout ltlt endl
- showValues(set2, 4)
- cout ltlt endl
- showValues(set3, 12)
59Program continues
- //
- // Definition of function showValues.
- // This function displays the contents of the
- // array passed into nums. The value passed
- // into elements is the number of elements in
- // the nums array.
- //
- void showValues(int nums, int elements)
-
- for (int index 0 index lt elements index)
- cout ltlt numsindex ltlt " "
-
60Program Output
- 5 10 15 20 25 30 35 40
- 2 4 6 8
- 1 2 3 4 5 6 7 8 9 10 11 12
61Program 7-17
- // This program uses a function that doubles the
contents of - // the elements within an array.
- include ltiostream.hgt
- void doubleArray(int , int) // Function
prototype - const int arraySize 12
- void main(void)
-
- int setarraySize 1, 2, 3, 4, 5, 6,
- 7, 8, 9, 10, 11, 12
- cout ltlt "The arrays values are\n"
- for (int index 0 index lt arraySize index)
- cout ltlt setindex ltlt " "
- cout ltlt endl
- doubleArray(set, arraySize)
- cout ltlt "After calling doubleArray, the values
are\n"
62Program continues
- for (int index 0 index lt arraySize index)
- cout ltlt setindex ltlt " "
- cout ltlt endl
-
- //
- // Definition of function doubleArray.
- // This function doubles the value of each
element - // in the array passed into nums.
- // The value passed into size is the number of
- // elements in the nums array.
- //
- void doubleArray(int nums, int size)
-
- for (int index 0 index lt size index)
- numsindex 2
-
63Program Output
- The array values are
- 1 2 3 4 5 6 7 8 9 10 11 12
- After calling doubleArray, the values are
- 2 4 6 8 10 12 14 16 18 20 22 24
647.10 Two-dimensional Arrays
- A two-dimensional array is like several identical
arrays put together. It is useful for storing
multiple sets of data.
65Program 7-18
- // This program demonstrates a two-dimensional
array. - include ltiostream.hgt
- void main(void)
-
- float sales34 // 2D array, 3 rows and 4
columns. - float totalSales 0 // To hold the total
sales. - int dir, qtr // Loop counters.
66Program continues
- cout ltlt "This program will calculate the total
sales of\n" - cout ltlt "all the company's divisions.\n"
- cout ltlt "Enter the following sales
information\n\n" - // Nested loops to fill the array with quarterly
- // sales figures for each division.
- for (div 0 div lt 3 div)
-
- for (qtr 0 qtr lt 4 qtr)
-
- cout ltlt "Division " ltlt (div 1)
- cout ltlt ", Quarter " ltlt (qtr 1) ltlt " "
- cin gtgt salesdivqtr
-
- cout ltlt endl // Print blank line.
-
67Program continues
- // Nested loops to add all the elements.
- for (div 0 div lt 3 div)
- for (qtr 0 qtr lt 4 qtr)
- totalSales salesdivqtr
- cout.precision(2)
- cout.setf(iosfixed iosshowpoint)
- cout ltlt "The total sales for the company are
" - cout ltlt totalSales ltlt endl
-
68Program Output with Example Input
- This program will calculate the total sales of
- all the company's divisions.
- Enter the following sales information
- Division 1, Quarter 1 31569.45 Enter
- Division 1, Quarter 2 29654.23 Enter
- Division 1, Quarter 3 32982.54 Enter
- Division 1, Quarter 4 39651.21 Enter
-
- Division 2, Quarter 1 56321.02 Enter
- Division 2, Quarter 2 54128.63 Enter
- Division 2, Quarter 3 41235.85 Enter
- Division 2, Quarter 4 54652.33 Enter
69Output continues
- Division 3, Quarter 1 29654.35 Enter
- Division 3, Quarter 2 28963.32 Enter
- Division 3, Quarter 3 25353.55 Enter
- Division 3, Quarter 4 32615.88 Enter
- The total sales for the company are 456782.34
70Passing Two-dimensional Arrays to Functions
- When a two-dimensional array is passed to a
function, the parameter type must contain a size
declarator for the number of columns.
717.11 Arrays of Strings
- A two-dimensional array of characters can be used
as an array of C-strings.
72Program 7-20
// This program displays the number of days in
each month.// It uses a two-dimensional
character array to hold the // names of the
months and an int array to hold the number// of
days.include ltiostream.hgtvoid
main(void) char months1210 "January",
"February", "March",
"April", "May", "June",
"July", "August", "September,
"October", "November","December" int
days12 31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30,
31 for (int count 0 count lt 12
count) cout ltlt monthscount ltlt " has
" cout ltlt dayscount ltlt " days.\n"
73Program 7-20 (continued)
Program Output January has 31 days.February has
28 days.March has 31 days.April has 30 days.
May has 31 days. June has 30 days. July has 31
days.August has 31 days. September has 30 days.
October has 31 days. November has 30
days.December has 31 days.
74Three Dimensional Arrays and Beyond
- C allows you to create arrays with virtually
any number of dimensions. - Here is an example of a three-dimensional array
declarationfloat seat358
757.14 Introduction to the STL vector
- The Standard Template Library (or STL) is a
collection of data types and algorithms that you
may use in your programs. These data types and
algorithms are programmer-defined. They are not
part of the C language, but were created in
addition to the built-in data types.
767.14 Introduction to the STL vector
- The data types that are defined in the STL are
commonly called containers, because they store
and organize data. - There are two types of containers in the STL
sequence containers and associative containers. - The vector data type is a sequence container.
777.14 Introduction to the STL vector
- A vector is like an array in the following ways
- A vector holds a sequence of values, or elements.
- A vector stores its elements in contiguous memory
locations. - You can use the array subscript operator to
read the individual elements in the vector
787.14 Introduction to the STL vector
- However, a vector offers several advantages over
arrays. Here are just a few - You do not have to declare the number of elements
that the vector will have. - If you add a value to a vector that is already
full, the vector will automatically increase its
size to accommodate the new value. - vectors can report the number of elements they
contain.
79Declaring a vector
- To use vectors in your program, you must first
include the vector header file with the
following statement include ltvectorgt Note
There is no .h at the end of the file name.
80Declaring a vector
- The next step is to include the following
statement after your include statementsusing
namespace std The STL uses namespaces to
organize the names of its data types and
algorithms.
81Declaring a vector
- Now you are ready to declare an actual vector
object. Here is an examplevectorltintgt
numbersThe statement above declares numbers as
a vector of ints.
82Declaring a vector
- You can declare a starting size, if you prefer.
Here is an examplevectorltintgt
numbers(10)The statement above declares
numbers as a vector of 10 ints.
83Other examples of vector Declarations
84Storing and Retrieving Values in a vector
- To store a value in an element that already
exists in a vector, you may use the array
subscript operator .
85Program 7-23
// This program stores, in two vectors, the hours
worked by 5// employees, and their hourly pay
rates.include ltiostream.hgtinclude ltvectorgt //
Needed to declare vectorsusing namespace
stdvoid main(void) vectorltintgt hours(5)
// Declare a vector of 5 integers vectorltfloatgt
payRate(5) // Declare a vector of 5
floats cout ltlt "Enter the hours worked by 5
employees and their\n" cout ltlt "hourly
rates.\n" for (int index 0 index lt 5
index) cout ltlt "Hours worked by employee
" ltlt (index 1) cout ltlt " " cin gtgt
hoursindex cout ltlt "Hourly pay rate for
employee " cout ltlt (index 1) ltlt " " cin
gtgt payRateindex
86Program 7-23 (continued)
cout ltlt "Here is the gross pay for each
employee\n" cout.precision(2) cout.setf(ios
fixed iosshowpoint) for (index 0 index lt
5 index) float grossPay
hoursindex payRateindex cout ltlt
"Employee " ltlt (index 1) cout ltlt " " ltlt
grossPay ltlt endl
87Program 7-23 (continued)
Program Output with Example Input Shown in
Bold Enter the hours worked by 5 employees and
theirhourly rates.Hours worked by employee 1
10 EnterHourly pay rate for employee 1 9.75
EnterHours worked by employee 2 15
EnterHourly pay rate for employee 2 8.62
EnterHours worked by employee 3 20
EnterHourly pay rate for employee 3 10.50
EnterHours worked by employee 4 40
EnterHourly pay rate for employee 4 18.75
EnterHours worked by employee 5 40
EnterHourly pay rate for employee 5 15.65
EnterHere is the gross pay for each
employeeEmployee 1 97.50Employee 2
129.30Employee 3 210.00Employee 4
750.00Employee 5 626.00
88Using the push_back Member Function
- You cannot use the operator to access a vector
element that does not exist. - To store a value in a vector that does not have a
starting size, or is already full, use the
push_back member function. Here is an
example numbers.push_back(25)
89Program 7-24
// This program stores, in two vectors, the hours
worked by a specified// number of employees, and
their hourly pay rates.include
ltiostream.hgtinclude ltvectorgt // Needed to
declare vectorsusing namespace stdvoid
main(void) vectorltintgt hours // hours is
an empty vector vectorltfloatgt payRate //
payRate is an empty vector int numEmployees
// The number of employees cout ltlt "How many
employees do you have? " cin gtgt
numEmployees cout ltlt "Enter the hours worked by
" ltlt numEmployees cout ltlt " employees and their
hourly rates.\n"
90Program 7-24 (continued)
for (int index 0 index lt numEmployees
index) int tempHours // To hold the
number of hours entered float tempRate // To
hold the payrate entered cout ltlt "Hours worked
by employee " ltlt (index 1) cout ltlt "
" cin gtgt tempHours hours.push_back(tempHours
) // Add an element to hours cout ltlt "Hourly
pay rate for employee " cout ltlt (index 1)
ltlt " " cin gtgt tempRate payRate.push_back(te
mpRate) // Add an element to payRate cout ltlt
"Here is the gross pay for each
employee\n" cout.precision(2) cout.setf(ios
fixed iosshowpoint) for (index 0 index lt
numEmployees index) float grossPay
hoursindex payRateindex cout ltlt
"Employee " ltlt (index 1) cout ltlt " " ltlt
grossPay ltlt endl
91Program 7-24 (continued)
Program Output with Example Input Shown in
Bold How many employees do you have? 3 Enter
Enter the hours worked by 3 employees and their
hourly rates.Hours worked by employee 1 40
EnterHourly pay rate for employee 1 12.63
EnterHours worked by employee 2 25
EnterHourly pay rate for employee 2 10.35
EnterHours worked by employee 3 45
EnterHourly pay rate for employee 3 22.65
EnterHere is the gross pay for each
employeeEmployee 1 505.20Employee 2
258.75Employee 3 1019.25
92Determining the Size of a vector
- Unlike arrays, vectors can report the number of
elements they contain. This is accomplished with
the size member function. Here is an example of a
statement that uses the size member
functionnumValues set.size() - In the statement above, assume that numValues is
an int, and set is a vector. After the statement
executes, numValues will contain the number of
elements in the vector set.
93Determining the Size of a vector
- Examplevoid showValues(vectorltintgt
vect) for (int count 0 count lt vect.size()
count) cout ltlt vectcount ltlt endl
94Program 7-25
// This program demonstrates the vector size //
member function.include ltiostream.hgt include
ltvectorgtusing namespace std // Function
prototypevoid showValues(vectorltintgt) void
main(void) vectorltintgt values for (int
count 0 count lt 7 count) values.push_back(
count 2) showValues(values)
95Program 7-25 (continued)
//
// Definition of function showValues.
// This function accepts an int vector as
its // argument. The value of each of the
vector's // elements is displayed.
//
void showValues(vectorltintgt
vect) for (int count 0 count lt
vect.size() count) cout ltlt vectcount ltlt
endl
96Program 7-25 (continued)
Program Output 024681012
97Removing Elements from a vector
- Use the pop_back member function to remove the
last element from a vector. collection.pop_back
() The statement above removes the last
element from the collection vector.
98Program 7-26
// This program demosntrates the vector size
member function. include ltiostream.hgtinclude
ltvectorgtusing namespace std void
main(void) vectorltintgt values // Store
values in the vector values.push_back(1) values
.push_back(2) values.push_back(3) cout ltlt
"The size of values is " ltlt values.size() ltlt
endl // Remove a value from the vector cout
ltlt "Popping a value from the vector...\n" values
.pop_back() cout ltlt "The size of values is now
" ltlt values.size() ltlt endl
99Program 7-26 (continued)
// Now remove another value from the
vector cout ltlt "Popping a value from the
vector...\n" values.pop_back() cout ltlt "The
size of values is now " ltlt values.size() ltlt
endl // Remove the last value from the
vector cout ltlt "Popping a value from the
vector...\n" values.pop_back() cout ltlt "The
size of values is now " ltlt values.size() ltlt
endlProgram OutputThe size of values is
3Popping a value from the vector...The size of
values is now 2Popping a value from the
vector...The size of values is now 1Popping a
value from the vector...The size of values is
now 0
100Clearing a vector
- To completely clear the contents of a vector, use
the clear member function. Here is an
example numbers.clear() After the statement
above executes, the numbers vector will be
cleared of all its elements.
101Program 7-27
// This program demosntrates the vector size
member function.include ltiostream.hgtinclude
ltvectorgtusing namespace std void
main(void) vectorltintgt values(100) cout ltlt
"The values vector has ltlt values.size() ltlt "
elements.\n" cout ltlt "I will call the clear
member function...\n" values.clear() cout ltlt
"Now, the values vector has ltlt values.size()
ltlt " elements.\n"
102Program 7-27 (continued)
Program Output The values vector has 100
elements.I will call the clear member
function...Now, the values vector has 0
elements.
103Detecting an Empty vector
- To determine if a vector is empty, use the empty
member function. The function returns true if
the vector is empty, and false if the vector has
elements stored in it. Here is an example of its
use - if (set.empty()) cout ltlt "No values in
set.\n"
104Program 7-28
// This program demosntrates the vector's empty
member function. include ltiostream.hgtinclude
ltvectorgtusing namespace std // Function
prototypefloat avgVector(vectorltintgt) void
main(void) vectorltintgt values int
numValues float average cout ltlt "How many
values do you wish to average? " cin gtgt
numValues
105Program 7-28 (continued)
for (int count 0 count lt numValues
count) int tempValue cout ltlt "Enter a
value " cin gtgt tempValue values.push_back(t
empValue) average avgVector(values) cout
ltlt "Average " ltlt average ltlt endl //
// Definition of function avgVector.
// This function accepts an int
vector as its argument. If // the vector
contains values, the function returns the
// average of those values. Otherwise, an error
message is // displayed and the function
returns 0.0.
//
106Program 7-28 (continued)
float avgVector(vectorltintgt vect) int total
0 // accumulator float avg // average if
(vect.empty()) // Determine if the vector is
empty cout ltlt "No values to
average.\n" avg 0.0 else for (int
count 0 count lt vect.size() count) total
vectcount avg total /
vect.size() return avg
107Program 7-28 (continued)
Program Output with Example Input Shown in
BoldHow many values do you wish to
average?Enter a value 12Enter a value
18Enter a value 3Enter a value 7Enter a
value 9Average 9 Program Output with Example
Input Shown in BoldHow many values do you wish
to average? 0No values to average.Average 0
108Summary of vector Member Functions
109Summary of vector Member Functions
110Summary of vector Member Functions
111Summary of vector Member Functions