Переглянути джерело

Update C/C++ supervision work

Andrea Franceschini 3 роки тому
батько
коміт
70a4e01503
2 змінених файлів з 61 додано та 90 видалено
  1. 44 79
      files/ProgC/supervision_1.html
  2. 17 11
      files/ProgC/supervision_2.html

+ 44 - 79
files/ProgC/supervision_1.html

@@ -6,6 +6,13 @@
     <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>
 
@@ -25,25 +32,19 @@
     <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>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>
+    <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>
+    <p>Implement a function <code>&nbsp;int cntlower(char str[])&nbsp;</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>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>
 
@@ -60,84 +61,47 @@
     <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 line.</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>
+    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>
+    <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>
+    $ 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>11</h2>
+    <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>
+        <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>
+    <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 int
+    for example, a 16-bit value of 125 would be 0000000001111101. Assume the <code>int</code>
     type is 32 bits.</p>
 
     <ol>
@@ -162,28 +126,29 @@
         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;
+    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>
 
+    <p>&nbsp;</p>
 </body>
 
 </html>

+ 17 - 11
files/ProgC/supervision_2.html

@@ -10,22 +10,28 @@
     <h1>Programming in C and C++ (2020/21)<br/>Supervision 2</h1>
 
     <h2>1</h2>
-    <p>If a function <code>f</code> has a static instance of a class as a local variable, when might the class constructor be called?</p>
+    <p>If a function <code>f</code> has a static instance of a class as a local variable, when is the class constructor called?</p>
+ 
+    <hr />
 
     <h2>2</h2>
     <p>Write a class <code>Matrix</code> which allows a programmer to define 2 &times; 2 matrices. Overload the common operators (e.g. <code>+</code>, <code>-</code>, <code>*</code>, and <code>/</code>). Can you easily extend your design to matrices of arbitrary size?</p>
 
+    <hr />
+
     <h2>3</h2>
     <p>Write a class <code>Vector</code> which allows a programmer to define a vector of length two. Modify your <code>Matrix</code> and <code>Vector</code> classes so that they interoperate correctly (e.g. <code>v2 = m*v1</code> should work as expected).</p>
 
+    <hr />
+
     <h2>4</h2>
     <p>Using meta programming, write a templated class <code>Prime</code>, which evaluates whether a literal integer constant (e.g. <code>7</code>) is prime or not at compile time.</p>
-
-    <h2>5</h2>
     <p>How can you be sure that your implementation of class <code>Prime</code> has been evaluated at compile time?</p>
 
-    <h2>6</h2>
-    <p>Write an implementation of a class <code>LinkList</code> which stores zero or more positive integers internally as a linked list on the heap. The class should provide appropriate constructors and destructors and a method <code>pop()</code> to remove items from the head of the list. The method <code>pop()</code> should return <code>-1</code> if there are no remaining items. Your implementation should override the copy constructor and assignment operator to copy the linked-list structure between class instances. You might like to test your implementation with the following:</p>
+    <hr />
+
+    <h2>5</h2>
+    <p>Write a class <code>LinkedList</code> which stores zero or more positive integers internally as a linked list on the heap. The class should provide appropriate constructors and destructors and a method <code>pop()</code> to remove items from the head of the list. The method <code>pop()</code> should return <code>-1</code> if there are no remaining items. Your implementation should override the copy constructor and assignment operator to copy the linked list structure between class instances. You might like to test your implementation with the following:</p>
 <pre>int main() {
     int test[] = {1,2,3,4,5};
     LinkList l1(test+1,4), l2(test,5);
@@ -37,24 +43,24 @@
 </pre>
 	<p>Hint: heap allocation and deallocation should occur exactly once!</p>
 
-    <h2>7</h2>
+    <h2>6</h2>
     <p>Why should destructors in an abstract class almost always be declared <code>virtual</code>?</p>
 
-    <h2>8</h2>
+    <h2>7</h2>
     <p>Provide an implementation for:</p>
     <pre>
     Stack(const Stack&amp; s);
     Stack&amp; operator=(const Stack&amp; s);</pre>
-    <p>as declared in the slides for lecture 7.</p>
+    <p>as declared in the slides for lecture 12.</p>
 
-    <h2>9</h2>
+    <h2>8</h2>
     <p>Provide an implementation for:</p>
     <pre>
     template&lt;class T&gt; T Stack&lt;T&gt;::pop();
     template&lt;class T&gt; Stack&lt;T&gt;::~Stack();</pre>
-    <p>as declared in the slides for lecture 7.</p>
+    <p>as declared in the slides for lecture 12.</p>
 
-    <h2>10</h2>
+    <h2>9</h2>
     <p>A hardware engineer stores a FIFO queue of bits in an <code>int</code> on a platform with<br>
         32-bit ints and 8-bit <code>char</code>s using the following C++ class:</p>
     <pre><code>class BitQueue {