C Review

Review

How review works

Ideas you missed in a checkpoint come back here a day later, then again a few days after that. A couple of short problems each. Coming back to them after a gap is what makes them stick.

    Back to the course
      problem.c to run
      #include <stdio.h>
      
      int main(void) {
          int s;
          for (int i = 0; i < 3; i++) {
              scanf("%d", &s);
              if (s = 100) {
                  printf("%d: gold\n", s);
              } else {
                  printf("%d: keep going\n", s);
              }
          }
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int main(void) {
          for (int i = 1; i <= 15; i++) {
              printf("%d\n", i);
          }
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int main(void) {
          int n;
          scanf("%d", &n);
          // count down here
          return 0;
      }
      
      problem.c
      #include <stdio.h>
      
      int main(void) {
          int slices = 7;
          int people = 2;
          double share = slices / people;
          printf("%.1f\n", share);
          printf("%.1f\n", (double)slices / people);
          printf("%d\n", -slices / people);
          printf("%d\n", (int)2.99);
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int main(void) {
          printf("char: %zu\n", sizeof(char));
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int main(void) {
          // declare level, rank and score here
      
          printf("Level ?, rank ?, score ?\n");
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int main(void) {
          unsigned char fuel = 250;
          // add 10, then print
          printf("%u\n", fuel);
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int main(void) {
          printf("%d = %d\n", 10, 10);
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int count_ones(unsigned char x) {
          int count = 0;
          return count;
      }
      
      int main(void) {
          printf("0 has %d one bits\n", count_ones(0));
          printf("5 has %d one bits\n", count_ones(5));
          printf("170 has %d one bits\n", count_ones(170));
          printf("255 has %d one bits\n", count_ones(255));
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int main(void) {
          int lives = 3
          int coins = 120;
          printf("lives %d, coins %d\n", lives, coins);
          return 0;
      }
      
      problem.c to run
      int main(void) {
          int level = 4;
          printf("Level %d\n", level);
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int main(void) {
          int total = add_tax(200);
          printf("total %d\n", total);
          return 0;
      }
      
      int add_tax(int pence) {
          return pence + pence / 5;
      }
      
      problem.c
      #include <stdio.h>
      
      int level_up(int level) {
          level = level + 1;
          return level * 10;
      }
      
      int split_loot(double gold) {
          return gold / 2;
      }
      
      void join_party(int size) {
          if (size >= 4) {
              printf("party full\n");
              return;
          }
          printf("joined, party of %d\n", size + 1);
      }
      
      int main(void) {
          int level = 3;
          int reward = level_up(level);
          printf("%d %d\n", level, reward);
          printf("%d\n", split_loot(7.0));
          join_party(2);
          join_party(4);
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int add_combo_bonus(int score, int combo) {
          if (combo >= 10) {
              score = score + combo * 5;
          }
          return score;
      }
      
      int main(void) {
          int score = 200;
          add_combo_bonus(score, 12);
          printf("after a 12-hit combo: %d\n", score);
          add_combo_bonus(score, 3);
          printf("after a 3-hit combo: %d\n", score);
          return 0;
      }
      
      problem.c to run
      #include <stdio.h>
      
      int damage(int attack, int armour) {
          return 0;
      }
      
      int main(void) {
          printf("goblin hits the knight for %d\n", damage(12, 9));
          printf("dragon hits the knight for %d\n", damage(40, 9));
          printf("bat hits the knight for %d\n", damage(3, 9));
          return 0;
      }
      
      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.