forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
263 lines (262 loc) · 29.6 KB
/
index.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
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title>PDR: Laboratory 4: Number Representation</title>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet" href="../../markdown.css" type="text/css" />
</head>
<body>
<h1 id="pdr-laboratory-4-number-representation">PDR: Laboratory 4: Number Representation</h1>
<p><a href="../index.html">Go up to the Labs table of contents page</a></p>
<h3 id="objective">Objective</h3>
<p>To become familiar with the underlying representation of various data types, and to learn how to examine these representations in the debugger.</p>
<h3 id="background">Background</h3>
<p>In class we discussed how various data types -- integers, characters, and floating point numbers -- were represented in computers. In this lab we will use the debugger to examine some of these representations.</p>
<h3 id="readings">Reading(s)</h3>
<ol style="list-style-type: decimal">
<li>Any of the optional readings on the <a href="../../docs/readings.html">Readings</a> page, in particular, those on arrays and unions, if you feel you need a bit more background. If you are a bit confused about unions, you might want to read about them prior to the in-lab. Do a Google search for 'C++ unions'.</li>
</ol>
<h2 id="procedure">Procedure</h2>
<h3 id="pre-lab">Pre-lab</h3>
<ol style="list-style-type: decimal">
<li>Any of the optional readings on the <a href="../../docs/readings.html">Readings</a> page, in particular, those on arrays and unions, if you feel you need a bit more background.</li>
<li>Go through <a href="../../tutorials/03-04-more-unix/index.html">Tutorial 4: Unix, part 2</a>, which is sections 5-8. This tutorial is originally from the department of Electrical Engineering at the University of Surrey, and is available online <a href="http://www.ee.surrey.ac.uk/Teaching/Unix/">here</a>. You went through sections 1-4 in the last tutorial; this lab has you completing sections 5-8.</li>
<li>Write the <code>sizeOfTest()</code> function (note the capitalization!), as described in the pre-lab section.</li>
<li>Write the <code>outputBinary()</code> function, as described in the pre-lab section.
<ul>
<li>You cannot use the <code>bitset</code> class (or anything similar) for this!</li>
</ul></li>
<li>Write the <code>overflow()</code> function, as described in the pre-lab section.</li>
<li>Combine these functions into a prelab4.cpp file, as described in the pre-lab section. This program should only take in a <em>single</em> <code>int</code> value as input!</li>
<li>Get your floating point number <a href="http://libra.cs.virginia.edu/getfloat.php">here</a>, and then complete your floating point conversion, as described in the pre-lab section, into a file called floatingpoint.pdf; you can convert a file into a PDF via the directions on the <a href="../../docs/convert_to_pdf.html">How to convert a file to PDF</a> page.
<ul>
<li>Note that this must be typed up in a document; you cannot do this by hand and scan it in (or take a photo of it)</li>
<li>Make sure you read the part, in the pre-lab section below, about the maximum file size! Otherwise your submission will not be accepted.</li>
</ul></li>
<li>Files to download: none</li>
<li>Files to submit: prelab4.cpp, floatingpoint.pdf</li>
</ol>
<h3 id="in-lab">In-lab</h3>
<ol style="list-style-type: decimal">
<li>Work through the steps in the in-lab one a time. Be sure that you understand what is happening at each step.</li>
<li>The three parts of the in-lab, described below, have you editing both <a href="inlab4.doc">inlab4.doc</a> and inlab4.cpp.
<ol style="list-style-type: decimal">
<li>Size of C++ data types</li>
<li>Representations in memory</li>
<li>Primitive arrays in C++</li>
</ol></li>
<li>Files to download: <a href="inlab4.doc">inlab4.doc</a></li>
<li>Convert inlab4.doc to a PDF via the directions on the <a href="../../docs/convert_to_pdf.html">How to convert a file to PDF</a> page</li>
<li>Files to submit: inlab4.pdf (the PDF version of inlab4.doc), inlab4.cpp</li>
</ol>
<h3 id="post-lab">Post-lab</h3>
<ol style="list-style-type: decimal">
<li>Write the recursive bit counter, in bitCounter.cpp, as described in the post-lab section.</li>
<li>Complete the radix worksheet -- this worksheet is in the <a href="radixWorksheet.doc">radixWorksheet.doc</a> file. This will be converted to PDF and called radixWorksheet.pdf.</li>
<li>Files to download: <a href="radixWorksheet.doc">radixWorksheet.doc</a></li>
<li>Files to submit: bitCounter.cpp, radixWorksheet.pdf</li>
</ol>
<hr />
<h2 id="pre-lab-1">Pre-lab</h2>
<p>One of the deliverables for the pre-lab is a PDF document named floatingpoint.pdf. It must be in PDF format! See <a href="../../docs/convert_to_pdf.html">How To Convert A File To PDF</a> for details.</p>
<h3 id="reading">Reading</h3>
<p>You should see the <a href="../../docs/readings.html">Readings</a> on <a href="../../docs/readings.html#arrays">arrays</a> -- that will be needed in the in-lab. Also, the <a href="../../docs/readings.html">Readings</a> on <a href="../../docs/readings.html#structsunions">unions</a>.</p>
<h3 id="sizeoftest">sizeOfTest()</h3>
<p>The size of C++ data types is dependent on the underlying hardware on which you are running. A programmer may determine the size of various data types by using the <code>sizeof()</code> operator. Although it looks like a function, it's a language construct -- somewhat like <code>while()</code> or <code>if()</code> -- so it's technically an operator. <code>sizeof()</code> returns the size, in bytes, of a given variable or data type. Note that you can use <code>sizeof()</code> with types, variables, pointers, classes, and objects.</p>
<p>Write a small C++ function that demonstrates the use of <code>sizeof()</code> with the following types: <code>int</code>, <code>unsigned int</code>, <code>float</code>, <code>double</code>, <code>char</code>, <code>bool</code>, <code>int*</code>, <code>char*</code>, and <code>double*</code>. Your function should print out all the types and their respective sizes. You will use the values outputted by your program to fill in the table in the in-lab section. The function should be called <code>sizeOfTest()</code> (note the capitalization!), so as not to confuse C++ with the <code>sizeof()</code> operator. This function should not take in any parameters.</p>
<h3 id="binary-number-output">Binary number output</h3>
<p>The second coding exercise for the pre-lab is a binary output program. The function to write is called <code>outputBinary()</code>, and it will take in one parameter, an <code>unsigned int</code>. It must be unsigned, or else your code may not work! You should then print out the 32-bit binary representation (this includes the leading 0s!) of the passed parameters in <strong>big Endian</strong> format. For example:</p>
<pre><code>outputBinary(1) //=> 0000 0000 0000 0000 0000 0000 0000 0001
outputBinary(5) //=> 0000 0000 0000 0000 0000 0000 0000 0101
outputBinary(1000000) //=> 0000 0000 0000 1111 0100 0010 0100 0000</code></pre>
<ul>
<li>If you do use for loops to do this, become familiar with the left shift operator (<<) and what it does to (unsigned) ints, and the binary and/or operators (& and |).</li>
<li>You can also use a calculator program (including one in Windows) to convert numbers to binary (select View->Scientific), as that is not the coding part of this lab.</li>
</ul>
<p>You can <strong><em>NOT</em></strong> use the <code>bitset</code> class for this, or any other class that does the work for you. You have to program this yourself.</p>
<h3 id="the-limits-of-representation">The Limits of Representation</h3>
<p>What do you think will happen when you add 1 to a variable containing the maximum value of a type? Write a function called <code>overflow()</code> to answer the following questions:</p>
<ul>
<li>What happens when you add 1 to an <code>unsigned int</code> variable containing the maximum value of an <code>unsigned int</code>?</li>
<li>Does the program halt?</li>
<li>What answer do you get?</li>
<li>Why does this happen?</li>
</ul>
<p>Your function should create an <code>unsigned int</code>, give it the max value, and add 1 to that. By printing out the result, you will effectively answer the first 3 of the 4 questions. Answer the last question in a <code>cout</code> statement (NOT as a comment!). The function takes in no parameters.</p>
<h3 id="prelab4.cpp">prelab4.cpp</h3>
<p>Your three functions, <code>sizeOfTest()</code>, <code>outputBinary()</code>, and <code>overflow()</code> should be combined into a prelab4.cpp file. This is the one C++ source code file that you will submit for the pre-lab. The input requirements for this program are fairly strict, so as to allow automated execution of your programs.</p>
<p><strong>Your program should ask for a single integer value for input</strong>, which we will call <em>x</em>. The program will call the three functions in order: <code>sizeOfTest()</code>, <code>outputBinary()</code>, and then <code>overflow()</code>. Note that only <code>outputBinary()</code> takes in <em>x</em> as the parameter. The program should take in no further input.</p>
<h3 id="floating-point-conversion">Floating point conversion</h3>
<p>For the last part of the pre-lab, you will need to convert a floating point number to binary representation, and another number from binary representation to a floating point number. You should do this by hand (i.e. not in a computer program), and have the worked-out solution (similar to the lecture notes) be in a floatingpoint.pdf file -- you can use any editor you would like to generate the file, as long as what you submit is a PDF file.</p>
<p>First, you will need to determine what your floating point numbers are going to be -- these numbers will be different for each student. To do so, visit <a href="http://libra.cs.virginia.edu/getfloat.php">http://libra.cs.virginia.edu/getfloat.php</a> and enter your UVa userid. Each floating point number is unique to the userid entered. Note that the hexadecimal number printed is in <strong>little Endian</strong> format.</p>
<p>The first number must be converted into (little-Endian) format -- you should leave your answer in hexadecimal, as that will be an easier way (versus binary) to represent the number. The second number (the one in hexadecimal) needs to be converted to a base 10 real number as per the algorithm for converting IEEE 754 single-precision (i.e. 32-bit) floating point numbers.</p>
<p>Note that a '0x' before a number means that the number is in hexadecimal. Thus, 0x11 has decimal value 17, as that is what 11 is in hexadecimal. The '0x' part is not part of the value.</p>
<p>For example, if you entered 'asb2t', you would get:</p>
<pre><code>Your magic (32 bit) floating point number is -35.125
This is the number that needs to be converted to (little endian) binary, and expressed in hexadecimal.
Your other magic floating point number is, in hex, 0x00401f41
This is the number that needs to be converted to a (32 bit) floating point number.
Note that the hexadecimal printed above is in little-endian format!</code></pre>
<p>In this example, you would convert -35.125 to 0x800cc2 (or 0x00800cc2 -- same thing with the leading zeros added), and 0x401f41 (or 0x00401f41 -- again, the same thing) to 9.95312.</p>
<p>Note: during the conversion, the numbers provided do not have any 1 bits in the mantissa after the 10th bit. It could be that your floating point number needs the first 10 bits of the mantissa, or it could need less. But all bits after the first 10 are supposed to be zero. So if your conversion has any bits beyond that set to 1, then you are doing something wrong. You will be expected to be able to do this on a test -- although in an exam situation, because no calculators are allowed, the math involved with determining the mantissa won't be very hard.</p>
<p>Your conversion should be in a PDF file called floatingpoint.pdf, which will be submitted with the pre-lab. The idea is to show the math behind the conversion (similar to how was done in class), not to write a program to do it.</p>
<p>You must actually type up your work in a word editor (Word is fine, as is Mac Pages; <a href="http://www.libreoffice.org/">LibreOffice</a> is a free alternative). You can <strong><em>NOT</em></strong> do the assignment by hand, then scan it in (or take a photo of it). It must actually be typeset in your favorite editor of choice. Note that the Unix honor pledge only applies to development, so you are free to use anything to type up the file.</p>
<p><strong>File size:</strong> the submission server will only accept files of up to a certain size, and your file must be less than this size. If you properly type set your document, then this will not be a problem. But if you write out your assignment by hand, then scan (or take a picture of) it, then it will be too big to submit. And note that you have to actually type set it, as mentioned above, so you should be doing this anyway.</p>
<hr />
<h2 id="in-lab-1">In-Lab</h2>
<p>The purpose of this in-lab is to complete the <a href="inlab4.doc">inlab4.doc</a> worksheet, and turn that in (in PDF format - see <a href="../../docs/convert_to_pdf.html">How To Convert A File To PDF</a> for details). The in-lab description, below, discusses filling in this worksheet. As part of this in-lab, you will have to write a few small programs, which you will combine into an inlab4.cpp file - the output of this program will give you some of the values to fill into the inlab4.doc worksheet. The sections below named <a href="#memory">Representation in memory</a> and <a href="#arrays">Primitive Arrays in C++</a> describe what should be in this file. It should not take in any input, and should just print out the necessary values.</p>
<h3 id="bit-versus-64-bit">32-bit versus 64-bit</h3>
<p>We realize that most of you will have 64 bit computers. However, the VirtualBox image is a 32-bit image. The information you provide for this in-lab can be either for a 32-bit computer (the VirtualBox image), or a 64-bit computer (what you probably have, as well as what is in the lab), as long as you are consistent - meaning you have to have <strong>all</strong> your answers be one or other; you can't mix-and-match.</p>
<p>If you are on a 64-bit machine, you can also compile your code with the <code>-m32</code> option, which will force the resulting program (a.out) to be a 32-bit program, regardless of whether it is running on a 64 bit machine or not.</p>
<p>When using GDB, you can use the 'x' (for 'eXamine') command to print out the pointee of an address. Consider the C++ program that has two variables defined, <code>int i</code> and <code>int *p</code>. To print out the int variable <code>i</code>, you would enter <code>x &i</code> (as you have to enter the address of the data). If <code>p</code> is a pointer to a value, you would enter <code>x p</code> to print out the <em>pointee</em>. This may print it using many more hexadecimal digits than you wanted, so you can add a parameter to the 'x' command to have it print only a certain amount:</p>
<ul>
<li><code>x/xb p</code>: this will print the one byte at the address that is pointed to by p</li>
<li><code>x/xh &i</code>: this will print the two bytes (short) of int variable i</li>
<li><code>x/xw p</code>: this will print the four bytes at the address that is pointed to by p</li>
<li><code>x/xg &i</code>: this will print the eight bytes of int variable i</li>
</ul>
<p>Note that this is only really useful when printing out a <strong>smaller</strong> size than really exists. If your variable is 4 bytes, and you print out 8 bytes, then the other 4 bytes will be whatever arbitrary values are adjacent in memory.</p>
<h3 id="size-of-c-data-types">Size of C++ data types</h3>
<p>The size of C++ data types is dependent on the underlying hardware on which you are running. A programmer may determine the size of various data types by using the <code>sizeof()</code> operator, discussed in the pre-lab. Note that, unlike a function, you can supply a type to the <code>sizeof()</code> operator (i.e., <code>sizeof(int)</code>) -- you can't do this with a function.</p>
<p>Download the document called <a href="inlab4.doc">inlab4.doc</a> -- this contains (among other things) the table below, in which you will enter in your answers. Remember to include the standard identifying header at the top of the file (name, date, etc.). This file will be submitted electronically in PDF format.</p>
<p>Note: <code>char</code>, <code>short</code>, <code>int</code>, and <code>long</code> are all integral (i.e. integer) types. Integral types may be either signed or unsigned. Signed types have a different range of values than their unsigned counterpart. A 32-bit <code>int</code> can have 2<sup>32</sup> = 4,294,967,296 values. An <code>unsigned int</code> can range from 0 to 4,294,967,295, and a regular (i.e. signed) <code>int</code> can range from -2,147,483,648 to 2,147,483,647. Unless specified as <code>unsigned</code> (as in: <code>unsigned long</code>), then all integral types in C++ are signed.</p>
<p>To fill in this table, you can use the <code>outputBinary()</code> function that you write for the pre-lab for some of the types (specifically those of size 4). To do this, create a union (as per the lecture notes on number representation) -- have it contain an <code>unsigned int</code> and a <code>float</code> (or whatever 4-byte type you are dealing with). Copy in a float, copy out an unsigned int, and print that to the screen using your <code>outputBinary()</code> method.</p>
<p>For the other types, you will need to use the debugger. Write a simple C++ program that creates the variables, and stores the appropriate value (zero or one) into them. Compile (remember the <code>-g</code> flag!), load the debugger, set a breakpoint, and start the program execution. To find out how the value is stored in hex, first find out the address of where the variable is in memory (<code>print &i</code>, for example). Then, using that address, you can use the examine command: <code>x/x 0xbf8cd9ac</code>. The first 'x' is telling gdb to eXamine a location in memory. The second 'x', after the forward slash, is telling GDB to print out that result in hex, and the address is the output from the previous print command. This will print the 4 bytes (32 bits) of memory at that location, in hex. If you want to print 8 bytes (64 bits), use <code>x/xg</code> (the 'g' tells GDB to print 8 bytes instead of 4). You can also combine these commands by entering <code>x/x &i</code>. The <code>x/x</code> part works as above; the <code>&i</code> tells it to print the value in memory at the address ('&') of the <code>i</code> variable.</p>
<p>For various optimization reasons, when you declare a variable in C++, it does not immediately initialize it. In fact, it will initialize it just before it is first used. Thus, if you set a breakpoint after you declare and initialize a variable, but before it is used, the variable will have a random value in it. You can solve this by printing out the variable via <code>cout</code> -- this causes C++ to initialize the variable for the output statement. You can then set your breakpoint after this <code>cout</code> statement.</p>
<p>The 'max value' column can be determined by understanding how the type stores the values (see the lecture notes for details). Note that you can assign hex constants directly to integer variables in your C++ program: rather than saying <code>x=17</code>, you can say <code>x=0x11</code>. You can also assign them directly in the debugger. In C++, a number beginning with 0 is in octal, e.g. 011 is 9 decimal. For chars, we are looking for the maximum integer value that can be stored therein (a char is just a byte-sized int); bools have only two possible values (<code>true</code> and <code>false</code>), so pick your max and min from those. For pointers, it's the highest memory address that can be addressed.</p>
<p>The following tables do not render very in this document, but the tables in <a href="inlab4.doc">inlab4.doc</a> render much better. Note that 'zero' and 'one' should be interpreted appropriately for the given data type. So 'zero' would be 0 for an <code>int</code>, 0.0 for a <code>float</code>, <code>false</code> for a <code>bool</code>, etc. Similarly for 'one'.</p>
<p>As was mentioned in lecture, the #define'd value <code>UINT_MAX</code> contains the maximum integer value, and you must <code>#include <climits></code> in order to access it.</p>
<table>
<thead>
<tr class="header">
<th align="left">C++ Type</th>
<th align="left">Size in bytes?</th>
<th align="left">Max value? (base 10)</th>
<th align="left">Zero is stored as (in hex)?</th>
<th align="left">One is stored as (in hex)?</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">int</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">unsigned int</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">float</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">double</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">char</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">bool</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
</tbody>
</table>
<p>And, for pointers:</p>
<table>
<thead>
<tr class="header">
<th align="left">C++ Type</th>
<th align="left">Size in bytes?</th>
<th align="left">Max value? (base 10)</th>
<th align="left">NULL is stored as (in hex)?</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">int*</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">char*</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">double*</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
</tbody>
</table>
<p>The results in the hex columns should be in <strong>big endian</strong> format, which is the same format that GDB displays, as well as the format that your <code>outputBinary()</code> program should display. To convert binary into hex, see the <a href="../../slides/03-numbers.html">03-numbers slide set</a>.</p>
<h3 id="representation-in-memory"><a name="memory">Representation in memory</a></h3>
<p>This exercise will show you how to read the contents of a particular memory address. This will be useful for debugging code and for understanding the underlying data representation of abstract data types.</p>
<p>Recall that all Intel 80x86 machines (i.e. all Pentium-class machines) are little-Endian. Thus, 0xd97c34a2 is stored as: a2 34 7c d9, with the least significant byte listed first. However, when you examine the value in gdb (using the <code>x/x</code> command), it will display it in big-endian format, as that is how humans typically think of numbers.</p>
<p>Write a C++ program, called inlab4.cpp, where you consecutively declare variables of these types: <code>bool</code>, <code>char</code>, <code>int</code>, <code>double</code>, <code>int*</code>, and assign a value to each of them. The last line(s) of the program should print out the values. Put a breakpoint near the end of the program, but before the last print statement(s). Once the breakpoint is hit, type expressions to examine the addresses of all of these variables (e.g. <code>&i</code>). Then for each of these variables, view the contents of their addresses (via the <code>x/x</code> command from above).</p>
<p>Find one of your <code>int</code> variables in memory. Change its value via the <code>set variable <var> = <value></code> command. Examine the new variable's contents in memory. Is it what you expected? Continue the program execution -- did it properly print the changed value?</p>
<p><strong>This program should take in no input.</strong></p>
<p>After completing this section of the lab, you will be expected to understand how to use the debugger to:</p>
<ul>
<li>View variables in memory.</li>
<li>Change an int value from positive to negative.</li>
<li>Observe interesting/different features in memory (e.g. skipped memory) and be able to explain it.</li>
</ul>
<h3 id="primitive-arrays-in-c"><a name="arrays">Primitive Arrays in C++</a></h3>
<p>For the pre-lab, you should have read the <a href="../../docs/readings.html">Readings</a> on arrays, if you feel you need a bit more background. Note how two (or higher) dimensional arrays are stored in row-major order (as described in the <a href="../../slides/04-arrays-bigoh.html">04-arrays-bigoh slide set</a>) in C++, as opposed to being stored as arrays of arrays in Java.</p>
<p>For this part, you will need to add a bit of code to your inlab4.cpp file. You program should show a clear separator where the previous section's part of inlab4.cpp ends and where this section's part of inlab4.cpp begins. The additional code should declare a one dimensional array of chars and a one dimensional array of ints:</p>
<pre><code>int IntArray[10];
char CharArray[10];</code></pre>
<p>In your program you should also declare a two dimensional array of chars and a two dimensional array of ints:</p>
<pre><code>int IntArray2D[6][5];
char CharArray2D[6][5];</code></pre>
<p>Assign different values into each element of all four arrays. As above, put a breakpoint in your program after the four arrays have been assigned values. Find the address of the first element of each array, and type that address into gdb (via the 'p' command).</p>
<p><strong>This program should take in no input.</strong></p>
<p>Examine where the elements of the four arrays are in memory. You will be expected to understand and be able to explain this representation for the exams.</p>
<p>Assuming that memory is byte-addressable, write an expression that will tell you the address of the (i,j)th element of <code>IntArray2D</code> as declared above. The base address of the array is the identifier (IntArray2D), and the expression should be in terms of that, as well as <code>i</code>, <code>j</code>, and the size of the <code>int</code> type. You can assume that: (0 ≤ <em>i</em> < 6), and (0 ≤ <em>j</em> < 5 ). Note: '&' here means "the address of", you may use '&' in your answer. This answer should also go in your inlab4.doc file.</p>
<pre><code>&(IntArray2D[i][j]) = _______________________________________________</code></pre>
<p>Lastly, convert your inlab4.doc file to PDF format prior to submission.</p>
<hr />
<h2 id="post-lab-1">Post-Lab</h2>
<h3 id="binary-bit-counter">Binary bit counter</h3>
<p>Write a <strong><em>recursive</em></strong> function that returns the number of 1's in the binary representation of <em>n</em>. Use the following fact: if <em>n</em> is even, the number of bits in the representation of <em>n</em> is the same as that in <em>n/2</em>; if <em>n</em> is odd, the number of bits is the same as that in <em>floor(n/2)</em> plus 1.</p>
<p>You may assume that <em>n</em> is a non-negative integer that will be stored in two's complement. However, <em>n</em> will be passed in the standard decimal (i.e. base-10) format. This should be a rather simple function that uses what you've learned about integer representation. If you find you need things like global variables or the <code>pow()</code> function to implement this then you are going too far.</p>
<p>This program, called bitCounter.cpp should take in a value as a command-line parameter (no input!). See below for how to handle command-line parameters. Note that if the program is run without any command-line parameters, your program should gracefully exit with an appropriate error message. Your program need not handle an invalid number for the command-line parameter. And any additional command-line parameters beyond the first can be (and should be) ignored.</p>
<p><strong>This program should take in no input, only one command-line parameter.</strong> Command line parameters are discussed in more detail in the <a href="../../slides/04-arrays-bigoh.html">04-arrays-bigoh slide set</a>, along with a source code example showing how to use them.</p>
<p>So far, our <code>main()</code> method has had the following prototype:</p>
<pre><code>int main()</code></pre>
<p>We will now change that prototype to the following:</p>
<pre><code>int main (int argc, char **argv)</code></pre>
<p>These two parameters are providing you with the command-line parameters. The first parameter, <code>argc</code>, is the number of parameters plus one. The second parameter, <code>argv</code>, is an array of C-style strings (some people list the type as <code>char *argv[]</code> to make this more clear -- either way is fine).</p>
<p>Thus, if you supply the program with 3 command-line parameters, then argc would be set to 4, <code>argv[0]</code> would be the C-string that contains the program name ('a.out', for example'), and <code>argv[1]</code>, <code>argv[2]</code>, and <code>argv[3]</code> are the 3 supplied command line parameters.</p>
<p>Your task is to implement the binary bit counter function that takes in a single command-line value (which is a standard base-10 integer) and prints out the number of bits contained therein.</p>
<h3 id="converting-between-number-systems">Converting between number systems</h3>
<p>Complete and submit the <a href="radixWorksheet.doc">radixWorksheet.doc</a> file as a PDF file called radixWorksheet.pdf. It must be in PDF format! See <a href="../../docs/convert_to_pdf.html">How To Convert A File To PDF</a> for details.</p>
</body>
</html>