Documente Academic
Documente Profesional
Documente Cultură
Recursion
Unsigned long Factorial (Unsigned long number) {
if (number <=1)
return number;
else
return number * Factorial (number-1);
}
Explanation
If the number is 5
return 5* Factorial(4)
return 5* 4 Factorial(3)
return 5*4*3* Factorial(2)
return 5*4*3*2 Factorial(1)
Figure 7-1
int count
float price
char letter
Figure 7-2
Table 7-1
Array Declaration
char letters[25];
short rings[100];
int miles[84];
float temp[12];
doubledDistance[1000];
Number of
Elements
25
100
84
12
1000
Size of
Each Element
Size of the
Array
1
2
4
4
8
25 bytes
200 bytes
336 bytes
48 bytes
8000 bytes
byte
bytes
bytes
bytes
bytes
Program 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 <iostream.h>
void main(void)
{
short hours[6];
cout << "Enter the hours worked by six employees: ";
cin >> hours[0];
cin >> hours[1];
cin >> hours[2];
cin >> hours[3];
Int hours[6][6];
cout << "Enter the hours worked by six employees: ";
For i:= 1 to 6 {
For j: 1 to 6 {
9
Cin >> hours [I] [j] }
}
Starting Out with C++, 3rd Edition
Program continues
cin >> hours[4];
cin >> hours[5];
cout << "The hours you entered are:";
cout << " " << hours[0];
cout << " " << hours[1];
cout << " " << hours[2];
cout << " " << hours[3];
cout << " " << hours[4];
cout << " " << hours[5] << endl;
}
10
11
Figure 7-7
12
Program 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 <iostream.h>
void main(void)
{
short hours[6];
cout << "Enter the hours worked by six employees: ";
for (int count = 0; count < 6; count++)
cin >> hours[count];
cout << "The hours you entered are:";
for (count = 0; count < 6; count++)
cout << " " << hours[count];
cout << endl;
}
13
14
Program 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.
#include<iostream.h>
void main(void)
{
short hours[6];
cout << "Enter the hours worked by six employees.\n";
for (int count = 1; count <= 6; count++)
{
cout << "Employee " << count << ": ";
cin >> hours[count - 1];
}
cout << "The hours you entered are\n";
15
Program continues
for (count = 1; count <= 6; count++)
{
cout << "Employee " << count << ": ";
cout << hours[count - 1] << endl;
}
}
16
17
18
Program 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 <iostream.h>
void main(void)
{
short values[3];
19
Figure 7-8
20
21
Program 7-5
// This program displays the number of days in each month.
// It uses a 12-element int array.
#include <iostream.h>
void main(void)
{
int days[12];
days[0] = 31;
days[1] = 28;
days[2] = 31;
days[3] = 30;
days[4] = 31;
days[5] = 30;
days[6] = 31;
//
//
//
//
//
//
//
January
February
March
April
May
June
July
22
Program continues
days[7] = 31; // August
days[8] = 30; // September
days[9] = 31; // October
days[10] = 30; // November
days[11] = 31; // December
for (int count = 0; count < 12; count++)
{
cout << "Month " << (count + 1) << " has ";
cout << days[count] << " days.\n";
}
}
23
Program Output
Month
Month
Month
Month
Month
Month
Month
Month
Month
Month
Month
Month
1 has 31 days.
2 has 28 days.
3 has 31 days.
4 has 30 days.
5 has 31 days.
6 has 30 days.
7 has 31 days.
8 has 31 days.
9 has 30 days.
10 has 31 days.
11 has 30 days.
12 has 31 days.
24
Program 7-6
// This program displays the number of days in each month.
// It uses a 12-element int array.
#include <iostream.h>
void main(void)
{
int days[12] = {31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31};
for (int count = 0; count < 12; count++)
{
cout << "Month " << (count + 1) << " has ";
cout << days[count] << " days.\n";
}
}
25
Program Output
Month
Month
Month
Month
Month
Month
Month
Month
Month
Month
Month
Month
1 has 31 days.
2 has 28 days.
3 has 31 days.
4 has 30 days.
5 has 31 days.
6 has 30 days.
7 has 31 days.
8 has 31 days.
9 has 30 days.
10 has 31 days.
11 has 30 days.
12 has 31 days.
26
Program 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 <iostream.h>
void main(void)
{
char letters[10] = {'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J'};
cout << "Character" << "\t" << "ASCII Code\n";
cout << "--------" << "\t" << "----------\n";
for (int count = 0; count < 10; count++)
{
cout << letters[count] << "\t\t";
cout << int(letters[count]) << endl;
}
}
27
Program Output
Character
--------A
B
C
D
E
F
G
H
I
J
ASCII Code
---------65
66
67
68
69
70
71
72
73
74
28
29
Program 7-8
// This program has a partially initialized array.
#include <iostream.h>
void main(void)
{
int numbers[7] = {1, 2, 4, 8}; // Initialize the
// first 4 elements.
cout << "Here are the contents of the array:\n";
for (int index = 0; index < 7; index++)
cout << numbers[index] << endl;
}
30
Program Output
Here are the contents of the array:
1
2
4
8
0
0
0
31
32
33
Figure 7-11
34
Program 7-9
// This program displays the contents of two char arrays.
#include <iostream.h>
void main(void)
{
char name1[] = "Holly";
char name2[] = {'W', 'a', 'r', 'r', 'e', 'n', '\0'};
cout << name1 << endl;
cout << name2 << endl;
}
35
Program Output
Holly
Warren
36
37
Program 7-10
// This program stores, in an array, the hours worked by 5
// employees who all make the same hourly wage.
#include <iostream.h>
void main(void)
{
int hours[5];
float payRate;
cout << "Enter the hours worked by 5 employees who all\n";
cout << "earn the same hourly rate.\n";
for (int index = 0; index < 5; index++)
{
cout << "Employee #" << (index + 1) << ": ";
cin >> hours[index];
}
38
Program continues
cout << "Enter the hourly pay rate for all the employees: ";
cin >> payRate;
cout << "Here is the gross pay for each employee:\n";
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
for (index = 0; index < 5; index++)
{
float grossPay = hours[index] * payRate;
cout << "Employee #" << (index + 1);
cout << ": $" << grossPay << endl;
}
}
39
40
Program 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 <iostream.h>
// Constant for defining the array size
void main(void)
{
int hours[5];
float payRate;
cout << "Enter the hours worked by 5 employees who all\n";
cout << "earn the same hourly rate.\n";
for (int index = 0; index < 5; index++)
{
cout << "Employee #" << (index + 1) << ": ";
cin >> hours[index];
}
41
Program continues
cout << "Enter the hourly pay rate for all the employees: ";
cin >> payRate;
cout << "Here is the gross pay for each employee:\n";
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
for (index = 0; index < 5; index++)
{
float grossPay, overTime;
if (hours[index] > 40)
{
// Calculate pay for 40 hours.
grossPay = 40 * payRate;
// Calculate overtime pay.
overTime = (hours[index] - 40) * 1.5 * payRate;
// Add regular pay and overtime pay.
grossPay += overTime;
}
42
Program continues
else
grossPay = hours[index] * payRate;
cout << "Employee #" << (index + 1);
cout << ": $" << grossPay << endl;
}
}
43
45
Program 7-12
// This program stores, in two arrays, the hours worked by 5
// employees, and their hourly pay rates.
#include <iostream.h>
// Constant for defining the array size
const int numEmps = 5;
void main(void)
{
int hours[numEmps];
float payRate[numEmps];
cout << "Enter the hours worked by << numEmps
<< employees and their\n";
cout << "hourly rates.\n";
for (int index = 0; index < numEmps; index++)
{
cout << "hours worked by employee #" << (index + 1);
cout << ": ";
46
Program continues
cin >> hours[index];
cout << "Hourly pay rate for employee #";
cout << (index + 1) << ": ";
cin >> payRate[index];
}
cout << "Here is the gross pay for each employee:\n";
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
for (index = 0; index < numEmps; index++)
{
float grossPay = hours[index] * payRate[index];
cout << "Employee #" << (index + 1);
cout << ": $" << grossPay << endl;
}
}
47
49
Table 7-2
Expression
Value
O ld V a lu e s [0 ]
10 (Contents of Element 0 of O l d V a l u e s )
O ld V a lu e s [1 ]
O ld V a lu e s [2 ]
O ld V a lu e s [3 ]
N e w V a lu e s
O ld V a lu e s
50
51
52
Program 7-13
// This program demonstrates that an array element is passed
// to a function like any other variable.
#include <iostream.h>
void ShowValue(int);
// Function prototype
void main(void)
{
int collection[8] = {5, 10, 15, 20, 25, 30, 35, 40};
for (int Cycle = 0; Cycle < 8; Cycle++)
ShowValue(collection[Cycle]);
}
53
Program continues
//************************************
// Definition of function showValue.
*
// This function accepts an integer argument. *
// The value of the argument is displayed.
*
//************************************
void ShowValue(int Num)
{
cout << Num << " ";
}
54
Program Output
5 10 15 20 25 30 35 40
55
Program 7-14
// This program demonstrates an array being passed to a function.
#include <iostream.h>
void showValues(int []); // Function prototype
void main(void)
{
int collection[8] = {5, 10, 15, 20, 25, 30, 35, 40};
showValues(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[])
{
for (int index = 0; index < 8; index++)
cout << nums[index] << " ";
}
56
Program Output
5 10 15 20 25 30 35 40
57
Program 7-15
// This program demonstrates an array being passed to a function.
#include <iostream.h>
void showValues(int []);
// Function prototype
void main(void)
{
int set1[8] = {5, 10, 15, 20, 25, 30, 35, 40};
int set2[8] = {2, 4, 6, 8, 10, 12, 14, 16};
showValues(set1);
cout << 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.
*
//***********************************************
void showValues(int nums[])
{
for (int index = 0; index < 8; index++)
cout << nums[index] << " ";
}
58
Program Output
5 10 15 20 25 30 35 40
2 4 6 8 10 12 14 16
59
Program 7-16
// This program uses a function that can display the contents
// of an integer array of any size.
#include <iostream.h>
void showValues(int [], int); // Function prototype
void main(void)
{
int set1[8] = {5, 10, 15, 20, 25, 30, 35, 40};
int set2[4] = {2, 4, 6, 8};
int set3[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
showValues(set1, 8);
cout << endl;
showValues(set2, 4);
cout << endl;
showValues(set3, 12);
}
60
Program 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 < elements; index++)
cout << nums[index] << " ";
}
61
Program Output
5 10 15 20 25 30 35 40
2468
1 2 3 4 5 6 7 8 9 10 11 12
62
Program 7-17
// This program uses a function that doubles the contents of
// the elements within an array.
#include <iostream.h>
void doubleArray(int [], int);
const int arraySize = 12;
// Function prototype
void main(void)
{
int set[arraySize] = {1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12};
cout << "The arrays values are:\n";
for (int index = 0; index < arraySize; index++)
cout << set[index] << " ";
cout << endl;
doubleArray(set, arraySize);
cout << "After calling doubleArray, the values are:\n";
63
Program continues
for (int index = 0; index < arraySize; index++)
cout << set[index] << " ";
cout << 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 < size; index++)
nums[index] *= 2;
}
64
Program 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
65
66
Program 7-18
// This program demonstrates a two-dimensional array.
#include <iostream.h>
void main(void)
{
float sales[3][4]; // 2D array, 3 rows and 4
columns.
float totalSales = 0;
// To hold the total sales.
int dir, qtr;
// Loop counters.
67
Program continues
cout << "This program will calculate the total sales of\n";
cout << "all the company's divisions.\n";
cout << "Enter the following sales information:\n\n";
// Nested loops to fill the array with quarterly
// sales figures for each division.
for (div = 0; div < 3; div++)
{
for (qtr = 0; qtr < 4; qtr++)
{
cout << "Division " << (div + 1);
cout << ", Quarter " << (qtr + 1) << ": $";
cin >> sales[div][qtr];
}
cout << endl; // Print blank line.
}
68
Program continues
// Nested loops to add all the elements.
for (div = 0; div < 3; div++)
for (qtr = 0; qtr < 4; qtr++)
totalSales += sales[div][qtr];
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
cout << "The total sales for the company are: $";
cout << totalSales << endl;
}
69
1,
1,
1,
1,
Quarter
Quarter
Quarter
Quarter
1:
2:
3:
4:
$31569.45
$29654.23
$32982.54
$39651.21
[Enter]
[Enter]
[Enter]
[Enter]
Division
Division
Division
Division
2,
2,
2,
2,
Quarter
Quarter
Quarter
Quarter
1: $56321.02 [Enter]
2: $54128.63 [Enter]
3: $41235.85 [Enter]
4: $54652.33 [Enter]
70
Output continues
Division
Division
Division
Division
3,
3,
3,
3,
Quarter
Quarter
Quarter
Quarter
1:
2:
3:
4:
$29654.35
$28963.32
$25353.55
$32615.88
[Enter]
[Enter]
[Enter]
[Enter]
71
72
73
Program 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 <iostream.h>
void main(void)
{
char months[12][10] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September,
"October", "November","December"};
int days[12] = { 31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31};
for (int count = 0; count < 12; count++)
{
cout << months[count] << " has ";
cout << days[count] << " days.\n";
}
}
74
Starting Out with C++, 3rd Edition
75
76
Declaring a vector
To use vectors in your program, you must
first #include the vector header file with
the following statement:
#include <vector>
Note: There is no .h at the end of the file
name.
81
Declaring a vector
The next step is to include the following
statement after your #include statements:
using namespace std;
The STL uses namespaces to organize the
names of its data types and algorithms.
82
Declaring a vector
Now you are ready to declare an actual
vector object. Here is an example:
vector<int> numbers;
The statement above declares numbers as
a vector of ints.
83
Declaring a vector
You can declare a starting size, if you
prefer. Here is an example:
vector<int> numbers(10);
The statement above declares numbers as
a vector of 10 ints.
84
Description
vector<float> amounts;
vector<int> scores(15);
vector<double> values2(values1);
85
86
Program 7-23
// This program stores, in two vectors, the hours worked by 5
// employees, and their hourly pay rates.
#include <iostream.h>
#include <vector>
// Needed to declare vectors
using namespace std;
void main(void)
{
vector<int> hours(5);
vector<float> payRate(5);
88
89
Program 7-24
// This program stores, in two vectors, the hours worked by a specified
// number of employees, and their hourly pay rates.
#include <iostream.h>
#include <vector>
// Needed to declare vectors
using namespace std;
void main(void)
{
vector<int> hours;
vector<float> payRate;
int numEmployees;
91
93
numValues = 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.
94
Example:
95
Program 7-25
// This program demonstrates the vector size
// member function.
#include <iostream.h>
#include <vector>
using namespace std;
// Function prototype
void showValues(vector<int>);
void main(void)
{
vector<int> values;
for (int count = 0; count < 7; count++)
values.push_back(count * 2);
showValues(values);
}
96
97
98
Program 7-26
// This program demosntrates the vector size member function.
#include <iostream.h>
#include <vector>
using namespace std;
void main(void)
{
vector<int> values;
// Store values in the vector
values.push_back(1);
values.push_back(2);
values.push_back(3);
cout << "The size of values is " << values.size() << endl;
// Remove a value from the vector
cout << "Popping a value from the vector...\n";
values.pop_back();
cout << "The size of values is now " << values.size() << endl;
100
Program Output
The size of values is 3
Popping a value from the vector...
The size of values is now 2
Popping a value from the vector...
The size of values is now 1
Popping a value from the vector...
The size of values is now 0
101
Clearing 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.
102
Program 7-27
// This program demosntrates the vector size member function.
#include <iostream.h>
#include <vector>
using namespace std;
void main(void)
{
vector<int> values(100);
cout << "The values vector has
<< values.size() << " elements.\n";
cout << "I will call the clear member function...\n";
values.clear();
cout << "Now, the values vector has
<< values.size() << " elements.\n";
}
103
104
Program 7-28
// This program demosntrates the vector's empty member function.
#include <iostream.h>
#include <vector>
using namespace std;
// Function prototype
float avgVector(vector<int>);
void main(void)
{
vector<int> values;
int numValues;
float average;
cout << "How many values do you wish to average? ";
cin >> numValues;
106
108
109
Description
at(element)
capacity()
110
empty()
pop_back()
111
reverse()
resize(elements,
value)
112
113