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

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 05 main.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#include "input.h"
/* Whether the grade is valid.
After: returns 1 if grade is from 1 to 10, otherwise 0. */
int is_valid(int grade) {
return grade >= 1 && grade <= 10;
}
/* Asks for a grade until a valid one is entered.
After: the returned grade is from 1 to 10. */
int read_grade(void) {
int grade = read_int("Grade: ");
while (!is_valid(grade)) {
printf("Grade must be between 1 and 10.\n");
grade = read_int("Grade: ");
}
return grade;
}
/* Prints the grade in words.
Before: grade is from 1 to 10. */
void print_in_words(int grade) {
assert(is_valid(grade));
switch (grade) {
case 10:
printf("Excellent\n");
break;
case 9:
printf("Very good\n");
break;
case 8:
printf("Good\n");
break;
case 7:
printf("Fair\n");
break;
case 6:
printf("Satisfactory\n");
break;
case 5:
printf("Weak\n");
break;
default:
printf("Unsatisfactory\n");
break;
}
}
/* Rounds to a whole number.
Before: x >= 0.
After: the nearest whole number; x.5 is rounded up. */
int round_to_int(double x) {
assert(x >= 0);
return (int)(x + 0.5);
}
void run_tests(void) {
assert(is_valid(1));
assert(is_valid(10));
assert(!is_valid(0));
assert(!is_valid(11));
assert(round_to_int(0) == 0);
assert(round_to_int(7.49) == 7);
assert(round_to_int(7.5) == 8);
}
int main(void) {
run_tests();
int count = read_int("How many grades? ");
while (count < 1) {
printf("At least one grade is needed.\n");
count = read_int("How many grades? ");
}
int sum = 0;
for (int i = 1; i <= count; i++) {
printf("%d. ", i);
int grade = read_grade();
print_in_words(grade);
sum += grade;
}
double average = (double)sum / count;
printf("Average: %.2f\n", average);
printf("Rounded: %d\n", round_to_int(average));
return 0;
}