-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 2.1 (Completed)
158 lines (152 loc) · 5.61 KB
/
Exercise 2.1 (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
1. (Practice) Determine data types suitable for the following data:
a. The average of four grades
float or double
b. The number of days in a month
int
c. The length of the Golden Gate Bridge
float or double
d. The numbers in a state lottery
int
e. The distance from Brooklyn, N.Y. to Newark, N.J.
float or double
f. The single-character prefix that specifies a component type
char
2. (Practice) Compile and run Program 2.1.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "\nData Type Bytes";
cout << "\n-------- - ---- - ";
cout << "\nint " << sizeof(int);
cout << "\nchar " << sizeof(char);
cout << "\nbool " << sizeof(bool);
cout << '\n';
system("PAUSE");
return 0;
}
3. (Modify) Modify Program 2.1 to determine the storage your compiler uses for all the C++
integer data types.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "\nData Type Bytes";
cout << "\n-------- - ---- - ";
cout << "\nint " << sizeof(int);
cout << "\nchar " << sizeof(char);
cout << "\nbool " << sizeof(bool);
cout << "\nshort int " << sizeof(short int);
cout << "\nlong int " << sizeof(long int);
cout << "\nunsigned char " << sizeof(unsigned char);
cout << "\nunsigned short int " << sizeof(unsigned short int);
cout << "\nunsigned int " << sizeof(unsigned int);
cout << "\nunsigned long int " << sizeof(unsigned long int);
cout << '\n';
system("PAUSE");
return 0;
}
4. (Practice) Show how the name KINGSLEY is stored in a computer that uses the ASCII code
by drawing a diagram similar to Figure 2.3, shown previously.
8 bytes of storage
01001011 01001001 01001110 01000111 01010011 01001100 01000101 01011001
-------- -------- -------- -------- -------- -------- -------- --------
K I N G S L E Y
5. (Practice) Repeat Exercise 4, using the letters of your own last name.
01010000 01000001 01010100 01010010 01001001 01000011 01001011
-------- -------- -------- -------- -------- -------- --------
P A T R I C K
6. (Modify) Modify Program 2.1 to determine how many bytes your compiler assigns to the
float, double, and long double data types.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "\nData Type Bytes";
cout << "\n-------- - ---- - ";
cout << "\nfloat " << sizeof(float); //4
cout << "\ndouble " << sizeof(double); //8
cout << "\nlong double " << sizeof(long double); //8
cout << '\n';
system("PAUSE");
return 0;
}
7. (Practice) Convert the following numbers from exponential form to standard decimal form:
a. 6.34e5
634000
b. 1.95162e2
195.162
c. 8.395e1
83.95
d. 2.95e-3
f.00295
e. 4.623e-4
.0004623
8. (Practice) Convert the following numbers from scientific notation to standard decimal form:
a. 2.67 X 10^3
2670
b. 2.67 X 10^-3
.00267
c. 1.872 X 10^9
1872000000
Id. 1.872 X 10^-9
.000000001872
e. 6.6256 X 10^-34
.0000000000000000000000000000000066256
59. (Practice) Write the following decimal numbers in scientific notation:
a. 126.
1.26 x 10^2
b. 656.23
6.5623 x 10^2
c. 3426.95
3.42695 x 10^3
d. 4893.2
4.8932 x 10^3
e. .321
3.21 x 10^-1
f. .0123
1.23 x 10^-2
g. .006789
6.789 x 10^-3
10. (For thought) Because computers use different representations for storing integer, floatingpoint,
double-precision, and character values, discuss how a program might alert the computer
to the data types of different values it will be using.
A program's complier may alert the developer to a data type differents by the the current data type for compatablity.
The resulting conflict would than flagged as an error and a suggested data type by the complier could be tested.
11. (For thought) Although you have concentrated on operations involving integer and floatingpoint
numbers, C++ allows adding and subtracting characters and integers. (These operations
are possible with characters because they’re integer data types and are stored by using integer
codes.) Therefore, characters and integers can be mixed in arithmetic expressions. For example,
if your computer uses the 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 information as background,
determine the character results of the following expressions. (Assume all characters
are stored by using ASCII codes.)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
a. 'm'- 5 = 'h'
b. 'm'+ 5 = 'r'
c. 'G'+ 6 = 'M'
d. 'G'- 6 = 'A'
e. 'b'+ 7 = 'i'
f. 'g'- 1 = 'f'
g. 'G'- 1 ='F'
12. (Practice) Although the total number of bytes varies from computer to computer, memory
sizes of 65,536 to more than several million bytes are common. In computer language, the
letter K represents the number 1024, which is 2 raised to the 10th power, and M represents the
number 1,048,576, which is 2 raised to the 20th power. Therefore, a memory size of 640 KB is
really 640 times 1024 (655,360 bytes), and a memory size of 4 MB is really 4 times 1,048,576
(4,194,304 bytes). Using this information, calculate the actual number of bytes in the following:
a. A memory containing 512 MB
512 x 1,048,576 = 536,870,912 bytes
b. A memory consisting of 256 MB words, with each word consisting of 2 bytes
(256 x 1,048,576) x 2 = 536,870,912 bytes
c. A memory consisting of 256 MB words, with each word consisting of 4 bytes
(256 x 1,048,576) x 4 = 1,073,741,824 bytes
d. A thumb drive that specifies 2 MB
2 x 1,048,576 = 2,097,152 bytes
e. A disk that specifies 250 MB
250 x 1,048,576 = 262,144,000 bytes
f. A disk that specifies 8 GB (Hint: See Table 2.6.)
8 x 1,073,741,824 = 8,589,934,592 bytes