Skip to the task
Layout
Back to classic view
READTask 1 of 17 · step 1: How the test worksHow the test works
How the test works in practice/06/

Two parts: questions without a compiler and two programs in the test/2 folder with autotests. They give no points.

Details & hint

The test after topic 12 checks topics 7–12: modules, pointers and bits, strings, structs, files and dynamic memory. This lesson is practice in the same shape.

Two parts

  • Questions. Short code: what it prints, why it doesn't compile, where the bug is. You answer on your own, without a compiler. There are twelve here, each with an answer and an explanation.
  • Programs. Two small tasks in the test/2/ folder: words.c and shopping.c. Autotests check them, like the homework.

How long the real test takes and what you may use will be announced by your lecturer. Practice has no time limit, but try to write both tasks within an hour and a half.

How to check the programs

The task descriptions and the exact output text are in steps 4 and 5 and in the test/2/tests/ files. Run the tests like this:

ctest --preset debug -L test2

In CLion: the All CTest configuration and ▶. Both files already have the #includes and main; while they are empty, the tests fail.

These tasks give no points: GitHub Actions builds them and runs them with ASan and UBSan, but "Points" doesn't count them. They are for checking yourself.

What is checked, same as in the homework

  • the program builds without warnings (-Wall -Wextra -Werror -pedantic -Wvla);
  • the output matches the expected text letter by letter;
  • each part is a function with a contract and an assert, and main only calls them;
  • shopping.c: realloc, free and fclose are there, fscanf isn't, and GitHub CI finds no leak;
  • no TODO is left.

If you get stuck, go back to the topic the question comes from: every answer says which topic it belongs to.

practice 06 search.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#define N 1000
/* How many elements the last search checked. For measuring only. */
int checks = 0;
/* Linear search.
Before: 0 <= n.
After: the index of x, or -1 if x is not there. */
int linear_search(const int a[], int n, int x) {
assert(n >= 0);
for (int i = 0; i < n; i++) {
checks++;
if (a[i] == x) {
return i;
}
}
return -1;
}
/* Binary search.
Before: 0 <= n, a is sorted in ascending order.
After: the index of x, or -1 if x is not there. */
int binary_search(const int a[], int n, int x) {
assert(n >= 0);
int left = 0;
int right = n - 1;
while (left <= right) {
int middle = left + (right - left) / 2;
checks++;
if (a[middle] == x) {
return middle;
}
if (a[middle] < x) {
left = middle + 1;
} else {
right = middle - 1;
}
}
return -1;
}
int main(void) {
int numbers[N];
for (int i = 0; i < N; i++) {
numbers[i] = 2 * i;
}
int targets[] = {0, 998, 1998, 7};
int count = (int)(sizeof targets / sizeof targets[0]);
printf("Target Linear Binary\n");
for (int k = 0; k < count; k++) {
checks = 0;
int pos = linear_search(numbers, N, targets[k]);
int linear_checks = checks;
checks = 0;
int pos2 = binary_search(numbers, N, targets[k]);
assert(pos2 == pos);
printf("%6d %6d %6d\n", targets[k], linear_checks, checks);
}
return 0;
}