-
Notifications
You must be signed in to change notification settings - Fork 487
/
exam1-s08.cpp.html
125 lines (111 loc) · 14.8 KB
/
exam1-s08.cpp.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>exam1-s08.cpp</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">// CS 216 exam 1 code</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdio.h></span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<i><span style="color:#9A1900">/* This function will print out the results of the memory diagram</span></i>
<i><span style="color:#9A1900"> * question (question 13). The lines that are commented out in the</span></i>
<i><span style="color:#9A1900"> * function are the ones that cause errors: the first a compile-time</span></i>
<i><span style="color:#9A1900"> * error, the last two a run-time error.</span></i>
<i><span style="color:#9A1900"> */</span></i>
<span style="color:#009900">void</span> <b><span style="color:#000000">memoryDiagramQuestion</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Memory diagram question output: "</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#009900">int</span> a <span style="color:#990000">=</span> <span style="color:#993399">7</span><span style="color:#990000">;</span>
<span style="color:#009900">int</span> <span style="color:#990000">*</span>b <span style="color:#990000">=</span> <span style="color:#990000">&</span>a<span style="color:#990000">;</span>
<span style="color:#009900">int</span> <span style="color:#990000">&</span>c <span style="color:#990000">=</span> a<span style="color:#990000">;</span>
<span style="color:#009900">int</span> d<span style="color:#990000">[</span><span style="color:#993399">5</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> <span style="color:#FF0000">{</span> <span style="color:#993399">1</span><span style="color:#990000">,</span> <span style="color:#993399">2</span><span style="color:#990000">,</span> <span style="color:#993399">3</span><span style="color:#990000">,</span> <span style="color:#993399">4</span><span style="color:#990000">,</span> <span style="color:#993399">5</span> <span style="color:#FF0000">}</span><span style="color:#990000">;</span>
<span style="color:#009900">int</span> <span style="color:#990000">*</span>e <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> <span style="color:#009900">int</span><span style="color:#990000">[</span><span style="color:#993399">5</span><span style="color:#990000">];</span>
<span style="color:#009900">int</span> <span style="color:#990000">*</span>f <span style="color:#990000">=</span> NULL<span style="color:#990000">;</span>
<span style="color:#009900">int</span> <span style="color:#990000">&</span>g <span style="color:#990000">=</span> <span style="color:#990000">*</span>f<span style="color:#990000">;</span>
<i><span style="color:#9A1900">//c = b;</span></i>
c <span style="color:#990000">=</span> <span style="color:#990000">*</span>b<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"a: "</span> <span style="color:#990000"><<</span> a <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"b: "</span> <span style="color:#990000"><<</span> b <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"*b: "</span> <span style="color:#990000"><<</span> <span style="color:#990000">*</span>b <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"c: "</span> <span style="color:#990000"><<</span> c <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"d: "</span> <span style="color:#990000"><<</span> d <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"e: "</span> <span style="color:#990000"><<</span> e <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"*e: "</span> <span style="color:#990000"><<</span> <span style="color:#990000">*</span>e <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"f: "</span> <span style="color:#990000"><<</span> f <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">//cout << "*f: " << *f << endl;</span></i>
<i><span style="color:#9A1900">//cout << "g: " << g << endl;</span></i>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// The array that is declared in question 3</span></i>
<span style="color:#009900">int</span> x<span style="color:#990000">[</span><span style="color:#993399">3</span><span style="color:#990000">][</span><span style="color:#993399">2</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> <span style="color:#FF0000">{</span> <span style="color:#FF0000">{</span><span style="color:#993399">1</span><span style="color:#990000">,</span><span style="color:#993399">2</span><span style="color:#FF0000">}</span><span style="color:#990000">,</span> <span style="color:#FF0000">{</span><span style="color:#993399">3</span><span style="color:#990000">,</span><span style="color:#993399">4</span><span style="color:#FF0000">}</span><span style="color:#990000">,</span> <span style="color:#FF0000">{</span><span style="color:#993399">5</span><span style="color:#990000">,</span><span style="color:#993399">6</span><span style="color:#FF0000">}</span> <span style="color:#FF0000">}</span><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// The union used for questin 12</span></i>
<b><span style="color:#0000FF">union</span></b> <span style="color:#008080">foo</span> <span style="color:#FF0000">{</span>
<span style="color:#009900">int</span> x<span style="color:#990000">;</span> <i><span style="color:#9A1900">// used to write the int value</span></i>
<span style="color:#009900">float</span> f<span style="color:#990000">;</span> <i><span style="color:#9A1900">// used to read the floating point value</span></i>
<span style="color:#009900">int</span> <span style="color:#990000">*</span>p<span style="color:#990000">;</span> <i><span style="color:#9A1900">// used to print out the values in hex</span></i>
<span style="color:#009900">unsigned</span> <span style="color:#009900">char</span> c<span style="color:#990000">[</span><span style="color:#993399">4</span><span style="color:#990000">];</span> <i><span style="color:#9A1900">// used to swap the bytes</span></i>
<span style="color:#FF0000">}</span><span style="color:#990000">;</span>
<i><span style="color:#9A1900">/* This function will compute the result of the endian number question</span></i>
<i><span style="color:#9A1900"> * (question 12). Because the x86 writes both the floating point</span></i>
<i><span style="color:#9A1900"> * number and the int value both in the same endian format, the bytes</span></i>
<i><span style="color:#9A1900"> * have to be switched to simulate what was asked in the question.</span></i>
<i><span style="color:#9A1900"> */</span></i>
<span style="color:#009900">void</span> <b><span style="color:#000000">endianNumberQuestion</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// Write the int to the union, and print out the value in hex. This</span></i>
<i><span style="color:#9A1900">// is also the value expected for the first part of the 'partial</span></i>
<i><span style="color:#9A1900">// credit' option for this question.</span></i>
<span style="color:#008080">foo</span> bar<span style="color:#990000">;</span>
bar<span style="color:#990000">.</span>x <span style="color:#990000">=</span> <span style="color:#993399">2113</span><span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Decimal value: "</span> <span style="color:#990000"><<</span> bar<span style="color:#990000">.</span>x <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Hex value before endian swap: "</span> <span style="color:#990000"><<</span> bar<span style="color:#990000">.</span>p <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// Any machine will write both floating point numbers and int</span></i>
<i><span style="color:#9A1900">// numbers in the same endian format. To simulate the exam question</span></i>
<i><span style="color:#9A1900">// (which has one type written in one endian format, and the other</span></i>
<i><span style="color:#9A1900">// one written in the other endian format), the bytes have to be</span></i>
<i><span style="color:#9A1900">// switched. Note that it doesn't matter if this is running on a</span></i>
<i><span style="color:#9A1900">// big-endian machine or a little-endian machine, as the byte swap</span></i>
<i><span style="color:#9A1900">// will just switch the bytes to the 'other' format.</span></i>
<span style="color:#009900">unsigned</span> <span style="color:#009900">char</span> g<span style="color:#990000">;</span>
g <span style="color:#990000">=</span> bar<span style="color:#990000">.</span>c<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">];</span>
bar<span style="color:#990000">.</span>c<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> bar<span style="color:#990000">.</span>c<span style="color:#990000">[</span><span style="color:#993399">3</span><span style="color:#990000">];</span>
bar<span style="color:#990000">.</span>c<span style="color:#990000">[</span><span style="color:#993399">3</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> g<span style="color:#990000">;</span>
g <span style="color:#990000">=</span> bar<span style="color:#990000">.</span>c<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">];</span>
bar<span style="color:#990000">.</span>c<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> bar<span style="color:#990000">.</span>c<span style="color:#990000">[</span><span style="color:#993399">2</span><span style="color:#990000">];</span>
bar<span style="color:#990000">.</span>c<span style="color:#990000">[</span><span style="color:#993399">2</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> g<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// Extract the floating point value</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Hex value after endian swap: "</span> <span style="color:#990000"><<</span> bar<span style="color:#990000">.</span>p <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Floating point value: "</span> <span style="color:#990000"><<</span> bar<span style="color:#990000">.</span>f <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// This is the second part of the 'partial credit' option for this</span></i>
<i><span style="color:#9A1900">// question.</span></i>
bar<span style="color:#990000">.</span>x <span style="color:#990000">=</span> <span style="color:#993399">0x419c0000</span><span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"0x419c0000 as a float: "</span> <span style="color:#990000"><<</span> bar<span style="color:#990000">.</span>f <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Calls the two defined functions above, and then exits.</span></i>
<span style="color:#009900">int</span> <b><span style="color:#000000">main</span></b><span style="color:#990000">(</span><span style="color:#009900">int</span> argc<span style="color:#990000">,</span> <span style="color:#009900">char</span> <span style="color:#990000">**</span>argv<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">endianNumberQuestion</span></b><span style="color:#990000">();</span>
<b><span style="color:#000000">memoryDiagramQuestion</span></b><span style="color:#990000">();</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">/* Output:</span></i>
<i><span style="color:#9A1900">Decimal value: 2113</span></i>
<i><span style="color:#9A1900">Hex value before endian swap: 0x841</span></i>
<i><span style="color:#9A1900">Hex value after endian swap: 0x41080000</span></i>
<i><span style="color:#9A1900">Floating point value: 8.5</span></i>
<i><span style="color:#9A1900">0x419c0000 as a float: 19.5</span></i>
<i><span style="color:#9A1900">Memory diagram question output:</span></i>
<i><span style="color:#9A1900">a: 7</span></i>
<i><span style="color:#9A1900">b: 0xbfc8e07c</span></i>
<i><span style="color:#9A1900">*b: 7</span></i>
<i><span style="color:#9A1900">c: 7</span></i>
<i><span style="color:#9A1900">d: 0xbfc8e068</span></i>
<i><span style="color:#9A1900">e: 0x804a008</span></i>
<i><span style="color:#9A1900">*e: 0</span></i>
<i><span style="color:#9A1900">f: 0</span></i>
<i><span style="color:#9A1900">*/</span></i>
</pre>
</body>
</html>