supervision_1.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Programming in C and C++ -- 2020/21 -- Supervision 1</title>
  5. </head>
  6. <style>
  7. code, pre {
  8. font-size: 1.2em;
  9. color: darkslateblue;
  10. }
  11. </style>
  12. <body>
  13. <h1>Programming in C and C++ (2020/21)<br/>Supervision 1</h1>
  14. <h2>1</h2>
  15. <p>What is the difference between <code>'a'</code> and <code>"a"</code>?</p>
  16. <hr />
  17. <h2>2</h2>
  18. <p>Does the following code terminate? And if so, under which conditions?</p>
  19. <pre>
  20. char i, j;
  21. for (i = 0; i &lt; 10, j &lt; 5; i++, j++);
  22. </pre>
  23. <hr />
  24. <h2>3</h2>
  25. <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>
  26. <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>
  27. <hr />
  28. <h2>4</h2>
  29. <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>
  30. <hr />
  31. <h2>5</h2>
  32. <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>
  33. <p>What other invocations could cause a simple implementation to fail and how can we mitigate them?</p>
  34. <hr />
  35. <h2>6</h2>
  36. <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>
  37. <hr />
  38. <h2>7</h2>
  39. <p>Given a pointer <code>p</code>, what does the expression <code>p[-2]</code> mean, and when is it legal?</p>
  40. <hr />
  41. <h2>8</h2>
  42. <p>Write a string search function with a declaration of</p>
  43. <pre>
  44. char *strfind(const char *s, const char *f);</pre>
  45. <p>which returns a pointer to the first occurrence of <code>s</code> in <code>f</code> (and <code>NULL</code> otherwise)</p>
  46. <hr />
  47. <h2>9</h2>
  48. <p>Write a program <code>calc</code> which evaluates an expression in Reverse Polish Notation given on the command line; for example</p>
  49. <pre>
  50. $ calc 2 3 4 + \*</pre>
  51. <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>
  52. <hr />
  53. <h2>10</h2>
  54. <p>What is the value of <code>i</code> after executing each of the following:</p>
  55. <ol>
  56. <li><code>i = sizeof(char);</code></li>
  57. <li><code>i = sizeof(int);</code></li>
  58. <li><code>int a; i = sizeof a;</code></li>
  59. <li><code>char b[5]; i = sizeof(b);</code></li>
  60. <li><code>char *c=b; i = sizeof(c);</code></li>
  61. <li><code>struct { int d; char e;} s; i = sizeof s;</code></li>
  62. <li><code>void f(int j[5]) { i = sizeof j;}</code></li>
  63. <li><code>void f(int j[][10]) { i = sizeof j; }</code></li>
  64. </ol>
  65. <hr />
  66. <h2>11</h2>
  67. <p>A spacecraft arrives on Mars, but its memory has been corrupted by radiation en
  68. route. Luckily, it can receive updates one bit at a time using a predefined C
  69. function <code>short receive_bit(void)</code>, that when called will return either 1 or 0.
  70. The stream of bits for a value is transmitted in unsigned big-endian byte order:
  71. for example, a 16-bit value of 125 would be 0000000001111101. Assume the <code>int</code>
  72. type is 32 bits.</p>
  73. <ol>
  74. <li><p>Explain the meaning of the <code>inline</code> keyword on C function declarations, and a
  75. potential drawback of using it.</p></li>
  76. <li><p>Using <code>receive_bit()</code>, define a function <code>receive_int()</code> that decodes and
  77. returns a 32-bit value from the sequence of received bits.</p></li>
  78. <li><p>(<em>optional</em>) Build a more general decoding function receive using a C++
  79. template with two parameters that specify the number of bits to decode and a
  80. datatype for the decoded value. Use this to write two template instantiations
  81. that decode an 8-bit value into a short and a 32-bit value into an unsigned
  82. long.</p></li>
  83. <li><p>Find and explain <em>four</em> instances of undefined behaviour that could result
  84. from compiling and running the C code below with different command-line
  85. arguments.
  86. The <code>strcpy(dst,src)</code> function copies a zero-terminated C string from the
  87. <code>src</code> buffer to the <code>dst</code> buffer. The <code>putchar(c)</code> function outputs a
  88. character c to the console. You can assume that the standard C header
  89. 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>
  90. <pre>
  91. char *show_instruction(int msg) {
  92. char buf[6];
  93. int fuel;
  94. if (msg == 1 &amp;&amp; fuel--) {
  95. strcpy(buf, "THRUST");
  96. return buf;
  97. } else if (msg == 2) {
  98. char *msg = (char *)malloc(100);
  99. strcpy(msg, "DEPLOY_PARACHUTE");
  100. return msg;
  101. }
  102. }
  103. int main(int argc, char **argv) {
  104. char *msg;
  105. msg = show_instruction(argc);
  106. putchar(msg[0]);
  107. return 0;
  108. }
  109. </pre></li>
  110. </ol>
  111. <p>&nbsp;</p>
  112. </body>
  113. </html>