Sie sind auf Seite 1von 8

CHAPTER 2

Exercises 2.1
1. State if the following are valid function names. If they are valid,
state if they are mnemonic names. (Recall that a mnemonic function
name conveys some idea about the functions purpose). If they are
invalid names, state why.
a)
b)
c)
d)
e)
f)
g)
h)
i)

power
total
volts$
cosine
density
tangent
a2B3
speed
m1234

j)
k)
l)
m)
n)
o)
p)
q)

absval
while
netdistance
newamp
computed
minval
sum
1234

r)
s)
t)
u)
v)
w)
x)

b34a
sine
return
abcd
34ab
$sine
Stack

2. Assume that the following functions have been written:


getLength(),

getWidth(), calcArea(),

displayArea()

a) From the functions names, what do you think each function might
do?
b) In what order do you think a main() function might execute these
functions (based on their names)?

3. Assume that the following functions have been written:


speed(),

distance(),

acceleration()

From the functions names, what do you think each function might do?

4. Determine names for functions that do the following:


a) Find the average of a set of numbers.
b) Find the area of a rectangle.
c) Find the minimum value in a set of numbers.
d) Find the density of a steel door.
e) Sort a set of numbers from lowest to highest.

5. Just as the keyword int is used to signify that a function will


return an integer, the keywords void, char, float, and double are
used to signify that a function will return no value, a character, a
single-precision number, and a double-precision number, respectively.
Using this information, write header lines for main() function that
will receive no arguments but will return:
a)
b)
c)
d)

no value
a character
a single-precision number
a double-precision number

Prepared by: Miss Azlina Din

6. Using cout, write a C++ program that displays your name on one line,
your street address on a second line, and your city, state, and zip
code on a third line. Run the program you have written on a computer.

7. Write a C++ program to display the following:


The cosecant of an angle
is equal to one over
the sine of the angle.
Compile and run the program you have written on a computer.

8. a)

How many cout statement would you use to display the following:
Degrees
0
90
180
270
360

Radians
0.0000
1.5708
3.1416
4.7124
6.2832

b)

What is the minimum number of cout statements that could be used


to print the table in Exercise 8a?

c)

Write a complete C++ program to produce the output illustrated in


Exercise 8.a).

d)

Run the program you have written for Exercise 8.c) on a computer.

9. Assuming a case-insensitive compiler, determine which of these


program unit names are equivalent:
AVERAG
Total
denSTY

averag
besseL
MEAN

MODE
TeMp
total

BESSEL
Densty
mean

Mode
TEMP
moDE

Exercises 2.2
1. Will the following program work?
#include <iostream>
using namespace std;
int main() {cout << Hello there world!; return 0;}

2. Rewrite the following programs to conform to good programming practice


and correct syntax.
a)
#include <iostream>
int main(
){
cout
<<
The time has come
; return 0;}

Prepared by: Miss Azlina Din

b)
#include <iostream>
using namespace std;
int main
(
) {cout << Newark is a city\n; cout <<
In New Jersey\n; cout <<
It is also a city\n
; cout << In Delaware\n
; return 0;}

c)
#include <iostream>
using namespace std;
int main() {cout << Reading a program\n;cout <<
is much easier\n
; cout << if a standard form for main is used\n)
; cout
<< and each statement is written\n;cout
<<
on a line by itself\n)
; return 0;}

d)
#include <iostream.h>
using namespace std;
int main
(
){cout << Every C++ program
;cout
<<\nmust have one and only one
;
cout << main function
;
cout <<
\n the escape sequence of characters)
; cout <<
\nfor a newline can be placed anywhere
; cout
<< \n within the message passed to cout
; return 0;}

3. a)

b)

When used in message, the backslash character alters the meaning of


the character immediately following it. If we wanted to print the
backslash character, we would have to tell cout to escape from the
way it normally interprets the backslash. What character do you
think is used to alter the way a single backslash character is
interpreted?
Using your answer to Exercise 3.a), write the escape sequence for
printing a backslash.

Prepared by: Miss Azlina Din

Exercises 2.3
1. Determine data types appropriate for the following data:
a) the average of four grades
b) the number of days in a month
c) the length of the Penang Bridge
d) the numbers in a state lottery
e) the distance from Merlimau, Malacca to Arau, Perlis.

2. Modify the following program to determine the storage used by your


compiler for all C++s integer data types.
#include <iostream>
using namespace std;
int main()
{
cout << \nData Type Bytes
<< \n--------- -----
<< \nint
<<sizeof(int)
<< \nchar
<<sizeof(char)
<< \nbool
<<sizeof(bool)
<< \n;
return 0;
}

3. Show how the name KINGSLEY would be stored inside a computer that uses
the ASCII code. That is, draw a figure similar to the following for the
name KINGSLEY.

4. Although we have concentrated on operations involving integer and


floating-point numbers, C++ allows characters and integers to be added
or subtracted. This can be done because a character is stored using an
integer code (it is an integer data type.) Thus, characters and
integers can be freely mixed in arithmetic expressions. For example, if
your computer uses ASCII code, the expression a + 1 equals b, and
z 1 equals y. Similarly, A + 1 is B, and Z 1 is Y. With
this as background, determine the character results of the following
expressions. (Assume that all characters are stored using the ASCII
code.)
a) m 5
b) m + 5
c) G + 6
d) G 6
e) b a
f) g a + 1
g) G A + 1

Prepared by: Miss Azlina Din

Exercises 2.4
1.

2.

3.

4.

Listed below are correct algebraic expressions and incorrect C++


expressions corresponding to them. Find the errors and write corrected
C++ expressions.
Algebra
C++ Expression
a) (2)(3) + (4)(5)
(2)(3) + (4)(5)
b) 6 + 18
-----2

6 + 18 / 2

c) 4.5
---------12.2 3.1

4.5 / 12.2 3.1

d) 4.6(3.0 + 14.9)

4.6(3.0 + 14.9)

e) (12.1 + 18.9)(15.3 3.8)

(12.1 + 18.9)(15.3 3.8)

Determine the value of the following integer expressions:


a) 3 + 4 * 6

f)

20 2 / (6 + 3)

b) 3 * 4 / 6 + 6

g)

(20 2) / 6 + 3

c) 2 * 3 / 12 * 8 / 4

h)

(20 2) / (6 + 3)

d) 10 * (1 + 7 * 3)

i)

50 % 20

e) 20 2 / 6 + 3

j)

(10 + 3) % 4

Determine the value of the following floating-point expressions:


a) 3.0 + 4.0 * 6.0

e)

20.0 2.0 / 6.0 + 3.0

b) 3.0 * 4.0 / 6.0 + 6.0

f)

20.0 2.0 / (6.0 + 3.0)

c) 2.0 * 3.0 / 12.0 * 8.0 / 4.0

g)

(20.0 2.0) / 6.0 + 3.0

d) 10.0 * (1.0 + 7.0 * 3.0)

h)

(20.0 2.0) / (6.0 + 3.0)

Evaluate the following mixed-mode expressions and list the data type
of the result. In evaluating the expressions, be aware of the data
types of all intermediate calculations.
a) 10.0 + 15 / 2 + 4.3

f) 10 + 17 * 3 + 4

b) 10.0 + 15.0 / 2 + 4.3

g) 10 + 17 / 3. + 4

c) 3.0 * 4 / 6 + 6

h) 3.0 * 4 % 6 + 6

d) 3.0 * 4.0 / 6 + 6

i) 10 + 17 % 3 + 4.

e) 20.0 2 / 6 + 3

Prepared by: Miss Azlina Din

5.

Assume that amount stores the integer value 1, m stores the integer
value 50, n stores the integer value 10, and p stores the integer
value 5. Evaluate the following expressions:
a) n / p + 3

f) p * n

b) m / p + n 10 * amount

g) m / 20

c) m 3 * n + 4 * amount

h) (m + n) / (p + amount)

d) amount / 5

i) m + n / p + amount

e) 18 / p

6.

Repeat Exercise 5, assuming that amount stores the value 1.0, m stores
the value 50.0, n stores the value 10.0, and p stores the value 5.0.
a) n / p + 3

f) p * n

b) m / p + n 10 * amount

g) m / 20

c) m 3 * n + 4 * amount

h) (m + n) / (p + amount)

d) amount / 5

i) m + n / p + amount

e) 18 / p

7.

Determine the output of the following program:


#include <iostream>
using namespace std;
int main() //a program illustrating integer truncation
{
cout << answer1 is the integer << 9 / 4;
cout << answer2 is the integer << 17 / 3;
return 0;
}

8.

Determine the output of the following program:


#include <iostream>
using namespace std;
int main() //a program illustrating the % operator
{
cout << The remainder of 9 divided by 4 is << 9 % 4;
cout << The remainder of 17 divided by 3 is << 17 % 3;
return 0;
}

9.

Write a C++ program that displays the results of the expressions 3.0 *
5.0, 7.1 * 8.3 2.2, and 3.2 / (6.1 * 5). Calculate the value of
these expressions manually to verify that the displayed values are
correct.

Prepared by: Miss Azlina Din

10. Write a C++ program that displays the results of the expressions 15 /
4, 15 % 4, and 15 * 3 - (6 * 4). Calculate the value of these
expressions manually to verify that the displayed values are correct.
Exercises 2.5
1. State whether the following variable names are valid or not. If they
are invalid, state the reason why.
prod_a

sum.of

new$al

newamp

abcd

volts1

9ab6

$total

12345

c1234

average

a1b2c3d4

watts

_c3

finvolt

2. State whether the following variable names are valid or not. If they
are invalid, state the reason why. Also indicate which of the valid
variable names should not be used because they convey no information
about the variable.
current
okay
sue
firstNum

3.

harry

for

goforit

total

maximum

r2d2

tot.al

awesome

3sum

c3p0

netpower

c$five

a243

sum

cc_a1

a) Write a declaration statement to declare that the variable count


will be used to store an integer.
b) Write a declaration statement to declare that the variable volt
will be used to store a floating-point number.
c) Write a declaration statement to declare that the variable power
will be used to store a double-precision number.
d) Write a declaration statement to declare that the variable keychar
will be used to store a character.

4. Write declaration statements for the following variables:


a) num1, num2, and num3 used to store integer numbers.
b) amps1, amps2, amps3,and amps4 used to store double-precision
numbers.
c) codeA, codeB, and codeC used to store character types.

5. Rewrite each of these declaration statements as three individual


declarations.
a) int month, day = 30, year;
b) double hours, volt, power = 15.62;
c) double price, amount, taxes;
d) char inKey, ch, choice = f;

Prepared by: Miss Azlina Din

6. Determine what each statement causes to happen in the following program


and what is the output that will be printed when the program run.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, total;
num1 = 25;
num2 = 30;
total = num1 + num2;
cout << The total of << num1 << and
<< num2 << is << total << endl;
return 0;
}

NOTE FOR EXERCISES 7 AND 8: Assume that a character requires one byte of storage, an
integer four bytes, a single-precision number four bytes, a double-precision number eight bytes,
and that variables are assigned storage in the order they are declared.
Addresses
159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

7. a) Using the above figure and assuming that the variable name rate is
assigned to the byte having memory address 159, determine the
addresses corresponding to each variable declared in the following
statements. Also fill in the appropriate bytes with the
initialization data included in the declaration statements. ( Use
letters for the characters, not the computer codes that would
actually be stored.)
float rate;
char ch1 = M, ch2 = E, ch3 = L, ch4 = T;
double taxes;
int num, count = 0;

b) Repeat exercise 7.a), but substitute the actual byte by patterns


that a computer using the ASCII code would use to store the
characters in the variables ch1, ch2, ch3, and ch4.

8. Using the above figure and assuming that the variable name miles is
assigned to the byte at memory address 159, determine the addresses
corresponding to each variable declared in the following statements.
float miles;
int count, num;
double dist, temp;

Prepared by: Miss Azlina Din

Das könnte Ihnen auch gefallen