
lafore_robert_objectoriented_programming_in_c
.pdf

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;


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() |
|
{ |
|


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;
}


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 |
}; |
|
|
////////////////////////////////////////////////////////////////


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);