supervision_1.html 5.9 KB

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