|
@@ -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> 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>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->i
|
|
|
- p++->i
|
|
|
- *p->i
|
|
|
- *p->i++
|
|
|
- (*p->i)++
|
|
|
- *p++->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><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;
|
|
|
+ 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>
|