Simvik

Fix: the heater never turns on

A comparison that is really an assignment. An easy embedded C problem (Decisions and loops), run and graded in the browser.

easy · Decisions and loops · find-the-bug · if · operators

Bug report. The heater never turns on. Even in normal mode on a cold morning, the controller reports off.

heater_action returns "off" in safe mode. In normal mode it returns "heat" below 20 °C and "idle" otherwise. Find the defect and fix it by changing one line.

Starter code

const char *heater_action(int mode, int temp_c) {
  if (mode = MODE_SAFE) return "off";
  if (temp_c < 20) return "heat";
  return "idle";
}

Example tests

A cold morning in each mode

printf("%s\n", heater_action(MODE_NORMAL, 15));
printf("%s\n", heater_action(MODE_SAFE, 15));

Expected output:

heat
off

Learn it first

  • Decisions and loops · Choosing with if and switch, repeating with while and for, and the loop that never ends.

More Decisions and loops problems