Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: About.

HOMEWORKTask 16 of 17 · step 5: Task 2: a shopping listTask 2: a shopping list
Task 2: a shopping list in practice/06/

test/2/shopping.c: shopping.txt into a dynamic array of structs, the sums and the most expensive line; a bad line and no file – a message and an error code.

Checklist

  • Every task is one commit with a clear message.
  • Zero compiler warnings: the code builds with -Werror.
  • Once per topic you step through your code in the debugger (CLion, gdb).
  • Every function has preconditions and postconditions, and assert tests check them.
  • A multi-file program: .h files with Doxygen comments and their own CMake target.
  • Input only through the safe functions of your own input.h.
  • Every error code is checked (fopen, fclose, malloc and so on).
  • CI is clean: no AddressSanitizer or UBSan reports.
Details & hint

The file test/2/shopping.c, the program test2_shopping.

The task

The program reads the file shopping.txt (from the folder it is run in). Each line is one item: a name (one word, up to 30 bytes), a quantity (a whole number, at least 1) and a price (with a dot, not negative):

Milk 2 1.29
Bread 1 0.99
Coffee 2 4.49
Apples 6 0.35
Cheese 1 3.80
Tea 3 1.50

This test/2/shopping.txt is already there, and course_data(shopping.txt) copies it to where ▶ runs the program. Edit the original: the next Build refreshes the copy.

Print every item with its sum, the total and the most expensive line (the largest quantity × price; of several equal ones, the first):

Items: 6
Milk: 2 x 1.29 = 2.58
Bread: 1 x 0.99 = 0.99
Coffee: 2 x 4.49 = 8.98
Apples: 6 x 0.35 = 2.10
Cheese: 1 x 3.80 = 3.80
Tea: 3 x 1.50 = 4.50
Total: 22.95
Most expensive: Coffee (8.98)
  • No file – Cannot open the file shopping.txt. and return EXIT_FAILURE;.
  • A bad line (a part missing, 4,49, quantity 0, a negative price, junk at the end, an empty line) – shopping.txt, line 3: bad format. and EXIT_FAILURE. Don't print the other lines.
  • An empty file – Items: 0 and Total: 0.00, without Most expensive.

How to write it

  • typedef struct { char name[MAX_ITEM_NAME + 1]; int quantity; double price; } Item;
  • There is no limit on the number of items: they live in a dynamic array that grows via realloc and a temporary pointer, like topic 12's IntArray. There must be no MAX_ITEMS.
  • Read the lines with fgets, not fscanf. sscanf(line, "%30s %d %lf %c", ...) helps take one line apart: the line is good when it returns 3 – the fourth %c catches junk at the end.
  • Return errors as a code (an enum), not exit inside a function. On every path – fclose and free: GitHub CI with LeakSanitizer catches a leak on an error path too.
  • Every function has a contract and an assert.

Tests

Each test runs the program in its own folder: in the first shopping.txt is the example above (6 items: the capacity grows 4 → 8), in the second line 3 is Coffee 2 4,49, in the third there is no file. The second and third tests expect an error code.

When ctest --preset debug -L test2 shows 100% tests passed, 0 tests failed out of 7, commit: Solve test 2: shopping list.

practice 06 hall.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#define ROWS 4
#define SEATS 6
/* How many free seats (0) there are in a row.
Before: 0 <= row < ROWS.
After: a number from 0 to SEATS. */
int free_seats(int hall[][SEATS], int row) {
assert(row >= 0 && row < ROWS);
int count = 0;
for (int s = 0; s < SEATS; s++) {
if (hall[row][s] == 0) {
count++;
}
}
return count;
}
/* The first row that has k free seats side by side.
Before: 1 <= k <= SEATS.
After: the row's index, or -1 if there is none. */
int find_row(int hall[][SEATS], int k) {
assert(k >= 1 && k <= SEATS);
for (int r = 0; r < ROWS; r++) {
int in_a_row = 0;
for (int s = 0; s < SEATS; s++) {
if (hall[r][s] == 0) {
in_a_row++;
if (in_a_row == k) {
return r;
}
} else {
in_a_row = 0;
}
}
}
return -1;
}
int main(void) {
int hall[ROWS][SEATS] = {
{1, 1, 0, 0, 1, 1},
{0, 1, 1, 1, 1, 0},
{0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 0},
};
for (int r = 0; r < ROWS; r++) {
printf("Row %d: ", r + 1);
for (int s = 0; s < SEATS; s++) {
if (hall[r][s] == 1) {
printf("#");
} else {
printf(".");
}
}
printf(" free: %d\n", free_seats(hall, r));
}
for (int k = 2; k <= 5; k++) {
int r = find_row(hall, k);
if (r == -1) {
printf("No %d seats side by side.\n", k);
} else {
printf("%d seats side by side: row %d.\n", k, r + 1);
}
}
return 0;
}