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: the longest rising runTask 2: the longest rising run
Task 2: the longest rising run in practice/06/

test/1/rising.c: the longest stretch where each number is greater than the one before; of several the same length, the first wins.

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.
Details & hint

The file test/1/rising.c, the program test1_rising.

The task

The program asks how many numbers (1 to 30), reads them into an array and finds the longest rising run: neighbouring numbers where each is greater than the one before.

How many numbers? 8
1. Number: 5
2. Number: 1
3. Number: 3
4. Number: 4
5. Number: 2
6. Number: 6
7. Number: 7
8. Number: 8
Longest rising run: 2 6 7 8
Length: 4, starts at place 5

The runs here are 5, 1 3 4 and 2 6 7 8. The longest is the last one, and it starts at place 5 (places are numbered from 1).

  • Equal doesn't rise. 3 3 is two separate runs of one number each.
  • Several of the same length: the first one wins.
  • All falling: every number is a run of one, so the answer is the first number, length 1.
  • Ask again for an invalid count: There must be 1 to 30 numbers.

How to think about it

First write a function that says how many numbers rise from place from: it starts at 1 and grows while the next number is greater than the one before and the array hasn't ended. Then a second function goes over every start and remembers the best one: the same search as warmest in topic 6, only comparing lengths. Both with contracts and assert.

Mind the bounds: when you check a[i + 1], i + 1 must be less than n.

Tests

The first test is the example above. The second is an invalid count, 31, then 9 7 3 5 1 4: two runs of two numbers (3 5 and 1 4), and the first one wins:

Longest rising run: 3 5
Length: 2, starts at place 3

When ctest --preset debug -L test1 shows 100% tests passed, commit: Solve test 1: longest rising run.

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;
}