-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 2.3 (Completed)
333 lines (313 loc) · 11.4 KB
/
Exercise 2.3 (Completed)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
1. (Practice) State whether the following variable names are valid.
If they’re invalid, state the reason.
prod_a valid
newbal valid
9ab6 invalid (begins with a number)
c1234 valid
while invalid (C++ keyword)
sum.of invalid (decimal point not allowed)
abcd valid
$total invalid (begins with special character)
average valid
_c3 invalid (begins with special character)
new bal invalid (contains a space)
grade1 valid
12345 invalid (begins with a number)
a1b2c3d4 valid
finGrad valid
2. (Practice) State whether the following variable names are valid. If they’re invalid, state the
reason. Also, indicate which of the valid variable names shouldn’t be used because they convey
no information about the variable.
salestax valid
harry valid (not meaningful)
maximum valid
3sum invalid (begins with a number)
a243 valid (not meaningful)
sue valid (not meaningful)
okay valid
for invalid (C++ keyword)
r2d2 valid (not meaningful)
c3p0 valid (not meaningful)
a valid (not meaningful)
tot.al invalid (contains decimal point)
firstNum valid
average valid
awesome valid (not meaningful)
c$five invalid (contains a special character)
cc_al valid (not meaningful)sum valid
goforit valid (not meaningful)
netpay valid
3. (Practice)
a. Write a declaration statement to declare that the variable count will be used to
store an integer.
-----int count;
b. Write a declaration statement to declare that the variable grade will be used to store a
floating-point number.
-----float grade;
c. Write a declaration statement to declare that the variable yield will be used to store a
double-precision number.
-----double yield;
d. Write a declaration statement to declare that the variable initial will be used to store a
character.
-----char initial;
4. (Practice) Write a single declaration statement for each set of variables:
a. num1, num2, and num3 used to store integer numbers
int num1, num2, num3;
b. grade1, grade2, grade3, and grade4 used to store double-precision numbers
double grade1, grade2, grade3;
c. temp1, temp2, and temp3 used to store double-precision numbers
double temp1, temp2, temp3;
d. let1, let2, let3, and let4 used to store characters
char let1, let2, let3, let4;
5. (Practice) Write a single declaration statement for each set of variables:
a. firstnum and secnum used to store integers
int firstnum, secnum;
b. price, yield, and coupon used to store double-precision numbers
double price, yield, coupon
c. average used to store a double-precision number
double average;
6. (Modify) Rewrite each of these declaration statements as three separate declarations:
a. int month, day = 30, year;
int month;
int day = 30;
int year;
b. double hours, volt, power = 15.62;
double hours;
double volt;
double power = 15.62;
c. double price, amount, taxes;
double price;
double amount;
double taxes;
d. char inKey, ch, choice = 'f';
char inKey;
char ch;
char choice = 'f';
7. (Desk check)
a. Determine what each statement causes to happen in the following program:
#include <iostream> // include standard input and output
using namespace std; // using the standard library
int main() // beginning of main function
{
int num1, num2, total; // declartion of num1, num2, total
num1 = 25; // assigning value to num1
num2 = 30; // assigning value to num 2
total = num1 + num2; // algorithm for the function
cout << “The total of ” << num1 << “ and “ // Displays num1 and num 2
<< num2 << “ is “ << total << endl; // the total is displayed using the simple algorithm
return 0; // returns nothing to the console
}
b. What output will be displayed when the program in Exercise 7a runs?
The total of 25 and 30 is 55
8. (Program) Write a C++ program that stores the sum of the integer numbers 12 and 33 in a
variable named sum. Have your program display the value stored in sum.
#include <iostream>
using namespace std;
int main()
{
int sum;
sum = 12 + 33;
cout << "Displaying the value of sum" << sum << endl;
return 0;
}
9. (Program) Write a C++ program that stores the integer value 16 in the variable length and
the integer value 18 in the variable width. Have your program calculate the value assigned to
the perimeter variable, using this formula:
perimeter = 2 × (length + width)
Your program should then display the value stored in perimeter. Be sure to declare all variables
as integers at the beginning of the main() function.
#include <iostream>
using namespace std;
int main()
{
int length, width;
length = 16;
width = 18;
perimeter = 2 *(length + width);
cout << "Displaying the value stored in perimeter" << perimeter << endl;
return 0;
}
10. (Program) Write a C++ program that stores the integer value 16 in the variable num1 and the
integer value 18 in the variable num2. (Be sure to declare the variables as integers.) Have your
program calculate the total of these numbers and their average. Store the total in an integer
variable named total and the average in an integer variable named average. (Use the statement
average = total/2.0; to calculate the average.) Use a cout statement to display the
total and average.
#include <iostream>
using namespace std;
int main()
{
int num1 =16;
int num2 = 18;
total = num1 + num2;
average = total/2;
return 0;
}
11. (Debug) Enter, compile, and run the following program. Determine why an incorrect average
is displayed and correct the error.
#include <iostream>
using namespace std;
int main()
{
int num1 = 15;
int num2 = 18;
int total, average;
total = num1 + num2;
average = total / 2.0;
cout << “The average of “ << num1
<< “ and “ << num2 << “ is “
8 << average << endl;
return 0;
}
-------------------Corrected below-----------------------------
#include <iostream>
using namespace std;
int main()
{
float num1 = 15;
float num2 = 18;
float total, average;
total = num1 + num2;
average = total / 2.0;
cout << “The average of “ << num1
<< “ and “ << num2 << “ is “
<< average << endl;
return 0;
}
*The output for this particular situation requires a floating point declartion in order to produce the desired result.
12. (Debug) The following program was written to correct the error produced by the program in
Exercise 11. Determine why this program also doesn’t provide the correct result and correct
the error.
#include <iostream>
using namespace std;
int main()
{
int num1 = 15;
int num2 = 18;
int total;
double average;
total = num1 + num2;
average = total / 2;
cout << “The average of “ << num1
<< “ and “ << num2 << “ is “
<< average << endl;
return 0;
}
-------------------------------------------
#include <iostream>
using namespace std;
int main()
{
int num1 = 15;
int num2 = 18;
double total;
double average;
total = num1 + num2;
average = total / 2;
cout << “The average of “ << num1
<< “ and “ << num2 << “ is “
<< average << endl;
return 0;
}
*The varable total should be declared a floating point.
13. (Program) Write a C++ program that stores the number 105.62 in the variable firstnum,
89.352 in the variable secnum, and 98.67 in the variable thirdnum. (Be sure to declare the
variables first as float or double.) Have your program calculate the total of the three numbers
and their average. The total should be stored in the variable total and the average in the
variable average. (Use the statement average = total /3.0; to calculate the average.)
Use a cout statement to display the total and average.
---------------------------------------------------------
#include <iostream>
using namespace std;
{
float firstnum = 105.62;
float secnum = 89.352;
float thirdnum = 98.67;
total = firstnum + secnum + thirdnum;
average = total/3.0;
cout << "The total and average of hidden code is" << total << average << endl;
return 0;
}
14. (For thought) a. A statement used to clarify the relationship between squares and rectangles
is “All squares are rectangles but not all rectangles are squares.” Write a similar statement that
describes the relationship between definition and declaration statements.
"All definitions are declarations but not all declarations are definitions."
b. Why must a variable be defined before any other C++ statement that uses the variable?
A variable must be defined in order to have a value to act upon.
Note for Exercises 15 to 17: Assume that a character requires 1 byte of storage, an integer requires
4 bytes, a single-precision number requires 4 bytes, and a double-precision number requires 8 bytes.
Variables are assigned storage in the order they’re declared. (Review Section 2.6 if you’re unfamiliar
with the concept of a byte.) Refer to Figure 2.7 for these exercises.
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
Figure 2.7 Memory bytes for Exercises 15 to 17
15. (Practice) a. Using Figure 2.7 and assuming the variable name rate is assigned to the byte
at memory address 159, determine the addresses corresponding to each variable declared in
the following statements. Also, fill in the correct number of 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;
rate = 159,160,161,162
ch1 = 163
ch2 = 164
ch3 = 165
ch4 = 166
taxes =167,168,169,170,171,172,173,174
num = 175,176,177,178
count = 179,180,181,182
b. Repeat Exercise 15a, but substitute the actual byte patterns that a computer using the
ASCII code would use to store characters in the variables ch1, ch2, ch3, and ch4. (Hint : Use
Table 2.2.)
ch1 = 01001101
ch2 = 01000101
ch3 = 01001100
ch4 = 01010100
16. (Practice) a. Using Figure 2.7 and assuming the variable named cn1 is assigned to the byte
at memory address 159, determine the addresses corresponding to each variable declared in
the following statements. Also, fill in the correct number of bytes with the initialization data
included in the declaration statements. (Use letters for the characters, not the computer codes
that would actually be stored.)
char cn1 = 'P', cn2 = 'E', cn3 = 'R', cn4 = 'F', cn5 = 'E';
char cn6 = 'C', cn7 = 'T', key = '\\', sch = '\'', inc = 'A';
char inc1 = 'T';
cn1 = 159
cn2 = 160
cn3 = 161
cn4 = 162
cn5 = 163
cn6 = 164
cn7 = 165
key = 166
sch = 167
inc = 168
inc1 = 169
b. Repeat Exercise 16a, but substitute the actual byte patterns a computer using the ASCII
code would use to store characters in each declared variable. (Hint: Use Tables 2.2 and 2.3.)
cn1 = 01010000
cn2 = 01000101
cn3 = 01010010
cn4 = 01000110
cn5 = 01000101
cn6 = 01000011
cn7 = 01010100
key = 01011100
sch = 00100111
inc = 01000001
inc1 = 01010100
17. (Practice) Using Figure 2.7 and assuming 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;
miles = 159,160,161,162
count = 163,164,165,166
num = 167,168,169,170
dist = 171,172,173,174,175,176,177,178
temp = 179,180,181,182,183,184,185,186