You are on page 1of 30

Appendix I

Answers to Odd
Numbered
Exercises
Chapter 1
1. a. true; b. false; c. false; d. false; e. true; f; false; g. false; h. true; i. true; j. true
3. Fetch and decode instructions, control the flow of information (instructions or data) in and out of main
memory, and control the operation of the internal components of the C!.
". #creen and printer.
$. %&ery computer directly understands its own machine language. 'herefore, for the computer to e(ecute
a program written in a high)le&el language, the high)le&el language program must be translated into the
computer*s machine language.
+. , well)analy-ed problem leads to a well)designed algorithm. .oreo&er, a program that is well
analy-ed is easier to modify as well as spot and fi( errors.
11. 'o find the weighted a&erage of the four test scores, first you need to /now each test score and its
weight. 0e(t, you multiply each test score with its weight, and then add these numbers; the sum is then
di&ided by four to get the a&erage. 'herefore,
1. 1et testScore1, weightTestScore1
2. 1et testScore2, weightTestScore2
3. 1et testScore3, weightTestScore3
4. 1et testScore4, weightTestScore4
5. sum = testScore1 * weightTestScore1 +
testScore2 * weightTestScore2 +
testScore3 * weightTestScore3 +
testScore4 * weightTestScore4;
2. average = sum / 4;
13. 'o calculate the selling price of an item, we need to /now the original price (the price the store pays to
buy) of the item. 3e can then the use the following formula to find the selling price4
sellingPrice = originalPrice + originalPrice * .!
'he algorithm is as follows4
a. 1et originalPrice
". Calculate the sellingPrice using the formula4
sellingPrice = originalPrice + originalPrice * .!
'he information needed to calculate the selling price is the original price and the mar/ed)up
percentage.
1". 5et r denote the radius of the circle. 1i&en the lengths a, b, and c, such that a 6 b 6 c is the
circumference of the circle, we ha&e 78r 9 a 6 b 6 c. 'his implies that r 9 (a 6 b 6 c) : (78). 3e can
now write the algorithm as follows4
a. Get the values of a, ", c
b. Calculate r using the formula:
r 9 (a 6 " 6 c) : (78)
1$. #uppose averageTestScore denotes the a&erage test score, highestScore denotes the highest
test score, testScore denotes a test score, sum denote the sum of all the test scores, and count
denotes the number of students in class, and stu#ent$ame denotes the name of a student.
a. First you design an algorithm to find the a&erage test score. 'o find the a&erage test score, first you
need to count the number of students in the class and add the test score of each student. ;ou then
di&ide the sum by count to find the a&erage test score. 'he algorithm to find the a&erage test score
is as follows4
i. #et sum and count to !.
ii. <epeat the following for each student in class.
1. 1et testScore
7. =ncrement count and update the &alue of sum by adding the current test score to sum.
iii. !se the following formula to find the a&erage test score.
i% &count is !'
averageTestScore = !;
otherwise
averageTestScore = sum / count;
b. 'he following algorithm determines and prints the names of all the students whose test score is
below the a&erage test score.
<epeat the following for each student in class4
i. 1et stu#ent$ame and testScore
ii.
i% &testScore is less than averageTestScore'
(rint stu#ent$ame
c. 'he following algorithm determines and highest test score
i. 1et first student*s test score and call it highestTestScore.
ii. <epeat the following for each of the remaining student in class
1. 1et testScore
7.
i% &testScore is greater than highestTestScore'
highestTestScore = testScore;
d. 'o print the names of all the students whose test score is the same as the highest test score,
compare the test score of each student with the highest test score and if they are e>ual print the
name. 'he following algorithm accomplishes this
<epeat the following for each student in class4
i. 1et stu#ent$ame and testScore
ii.
i% &testScore is e)ual to highestTestScore'
(rint stu#ent$ame
;ou can use the solutions of the subproblems obtained in parts a to d to design the main algorithm as
follows4
1. !se the algorithm in part a to find the a&erage test score.
7. !se the algorithm in part b to print the names of all the students whose score is below the a&erage
test score.
3. !se the algorithm in part c to find the highest test score.
?. !se the algorithm in part d to print the names of all the students whose test score is the same as the
highest test score
Chapter 2
1. a. false; b. false; c. false; d. true; e. true; f. false; g. true; h. true; i. false; j. true; /. false
3. a, b
". a. 3
b. 0ot possible. @oth the operands of the operator * must be integers. @ecause the second operand,
w, is a floating)point &alue, the e(pression is in&alid.
c. 0ot possible. @oth the operands of the operator * must be integers. @ecause the first operand,
which is + + w, is a floating)point &alue, the e(pression is in&alid .
d. 3,.5
e. 1
f. 2
g. 2
h. 42!.!
$. -
+. a and c are &alid
11. a. .1! * a
b. /,/
c. &" * " . 4 * a * c' / &2 * a'
d. (." + &" * " . 4 * a * c'' / &2 * a'
13. 0 = 2!
+ = 15
1 =
w = 11.5
t = 4.5
1". a. !.5!
b. 24.5!
c. 3-.
d. ,.3
e. 1!
f. 3,.-5
1$. a and c are correct
1+. a. 0 *= 2;
b. 0 += + 2 2;
c. sum += num;
d. 1 *= 0 + 2;
e. + /= 0 + 5;
71.
a " c
a = &"++' + 3; 3 - un#
c = 2 * a + &++"'; 3 , 2
" = 2 * &++c' . &a++'; 1! 45 2-
73. ('he user input is shaded.)
a = 25
4nter two integers 5 2! 15
The num"ers +ou entere# are 2! an# 15
1 = 45.5
6our gra#e is 7
The value o% a = 5
7".
8inclu#e 9iostream:
8inclu#e 9string:
using names(ace st#;
const #ou"le ; = 13.45;
const int 6 = 34;
const char <=7$> = / /;
int main&'
?
string %irst$ame@ last$ame;
int num;
#ou"le salar+;
cout 99 A4nter %irst name5 A;
cin :: %irst$ame;
cout 99 en#l;
cout 99 A4nter last name5 A;
cin :: last$ame;
cout 99 en#l;
cout 99 A4nter a (ositive integer less than -!5 A;
cin :: num;
cout 99 en#l;

salar+ = num * ;;

cout 99 A$ame5 A 99 %irst$ame 99 <=7$> 99 last$ame 99 en#l;
cout 99 ABages5 CA 99 salar+ 99 en#l;
cout 99 A; = A 99 ; 99 en#l;
cout 99 A; + 6 = A 99 ; + 6 99 en#l;

return !;
D
Chapter 3
1. a. true; b. true; c. false; d. false; e. true; f. true
3. a. 0 = 3-@ + = ,@ 1 = !.5
b. 0 = 3-@ + = 32@ 1 = ,.5
c. =nput failure4 1 = 3-.!@ 0 = ,@ trying to read the . (period) into +.
". =nput failure4 'rying to read 7 into +, which is an int &ariable. 0 = 4@ + = 1,@ an# 1 =
/7/. 'he &alues of + and 1 are unchanged.
$. a. name = A EicFe+ <altoA@ age = 35
b. name = A A@ age = 35
+.
8inclu#e 9iostream:
8inclu#e 9%stream:
using names(ace st#;
int main&'
?
int num1@ num2;
i%stream in%ile;
ostream out%ile;
in%ile.o(en&Ain(ut.#atA';
out%ile.o(en&Aout(ut.#atA';
in%ile :: num1 :: num2;
out%ile 99 ASum = A 99 num1 + num2 99 en#l;
in%ile.close&';
out%ile.close&';
return !;
D
11. a. #ame as before.
b. 'he file contains the output produced by the program.
c. 'he file contains the output produced by the program. 'he old contents are erased.
d. 'he program would prepare the file and store the output in the file.
Chapter 4
1. a. false; b. false; c. false; d. true; e. false; f. false; g. false; h. false; i. false; j. true
3. 1!! 2!! !
". Amit the semicolon after else. 'he correct statement is4
i% &score := !'
cout 99 A6ou (ass.A 99 en#l;
else
cout 99 A6ou %ail.A 99 en#l;
$. 15
+. 3
11.
8inclu#e 9iostream:
using names(ace st#;
const int one = 5;
int main&'
?
int 0@ +@ w@ 1;
1 = 3;
i% &1 : 1!'
?
0 = 12;
+ = 5;
w = 0 + + + one;
D
else
?
0 = 12;
+ = 4;
w = 0 + + + one;
D
cout 99 Aw = A 99 w 99 en#l;
return !;
D
Chapter 5
1. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false
3. 5
". if ch : /G/ or ch 9 /7/
$. Sum = 15,
+. Sum = 15,
11. 11 1, 25
13. a. 1,
b. 14
c. false
1". 2 - 1- 3- -- 15-
1$. a. *
b. infinite loop
c. infinite loop
d. ****
e. ******
f. ***
1+. BH7T IS JK$4 @ KLT4$ IS $KT JK$4 B4==.
71.
! 2 24
25 2 43
5! 2 -4
-5 2 33
1!! 2 124
125 2 143
15! 2 1-4
1-5 2 2!!
73. a. both
b. #o...while
c. while
d. while
7". 'here is more than one answer to this problem. Ane solution is4
#o
?
cin :: num"er;
i% &num"er M= 21'
total = total + num"er;
count++;
D
while &num"er M= 21';
7$. a.
num"er = 1;
while &num"er 9= 1!'
?
cout 99 setw&3' 99 num"er;
num"er++;
D
b.
num"er = 1;
#o
?
cout 99 setw&3' 99 num"er;
num"er++;
D
while &num"er 9= 1!';
7+. 11 1, 25
Chapter 6
1. a. false; b. true; c. true; d. true; e. false
3. a, b, c, e, f, and g are &alid. =n d, the function call in the output (cout) statement re>uires one more
argument.
". a. ?; int
b. 7; #ou"le
c. ?; char
d. 'he function test re>uires four actual parameters. 'he order of the parameters is4 int, char,
#ou"le, int.
e. cout 99 test&5@ /1/@ -.3@ 5' 99 en#l;
f. cout 99 two&1-.5@ 1,.3' 99 en#l;
g.
cout 99 staticNcast9char:&staticNcast9int:&three&4@ 3@ /7/@ 1-.'' + 1'
99 en#l;
$. a. &i' 125 &ii' 432
b. 'he function computes 0
3
, where 0 is the argument of the function.
+. 1
2

24
12!
Chapter 7
1. a. true; b. false; c. true; d. false; e. true; f. false; g. false; h. false; i. true
3. a. , &ariable declared in the heading of a function definition is called a formal parameter. , &ariable
or e(pression used in a function call is called an actual parameter.
b. , &alue parameter recei&es a copy of the actual parameter*s data. , reference parameter recei&es
the address of the actual parameter.
c. , &ariable declared within a function or bloc/ is called a local &ariable. , &ariable declared
outside of e&ery function definition is called a global &ariable.
".
3 4 2! -,
- 3 2! 4
- 5 2
$.
#include <iostream>
using namespace std;
void func(int val1, int val2);
int main()
{
int num1, num2;
__1__ cout << "Please enter two integers." << endl;
__2__ cin >> num1 >> num2;
__3__ func (num1, num2);
__7__ cout << " The two integers are " << num1
<< ", " << num2 << endl;
__8__ return 0;
}
void func (int val1, int val2)
{
int val3, val4;
__4__ val3 = val1 + val2;
__5__ val4 = val1 * val2;
__6__ cout << "The sum and product are " << val3
<< " and " << val4 << endl;
}
+.
=ine 45 In main5 num1 = 1!@ num2 = 2!@ an# t = 15
=ine 35 In %unKne5 a = 15@ 0 = 1!@ 1 = 25@ an# t = 15
=ine 115 In %unKne5 a = 15@ 0 = 15@ 1 = 25@ an# t = 15
=ine 135 In %unKne5 a = 2-@ 0 = 15@ 1 = 25@ an# t = 2-
=ine 155 In %unKne5 a = 4!@ 0 = 15@ 1 = 25@ an# t = 4!
=ine 5 In main a%ter %unKne5 num1 = 15@ num2 = 2!@ an# t = 4!
11. (i), (ii), and (i&) are correct.
Chapter 8
1. a. true; b. false; c. true; d. false; e. false; f. true; g. true; h. true; i. false; j. true; /. false
3. Anly a and c are &alid.
". 'he statement4
using names(ace st#;
is missing between 5ines 1 and 7.
$. %ither include the statement4
using names(ace aaa;
before the function main or refer to the identifiers 0 and + in main as aaa550 and aaa55+,
respecti&ely.
+.
Ooing to the 7musement ParF
14
1!
musem
7<PJ4LOHIQ>
11
a<#J4LOHIQ>
Chapter 9
1. a. true; b. true; c. false; d. false; e. true; f. false; g. false; h. false; i. true; j. false; /. false; l. false
3.
a. %uncKne&list@ 5!';
b. cout 99 %uncSum&5!@ listR3S' 99 en#l;
c. cout 99 %uncSum&listR23S@ listR3S' 99 en#l;
d. %uncTwo&list@ 7list';
". list elements are4 5@ @ 3@ 13@ 23@ 3-
$. a. =n&alid; the assignment operator is not defined for P)strings.
b. =n&alid; the relational operators are not defined for P)strings.
c. =n&alid; the assignment operator is not defined for P)strings.
d. Balid
+.
a. strc(+&str1@ ASunn+ Ja+A';
b. length = strlen&str1';
c. strc(+&str2@ name';
d.
i% &strcm(&str1@ str2' 9= !'
cout 99 str1 99 en#l;
else
cout 99 str2 99 en#l;
11. =ist elements5 11 1 21 2 3!
13. a. 3C
b. "
c. 2
d. row
e. column
1". a. "eta is initiali-ed to !.
b.
Lirst row o% "eta5 ! 1 2
Secon# row o% "eta5 1 2 3
Thir# row o% "eta5 2 3 4
c.
Lirst row o% "eta5 ! ! !
Secon# row o% "eta5 ! 1 2
Thir# row o% "eta5 ! 2 4
d.
Lirst row o% "eta5 ! 2 !
Secon# row o% "eta5 2 ! 2
Thir# row o% "eta5 ! 2 !
Chapter 10
1. a. false; b. true; c. false; d. false; e. false
3. a. "
b. $
c. D
d. 11
". 5ist before the first iteration4 2@ 45@ 1-@ 5@ 33@ 55@ 12@ 1,
5ist after the first iteration4 2@ 1-@ 45@ 33@ 55@ 12@ 1,@ 5
5ist after the second iteration4 1-@ 2@ 33@ 45@ 12@ 1,@ 55@ 5
5ist after the third iteration4 1-@ 2@ 33@ 12@ 1,@ 45@ 55@ 5
5ist after the fourth iteration4 1-@ 2@ 12@ 1,@ 33@ 45@ 55@ 5
5ist after the fifth iteration4 1-@ 12@ 1,@ 2@ 33@ 45@ 55@ 5
5ist after the si(th iteration4 12@ 1-@ 1,@ 2@ 33@ 45@ 55@ 5
5ist after the se&enth iteration4 12@ 1-@ 1,@ 2@ 33@ 45@ 55@ 5
$. 3
+. 1!@ 12@ 1,@ 21@ 25@ 2,@ 3!@ -1@ 32@ 5,@ 15
11. 'o use a vector object in a program, the program must include the header file vector.
13. 1 3 5 - 3
1".
a. vector9int: secret=ist;
b.
secret=ist.(ushN"acF&5';
secret=ist.(ushN"acF&2,';
secret=ist.(ushN"acF&32';
secret=ist.(ushN"acF&3';
secret=ist.(ushN"acF&-5';
c.
%or &unsigne# int i = !; i 9 secret=ist.si1e&'; i++'
cout 99 secret=istRiS 99 A A;
cout 99 en#l;
1$. a. cout 99 m+=ist.%ront&' 99 A A 99 m+=ist."acF&' 99 en#l;
b length = m+=ist.si1e&';
c.
%or &int i = !; i 9 m+=ist.si1e&'; i++'
cout 99 m+=istRiS 99 A A;
cout 99 en#l;
Chapter 11
1. a. false; b. false; c. true; d. true; e. true; f. true; g. false
3. a. =n&alid; the member name of new4m(lo+ee is a struct. #pecify the member names to store
the &alue AQohn SmithA. For e(ample,
new4m(lo+ee.name.%irst = AQohnA;
new4m(lo+ee.name.last = ASmithA;
b. =n&alid; the member name of new4m(lo+ee is a struct. 'here are no aggregate output
operations on a struct. , correct statement is4
cout 99 new4m(lo+ee.name.%irst 99 A A
99 new4m(lo+ee.name.last 99 en#l;
c. Balid
d. Balid
e. =n&alid; em(lo+ees is an array. 'here are no aggregate assignment operations on arrays.
Chapter 12
1. a. false; b. false; c. true; d. false; e. false;
3. a. 2
b. 7
c. 7
d.
voi# 0Plass55%unc&'
?
u = 1!;
w = 15.3;
D
d e.
voi# 0Plass55(rint&'
?
cout 99 u 99 A A 99 w 99 en#l;
D
f.
0Plass550Plass&'
?
u = !;
w = !;
D
g. 0.(rint&';
h. 0Plass t&2!@ 35.!';
". a.
int testPlass55sum&'
?
return 0 + +;
D
voi# testPlass55(rint&' const
?
cout 99 A0 = A 99 0 99 A@ + = A 99 + 99 en#l;
D
testPlass55testPlass&'
?
0 = !;
+ = !;
D
testPlass55testPlass&int a@ int "'
?
0 = a;
+ = ";
D
b. Ane possible solution. (3e assume that the name of the header file containing the definition of the
class testPlass is 40ercise5Ph12.h.)
8inclu#e 9iostream.h:
8inclu#e A40ercise5Ph12.hA
int main&'
?
testPlass one;
testPlass two&4@ 5';
one.(rint&';
two.(rint&';
return !;
D
$. a. (ersonT+(e stu#ent&A<u##+A@ AAroraA';
b. stu#ent.(rint&';
c. stu#ent.set$ame&ASusanA@ AOil"ertA';
+.
a. m+Plass55count = !;
b. m+Plass.incrementPount&';
c. m+Plass.(rintPount&';
d.
int m+Plass55count = !;
voi# m+Plass55set;&int a'
?
0 = a;
D
voi# m+Plass55(rint;&' const
?
cout 99 0;
D
voi# m+Plass55(rintPount&'
?
cout 99 count;
D
voi# m+Plass55incrementPount&'
?
count++;
D
m+Plass55m+Plass&int a'
?
0 = a;
D
e. m+Plass m+K"Tect1&5';
f. m+Plass m+K"Tect2&3';
g.
'he statements in 5ines 1 and 7 are &alid.
'he statement in 5ine 3 should be4 m+Plass55(rintPount&';.
'he statement in 5ine ? is in&alid because the member function (rint; is not a static
member of the class, and so cannot be called by using the name of class.
'he statement in 5ine " is in&alid because count is a (rivate static member &ariable of
the class.
h.
5
2
2
3
14
3
3
Chapter 13
1. a. true; b. true; c. true
3. #ome of the member &ariables that can be added to the class em(lo+eeT+(e are4 #e(artment,
salar+, em(lo+eePategor+ (such as super&isor and president), and em(lo+eeIJ. #ome of the
member functions are4 setIn%o, getSalar+, get4m(lo+eePategor+, and setSalar+.
". a. 'he statement 4
class "Plass (u"lic aPlass
should be4
class "Plass5 (u"lic aPlass
b. .issing semicolon after D.
$.
a.
+Plass55+Plass&'
?
a = !;
" = !;
D
b.
0Plass550Plass&'
?
1 = !;
D
c.
voi# +Plass55two&int u@ int v'
?
a = u;
" = v;
D
+. a.
voi# two55setJata&int a@ int "@ int c'
?
one55setJata&a@ "';
1 = c;
D
b.
voi# two55(rint&' const
?
one55(rint&';
cout 99 1 99 en#l;
D
11.
In "ase5 0 = -
In #erive#5 0 = 3@ + = ,; 0 + + = 11
****-
888811
Chapter 14
1. a. false; b. false; c. false; d. true; e. true; f. true; g false; h. false
3.
3, 3,
3, 3,
". b and c
$. -, -,
+. 4 4 5 - 1! 14 13 25 32 4!
11. =n a shallow copy of data, two or more pointers point to the same memory space. =n a deep copy of
data, each pointer has its own copy of the data.
13.
7rra+ (5 5 - 11 1- 25
7rra+ )5 25 1- 11 - 5
1". 'he copy constructor ma/es a copy of the actual &ariable.
1$. Classes with pointer data members should include the destructor, o&erload the assignment operator,
and e(plicitly pro&ide the copy constructor by including it in the class definition and pro&iding its
definition.
1+.
Plass7 05 4
Plass7 05
Plass< +5 1!
71. ;es.
73.
a. @ecause em(lo+eeT+(e is an abstract class, you cannot instantiate an object of this class.
'herefore, this statement is illegal.
b. 'his statement is legal.
c. 'his statement is legal.
Chapter 15
1. a. false; b. true; c. true; d. false; e. false; f. true; g. false; h. true; i. false; j. true; /. false
3. @ecause the left operand of 99 is a stream object, which is not of the type m+ster+.
". 3hen the class has pointer data members.
$. a. %rien# strange o(erator+&const strangeU@ const strangeU';
b. %rien# "ool o(erator==&const strangeU@ const strangeU';
c. %rien# strange o(erator++&strangeU@ int';
+. =n 5ine 7, the word %rien# before the word "ool is missing. 'he correct statement is4
%rien# "ool o(erator9=&m+ster+@ m+ster+'; //=ine 2
11. 0one.
13. Ane.
1".
class com(le0T+(e
?
//overloa# the stream insertion an# e0traction o(erators
%rien# ostreamU o(erator99&ostreamU@ const com(le0T+(eU';
%rien# istreamU o(erator::&istreamU@ com(le0T+(eU';
(u"lic5
voi# setPom(le0&const #ou"leU real@ const #ou"leU imag';
//set the com(le0 num"er accor#ing to the (arameters
//Postcon#ition5 realPart = real; imaginar+Part = imag
com(le0T+(e&#ou"le real = !@ #ou"le imag = !';
//constructor
//initiali1e the com(le0 num"er accor#ing to the (arameters
//Postcon#ition5 realPart = real; imaginar+Part = imag
com(le0T+(e o(erator+&const com(le0T+(eU otherPom(le0' const;
//overloa# +
com(le0T+(e o(erator*&const com(le0T+(eU otherPom(le0' const;
//overloa# *
com(le0T+(e o(eratorV&' const;
#ou"le o(eratorM&' const;
"ool o(erator==&const com(le0T+(eU otherPom(le0' const;
//overloa# ==
(rivate5
#ou"le realPart; //varia"le to store the real (art
#ou"le imaginar+Part; //varia"le to store the imaginar+ (art
D;
// Je%initions o% o(eratorV an# o(eratorM
com(le0T+(e com(le0T+(e55o(eratorV&' const
?
com(le0T+(e tem( = *this;
tem(.imaginar+Part = 2tem(.imaginar+Part;
return tem(;
D
#ou"le com(le0T+(e55o(eratorM&' const
?
return &(ow&&realPart * realPart +
imaginar+Part * imaginar+Part'@ !.5'';
D
1$. %rror in 5ine ?. , template instantiation can be for only a built)in type or a user)defined type. 'he
word Et+(eF between the angular brac/ets must be replaced either with a built)in type or a user)
defined type.
1+. a. 12 b. Sunn+ Ja+
71.
tem(late 9class T+(e:
voi# swa(&T+(e U0@ T+(e U+'
?
T+(e tem(;
tem( = 0;
0 = +;
+ = tem(;
D
Chapter 16
1. a. false, b. true, c. true, d. false
3. a.
4ntering the tr+ "locF.
40ce(tion5 =ower limit violation.
7%ter the catch "locF
b.
4ntering the tr+ "locF.
40ce(tion5 !
7%ter the catch "locF
c.
4ntering the tr+ "locF.
40iting the tr+ "locF.
7%ter the catch "locF
d.
4ntering the tr+ "locF.
40ce(tion5 !
7%ter the catch "locF
". (,ssume that the definition of the class torna#o40ce(tion is in the header file
torna#o40ce(tion.h.)
8inclu#e 9iostream:
8inclu#e Atorna#o40ce(tion.hA
using names(ace st#;
int main&'
?
int miles;
tr+
?
cout 99 A4nter the miles5 A;
cin :: miles;
cout 99 en#l;
i% &miles 9 5'
throw torna#o40ce(tion&';
else
throw torna#o40ce(tion&miles';
D
catch &torna#o40ce(tion t4'
?
cout 99 t4.what&' 99 en#l;
D
return !;
D
Chapter 17
1. a. true; b. true; c. false; d. false; e. false
3. 'he case in which the solution is defined in terms of smaller &ersions of itself.
". , function that calls another function and e&entually results in the original function call is said to be
indirectly recursi&e.
$. a. 'he statements in 5ines 7 and 3.
b. 'he statements in 5ines ? and ".
c. ,ny nonnegati&e integer.
d. =t is a &alid call. 'he &alue of m+ster+&!' is !.
e. =t is a &alid call. 'he &alue of m+ster+&5' is 15.
f. =t is an in&alid call. =t will result in infinite recursion.
+. a. =t does not produce any output.
b. 5 - , 3
c. =t does not produce any output.
d. =t does not produce any output.
11. a. 2
b. 3
c. 5
d. 21
13.

+
=
=
=
otherwise n m multiply m
n if m
n if
n m multiply
) 1 , (
1
C C
) , (
'he base cases are when n 9 C or n 9 1. 'he general case is specified by the option otherwise.
Chapter 18
1. a. false; b. false; c. false; d. false
3. a. true
b. true
c. false
d. false
e. true
". a. 7 = 72:linF;
b. list = 72:linF2:linF;
c. < = <2:linF2:linF;
d. list = $W==;
e. <2:linF2:in%o = 35;
f. new$o#e = new no#eT+(e;
new$o#e2:in%o = 1!;
new$o#e2:linF = 72:linF;
72:linF = new$o#e;
g. ( = 72:linF;
72:linF = (2:linF;
#elete (;
$. a. 'his is an in&alid code. 'he statement s2:in%o = <; is in&alid because < is a pointer and
s2:in%o is an int.
b. 'his is an in&alid code. ,fter the statement s = s2:linF; e(ecutes, s is $W== and so s2:in%o does not
e(ist.
+.
Item to "e #elete# is not in the list.
1, 3, 2 15 45 25
11.


13.
Chapter 19
1. 0 = 3
+ = 3
-
13
4
-
3. a. 2
b. 45
c. ,
d. 23
". a. 7 * < + P
b. &7 + <' * &P . J'
c. &7 . < . P' * J
$. 1! 2! 3! 4! 5!
+.
tem(late 9class elemT+(e:
elemT+(e secon#&stacFT+(e9elemT+(e: stacF'
?
elemT+(e tem(1@ tem(2;
i% &stacF.is4m(t+StacF&''
?
cout 99 AStacF is em(t+.A 99 en#l;
e0it&!'; //terminate the (rogram
D
tem(1 = stacF.to(&';
stacF.(o(&';
i% &stacF.is4m(t+StacF&''
?
cout 99 AStacF has onl+ one element.A 99 en#l;
e0it&!'; //terminate the (rogram
D

tem(2 = stacF.to(&';
stacF.(ush&tem(1';
return tem(2;
D
11. Xueue 4lement = !
Xueue 4lement = 14
Xueue 4lement = 22
Sorr+@ the )ueue is em(t+
Sorr+@ the )ueue is em(t+
StacF 4lement = 32
StacF 4lements5 4 2, !
Xueue 4lements5 3!
13. a. )ueueLront = 5!; )ueueYear = !.
b. )ueueLront = 51; )ueueYear = 33.
1". a. )ueueLront = 25; )ueueYear = -.
b. )ueueLront = 2; )ueueYear = -5.
1$. "1
1+.
tem(late 9class T+(e:
voi# reverseStacF&stacFT+(e9T+(e: Us'
?
linFe#XueueT+(e9T+(e: );
T+(e elem;
while &Ms.is4m(t+StacF&''
?
elem = s.to(&';
s.(o(&';
).a##Xueue&elem';
D
while &M).is4m(t+Xueue&''
?
elem = ).%ront&';
).#eleteXueue&';
s.(ush&elem';
D
D
71.
tem(late 9class T+(e:
int )ueueT+(e9T+(e:55)ueuePount&'
?
return count;
D
73.
7".

You might also like