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/10/

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 10 cities.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#include <string.h>
#define MAX_NAME 20
#define CITY_COUNT 5
/* City: three pieces of data of different types under one name. */
typedef struct {
char name[MAX_NAME];
int population;
double area; /* km2 */
} City;
/* Prints one city. Before: c != NULL. */
void print_city(const City *c) {
assert(c != NULL);
printf("%s: %d people, %.1f km2\n", c->name, c->population, c->area);
}
/* Gets a COPY of the city: the original doesn't change. */
void grow_copy(City c, int count) {
c.population += count;
}
/* Gets a pointer to the city: changes the original. Before: c != NULL. */
void grow(City *c, int count) {
assert(c != NULL);
c->population += count;
}
/* Population density, people per km2. Before: c != NULL, c->area > 0. */
double density(const City *c) {
assert(c != NULL && c->area > 0);
return c->population / c->area;
}
/* The index of the city with this name, or -1.
Before: n >= 0, t and name not NULL. */
int find_city(const City t[], int n, const char *name) {
assert(n >= 0 && t != NULL && name != NULL);
for (int i = 0; i < n; i++) {
if (strcmp(t[i].name, name) == 0) {
return i;
}
}
return -1;
}
/* Sorts by population, largest first, by selection.
Before: n >= 0, t != NULL. */
void sort_by_population(City t[], int n) {
assert(n >= 0 && t != NULL);
for (int i = 0; i < n - 1; i++) {
int max = i;
for (int j = i + 1; j < n; j++) {
if (t[j].population > t[max].population) {
max = j;
}
}
City temp = t[i];
t[i] = t[max];
t[max] = temp;
}
}
int main(void) {
City kaunas = {"Kaunas", 300000, 157.0};
City vilnius = {.name = "Vilnius", .population = 600000, .area = 401.0};
print_city(&kaunas);
print_city(&vilnius);
printf("sizeof(City): %zu\n", sizeof(City));
City copy = kaunas;
copy.population = 0;
printf("kaunas: %d, copy: %d\n", kaunas.population, copy.population);
grow_copy(kaunas, 1000);
printf("After grow_copy: %d\n", kaunas.population);
grow(&kaunas, 1000);
printf("After grow: %d\n", kaunas.population);
City cities[CITY_COUNT] = {
{"Klaipeda", 150000, 98.0}, {"Panevezys", 85000, 50.0}, {"Vilnius", 600000, 401.0},
{"Alytus", 50000, 40.0}, {"Kaunas", 300000, 157.0},
};
sort_by_population(cities, CITY_COUNT);
printf("\nBy population:\n");
for (int i = 0; i < CITY_COUNT; i++) {
printf("%d. ", i + 1);
print_city(&cities[i]);
}
int k = find_city(cities, CITY_COUNT, "Alytus");
if (k != -1) {
printf("Alytus is in place %d, density %.0f people/km2\n", k + 1, density(&cities[k]));
}
if (find_city(cities, CITY_COUNT, "Utena") == -1) {
printf("Utena is not on the list.\n");
}
return 0;
}