Jelajahi Sumber

:memo: Add ProgC Sup 1

Andrea Franceschini 6 tahun lalu
induk
melakukan
206f0a96ec
1 mengubah file dengan 189 tambahan dan 0 penghapusan
  1. 189 0
      files/ProgC/supervision_1.html

+ 189 - 0
files/ProgC/supervision_1.html

@@ -0,0 +1,189 @@
+<!DOCTYPE html>
+
+<html>
+
+<head>
+    <title>Programming in C and C++ -- 2018/19 -- Supervision 1</title>
+</head>
+
+<body>
+    <h1>Programming in C and C++ (2018/19)<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; language-char i, j;
+    for (i = 0; i &lt; 10, j &lt; 5; i++, j++);
+    </pre>
+
+    <hr />
+
+    <h2>3</h2>
+    <p>Write an implementation of bubble sort for a fixed array of integers.</p>
+
+    <p>An array of integers can be defined as <code>int i[] = {1, 2, 3, 4};</code></p>
+
+    <p>Modify youur answer to sort characters into lexicographical order.</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 for</p>
+
+    <pre>SWAP(int, v[i++], w[f(x)])</pre>
+
+    <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>If <code>p</code> is a pointer to a structure, write some C code which uses all the following indirections: </p>
+
+    <pre>
+    ++p-&gt;i
+    p++-&gt;i
+    *p-&gt;i
+    *p-&gt;i++
+    (*p-&gt;i)++
+    *p++-&gt;i
+    </pre>
+
+    <p>and describe the action of each code snippet.</p>
+
+    <hr />
+
+    <h2>9</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>10</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.</p>
+
+    <hr />
+
+    <h2>11</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>12</h2>
+    <p>According to the rules of pre and post increment what would you expect these expressions to do:</p>
+
+    <pre>
+    i++ + ++i
+    ++i + ++i
+    </pre>
+
+    <p>What actually happens if you try it?</p>
+
+    <hr />
+
+    <h2>13</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 int
+    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 receive <code>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>&lt;stdio.h&gt;</code>, <code>&lt;stdlib.h&gt;</code> and <code>&lt;string.h&gt;</code>.</p>
+
+        <pre>
+        char *show_instruction(int msg) {
+            char buf[6];
+            int fuel;
+            if (msg == 1 &amp;&amp; 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>
+
+</body>
+
+</html>