Brainlag
  1. C
  2. Foundations
07/38 lessons

Checkpoint: the foundations

Checkpoint25 min4 problems

Learn

Before the memory lessons

Four problems that use everything so far: printf, scanf, types, division, if and loops, functions and bits. No starter scaffolding beyond what a real task gives you, and no hints until your first check. Next up is where values actually live in memory, so this is a good moment to find out what has stuck.

Checkpoint

Season stats from input

The input is a count n, then n scores. Print one line: how many scores, their total, the average to one decimal place and the best score. Check runs three different inputs, including one where every score is negative.

For 4 then 10 20 35 41 the line is count 4, total 106, average 26.5, best 41.

Milestones

  1. Four scores
  2. All negative
  3. Just one

On your own: hints appear after your first check, a solution after 3 checks or if you give up.

Your program reads the Input box under the editor. Check also tries 2 other inputs.

exercise_1.c to run
#include <stdio.h>

int main(void) {
    int n;
    scanf("%d", &n);

    return 0;
}

CheckpointPredict the output

Damage on an unsigned byte

A character's health is stored in an unsigned char. Type exactly what this prints.

Read the program, type exactly what it prints, then lock it in. It runs after that.

exercise_2.c
#include <stdio.h>

int main(void) {
    unsigned char hp = 3;
    hp = hp - 5;
    printf("%u\n", hp);
    printf("%x\n", hp);
    printf("%d\n", 9 / 4 * 4);
    printf("%d\n", (5 >> 1) & 1);
    return 0;
}

CheckpointFix the bug

Nine evens out of twenty

This should count the even numbers from 1 to 20 inclusive and print 10 even numbers from 1 to 20. It has two problems. Fix both.

On your own: hints appear after your first check, a solution after 3 checks or if you give up.

exercise_3.c to run
#include <stdio.h>

int main(void) {
    int count = 0;
    for (int i = 1; i < 20; i++) {
        if (i % 2 == 0) count++
    }
    printf("%d even numbers from 1 to 20\n", count);
    return 0;
}

Checkpoint

Battle pass tiers

A battle pass starts at tier 1 with 0 XP. Each tier costs 100 XP more than the one before: tier 2 needs 100 XP in total, tier 3 needs 300 (100 + 200), tier 4 needs 600 (300 + 300), and so on with no top tier. Write two functions, for any xp of 0 or more:

  • int tier_for(int xp) returns the tier reached with that much XP.
  • int xp_to_next(int xp) returns how much more XP the next tier needs.

On your own: hints appear after your first check, a solution after 3 checks or if you give up.

exercise_4.c to run
Step 1 of 5
C, 32-bit: int and pointers are 4 bytes

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.