123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <!DOCTYPE html>
- <html>
- <head>
- <title>Programming in C and C++ -- 2020/21 -- Supervision 1</title>
- </head>
- <style>
- code, pre {
- font-size: 1.2em;
- color: darkslateblue;
- }
- </style>
- <body>
- <h1>Programming in C and C++ (2020/21)<br/>Supervision 1</h1>
- <h2>1</h2>
- <p>What is the difference between <code>'a'</code> and <code>"a"</code>?</p>
- <hr />
- <h2>2</h2>
- <p>Does the following code terminate? And if so, under which conditions?</p>
- <pre>
- char i, j;
- for (i = 0; i < 10, j < 5; i++, j++);
- </pre>
- <hr />
- <h2>3</h2>
- <p>Write an implementation of <strong>bubble sort</strong> for a fixed array of integers. An array of integers can be defined as <code>int i[] = {1, 2, 3, 4};</code></p>
- <p>Use function recursion to write an implementation of merge sort for a fixed array of integers. How much memory does your program use for a list of length <em>n</em>?</p>
- <hr />
- <h2>4</h2>
- <p>Implement a function <code> int cntlower(char str[]) </code> that, given a string, returns the number of lower-case letters it contains.</p>
- <hr />
- <h2>5</h2>
- <p>Define a macro <code>SWAP(t, x, y)</code> that swaps two arguments <code>x</code> and <code>y</code> of type <code>t</code>. Does your macro work as expected when called as <code>SWAP(int, v[i++], w[f(x)])</code>?</p>
- <p>What other invocations could cause a simple implementation to fail and how can we mitigate them?</p>
- <hr />
- <h2>6</h2>
- <p>Define a macro <code>SWAP(x, y)</code> that swaps <code>x</code> and <code>y</code> of the same type without using any extra memory.</p>
- <hr />
- <h2>7</h2>
- <p>Given a pointer <code>p</code>, what does the expression <code>p[-2]</code> mean, and when is it legal?</p>
- <hr />
- <h2>8</h2>
- <p>Write a string search function with a declaration of</p>
- <pre>
- char *strfind(const char *s, const char *f);</pre>
- <p>which returns a pointer to the first occurrence of <code>s</code> in <code>f</code> (and <code>NULL</code> otherwise)</p>
- <hr />
- <h2>9</h2>
- <p>Write a program <code>calc</code> which evaluates an expression in Reverse Polish Notation given on the command line; for example</p>
- <pre>
- $ calc 2 3 4 + \*</pre>
- <p>should print 14. The character <code>*</code> needs to be escaped with a backslash in some shells or otherwise it gets expanded to all the files in your current directory.</p>
- <hr />
- <h2>10</h2>
- <p>What is the value of <code>i</code> after executing each of the following:</p>
- <ol>
- <li><code>i = sizeof(char);</code></li>
- <li><code>i = sizeof(int);</code></li>
- <li><code>int a; i = sizeof a;</code></li>
- <li><code>char b[5]; i = sizeof(b);</code></li>
- <li><code>char *c=b; i = sizeof(c);</code></li>
- <li><code>struct { int d; char e;} s; i = sizeof s;</code></li>
- <li><code>void f(int j[5]) { i = sizeof j;}</code></li>
- <li><code>void f(int j[][10]) { i = sizeof j; }</code></li>
- </ol>
- <hr />
- <h2>11</h2>
- <p>A spacecraft arrives on Mars, but its memory has been corrupted by radiation en
- route. Luckily, it can receive updates one bit at a time using a predefined C
- function <code>short receive_bit(void)</code>, that when called will return either 1 or 0.
- The stream of bits for a value is transmitted in unsigned big-endian byte order:
- for example, a 16-bit value of 125 would be 0000000001111101. Assume the <code>int</code>
- type is 32 bits.</p>
- <ol>
- <li><p>Explain the meaning of the <code>inline</code> keyword on C function declarations, and a
- potential drawback of using it.</p></li>
- <li><p>Using <code>receive_bit()</code>, define a function <code>receive_int()</code> that decodes and
- returns a 32-bit value from the sequence of received bits.</p></li>
- <li><p>(<em>optional</em>) Build a more general decoding function receive using a C++
- template with two parameters that specify the number of bits to decode and a
- datatype for the decoded value. Use this to write two template instantiations
- that decode an 8-bit value into a short and a 32-bit value into an unsigned
- long.</p></li>
- <li><p>Find and explain <em>four</em> instances of undefined behaviour that could result
- from compiling and running the C code below with different command-line
- arguments.
- The <code>strcpy(dst,src)</code> function copies a zero-terminated C string from the
- <code>src</code> buffer to the <code>dst</code> buffer. The <code>putchar(c)</code> function outputs a
- character c to the console. You can assume that the standard C header
- prototypes have been included for <code><stdio.h></code>, <code><stdlib.h></code> and <code><string.h></code>.</p>
- <pre>
- char *show_instruction(int msg) {
- char buf[6];
- int fuel;
- if (msg == 1 && fuel--) {
- strcpy(buf, "THRUST");
- return buf;
- } else if (msg == 2) {
- char *msg = (char *)malloc(100);
- strcpy(msg, "DEPLOY_PARACHUTE");
- return msg;
- }
- }
- int main(int argc, char **argv) {
- char *msg;
- msg = show_instruction(argc);
- putchar(msg[0]);
- return 0;
- }
- </pre></li>
- </ol>
- <p> </p>
- </body>
- </html>
|