Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

lafore_robert_objectoriented_programming_in_c

.pdf
Скачиваний:
51
Добавлен:
27.03.2023
Размер:
8.94 Mб
Скачать

8.a. 4

b.10

c.4

d.4

9.false

10.a. integer constant

b.character constant

c.floating-point constant

d.variable name or identifier

e.function name

11.a. cout << ‘x’;

b.cout << “Jim”;

c.cout << 509;

12.false; they’re not equal until the statement is executed

13.cout << setw(10) << george;

14.IOSTREAM

15.cin >> temp;

16.IOMANIP

17.string constants, preprocessor directives

18.true

19.2

20.assignment (=) and arithmetic (like + and *)

21.

temp += 23;

temp = temp + 23;

Answers to Questions and Exercises

915

G

Q

E UESTIONS ANSWERS

TOANDXERCISES

22.1

23.2020

24.to provide declarations and other data for library functions, overloaded operators, and objects

25.library

Appendix G

916

Solutions to Exercises

1.

//ex2_1.cpp

//converts gallons to cubic feet #include <iostream>

using namespace std;

int main()

{

float gallons, cufeet;

cout << “\nEnter quantity in gallons: “; cin >> gallons;

cufeet = gallons / 7.481;

cout << “Equivalent in cublic feet is “ << cufeet << endl; return 0;

}

2.

//ex2_2.cpp

//generates table #include <iostream> #include <iomanip> using namespace std;

int main()

{

cout << 1990 << setw(8) << 135 << endl

<<1991 << setw(8) << 7290 << endl

<<1992 << setw(8) << 11300 << endl

<<1993 << setw(8) << 16200 << endl; return 0;

}

3.

//ex2_3.cpp

//exercises arithmetic assignment and decrement #include <iostream>

using namespace std;

int main()

{

int var = 10;

 

Answers to Questions and Exercises

917

cout << var << endl;

// var is 10

 

 

var *= 2;

// var becomes 20

 

cout << var-- << endl;

// displays var, then decrements it

 

cout << var << endl;

// var is 19

 

return 0;

 

 

}

 

 

Chapter 3

Answers to Questions

1.b, c

2.george != sally

3.–1 is true; only 0 is false.

4.The initialize expression initializes the loop variable, the test expression tests the loop variable, and the increment expression changes the loop variable.

5.c, d

6. true

7.

for(int j=100; j<=110; j++) cout << endl << j;

8.braces (curly brackets)

9.c

10.

int j = 100; while( j <= 110 )

cout << endl << j++;

11.false

12.at least once

int j = 100; do

cout << endl << j++; while( j <= 110 );

14.

if(age > 21)

cout << “Yes”;

G

Q E UESTIONS ANSWERS XERCISES AND TO

15. d

Appendix G

918

16.

if( age > 21 ) cout << “Yes”;

else

cout << “No”;

17.a, c

18.‘\r’

19.preceding, surrounded by braces

20.reformatting

21.

switch(ch)

{

case ‘y’:

cout << “Yes”; break;

case ‘n’:

cout << “No”; break;

default:

cout << “Unknown response”;

}

22.ticket = (speed > 55) ? 1 : 0;

23.d

24.limit == 55 && speed > 55

25.unary, arithmetic, relational, logical, conditional, assignment

26.d

27.the top of the loop

28.b

Solutions to Exercises

1.

//ex3_1.cpp

//displays multiples of a number #include <iostream>

#include <iomanip>

//for setw()

using namespace std;

 

int main()

 

{

 

Answers to Questions and Exercises

919

unsigned long n;

//number

cout << “\nEnter a number: “;

 

cin >> n;

//get number

for(int j=1; j<=200; j++)

//loop from 1 to 200

{

 

cout << setw(5) << j*n << “ “;

//print multiple of n

if( j%10 == 0 )

//every 10 numbers,

cout << endl;

//start new line

}

 

return 0;

 

}

 

2.

//ex3_2.cpp

//converts fahrenheit to centigrad, or

//centigrad to fahrenheit

#include <iostream> using namespace std;

int main()

{

int response; double temper;

cout <<

“\nType 1

to convert fahrenheit to celsius,”

<<

“\n

2

to convert celsius to fahrenheit: “;

cin >> response;

 

 

if( response == 1

)

{

 

 

 

cout

<< “Enter temperature in fahrenheit: “;

cin >> temper;

 

cout

<< “In celsius that’s “ << 5.0/9.0*(temper-32.0);

}

 

 

 

else

{

cout << “Enter temperature in celsius: “; cin >> temper;

cout << “In fahrenheit that’s “ << 9.0/5.0*temper + 32.0;

}

cout << endl; return 0;

}

G

Q E UESTIONS ANSWERS XERCISES AND TO

Appendix G

920

3.

//ex3_3.cpp

//makes a number out of digits #include <iostream>

using namespace std;

#include <conio.h>

//for getche()

int main()

 

 

{

 

 

char ch;

 

 

unsigned

long total = 0;

//this holds the number

cout << “\nEnter a number: “;

 

while( (ch=getche()) != ‘\r’ )

//quit on Enter

total

= total*10 + ch-’0’;

//add digit to total*10

cout << “\nNumber is: “ << total << endl; return 0;

}

4.

//ex3_4.cpp

//models four-function calculator #include <iostream>

using namespace std;

int main()

{

double n1, n2, ans; char oper, ch;

do {

cout << “\nEnter first number, operator, second number: “; cin >> n1 >> oper >> n2;

switch(oper)

{

case ‘+’: ans = n1 + n2; break; case ‘-’: ans = n1 - n2; break; case ‘*’: ans = n1 * n2; break; case ‘/’: ans = n1 / n2; break; default: ans = 0;

}

cout << “Answer = “ << ans;

cout << “\nDo another (Enter ‘y’ or ‘n’)? “; cin >> ch;

} while( ch != ‘n’ ); return 0;

}

Answers to Questions and Exercises

921

Chapter 4

Answers to Questions

1.b, d

2.true

3.semicolon

struct time

{

int hrs; int mins; int secs; };

5.false; only a variable definition creates space in memory

6.c

7.time2.hrs = 11;

8.18 in 16-bit systems (3 structures times 3 integers times 2 bytes), or 36 in 32-bit systems

9.time time1 = { 11, 10, 59 };

10.true

11.temp = fido.dogs.paw;

12.c

13.enum players { B1, B2, SS, B3, RF, CF, LF, P, C };

14.

players joe, tom; joe = LF;

tom = P;

15.a. no

b.yes

c.no

d.yes

16.0, 1, 2

17.enum speeds { obsolete=78, single=45, album=33 };

18.because false should be represented by 0

G

Q E UESTIONS ANSWERS XERCISES AND TO

Appendix G

922

Solutions to Exercises

1.

//ex4_1.cpp

//uses structure to store phone number #include <iostream>

using namespace std;

////////////////////////////////////////////////////////////////

struct phone

{

int area;

//area code

(3 digits)

int exchange;

//exchange (3 digits)

int number;

//number (4

digits)

};

 

 

////////////////////////////////////////////////////////////////

int main()

 

 

 

{

 

 

 

phone ph1 = { 212, 767,

8900 };

//initialize phone number

phone ph2;

 

//define phone number

 

 

 

// get phone no from user

cout <<

“\nEnter your area code,

exchange, and number”;

cout <<

“\n(Don’t use leading zeros): “;

cin >> ph2.area >> ph2.exchange >> ph2.number;

cout <<

“\nMy number is

//display numbers

<<

‘(‘ << ph1.area

<< “) “

 

<<

ph1.exchange <<

‘-’ << ph1.number;

cout << “\nYour number is “

<<‘(‘ << ph2.area << “) “

<<ph2.exchange << ‘-’ << ph2.number << endl; return 0;

}

2.

//ex4_2.cpp

//structure models point on the plane #include <iostream>

using namespace std;

////////////////////////////////////////////////////////////////

struct point

{

int xCo;

//X

coordinate

int yCo;

//Y

coordinate

};

 

 

////////////////////////////////////////////////////////////////

 

 

Answers to Questions and Exercises

int main()

 

 

 

 

{

 

 

point p1, p2, p3;

//define 3 points

cout << “\nEnter coordinates for p1: “;

//get 2 points

cin >>

p1.xCo >> p1.yCo;

//from user

cout << “Enter coordinates for p2: “;

 

cin >>

p2.xCo >> p2.yCo;

 

p3.xCo

= p1.xCo + p2.xCo;

//find sum of

p3.yCo

= p1.yCo + p2.yCo;

//p1 and p2

cout << “Coordinates of p1+p2 are: “

//display the sum

<< p3.xCo << “, “ << p3.yCo << endl;

 

return

0;

 

}

 

 

3.

//ex4_3.cpp

//uses structure to model volume of room #include <iostream>

using namespace std;

////////////////////////////////////////////////////////////////

struct Distance

{

int feet; float inches; };

////////////////////////////////////////////////////////////////

struct Volume

{

Distance length; Distance width; Distance height; };

////////////////////////////////////////////////////////////////

int main()

{

float l, w, h;

Volume room1 = { { 16, 3.5 }, { 12, 6.25 }, { 8, 1.75 } };

l = room1.length.feet + room1.length.inches/12.0; w = room1.width.feet + room1.width.inches /12.0; h = room1.height.feet + room1.height.inches/12.0;

cout << “Volume = “ << l*w*h << “ cubic feet\n”; return 0;

}

923

G

Q E UESTIONS ANSWERS XERCISES AND TO

Appendix G

924

Chapter 5

Answers to Questions

1.d (half credit for b)

2.definition

3.

void foo()

{

cout << “foo”;

}

4.declaration, prototype

5.body

6.call

7.declarator

8.c

9.false

10.To clarify the purpose of the arguments

11.a, b, c

12.Empty parentheses mean the function takes no arguments

13.one

14.Ttrue

15.at the beginning of the declaration and declarator

16.void

17.

main()

 

 

 

{

 

 

 

int

times2(int);

//

prototype

int

alpha = times2(37);

//

function call

}

 

 

 

18.d

19.to modify the original argument (or to avoid copying a large argument)

20.a, c

21.

int bar(char);

int bar(char, char);