Beruflich Dokumente
Kultur Dokumente
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
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)?
distance(),
acceleration()
From the functions names, what do you think each function might do?
no value
a character
a single-precision number
a double-precision number
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.
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)
c)
d)
Run the program you have written for Exercise 8.c) on a computer.
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;}
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)
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.
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.
Exercises 2.4
1.
2.
3.
4.
6 + 18 / 2
c) 4.5
---------12.2 3.1
d) 4.6(3.0 + 14.9)
4.6(3.0 + 14.9)
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
e)
f)
g)
h)
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
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
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.
8.
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.
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
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;
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;