Coding Challenge 1 (C)

Find the errors in this code
float readTemperature() {
return 25.0;
}

void controlHeater(float currentTemperature, float setpoint, bool *heaterStatus) {

float tolerance = 0.5;

float temperatureDifference = currentTemperature - setpoint;

if (temperatureDifference > tolerance) {
*heaterStatus = false;
} else if (temperatureDifference < -tolerance) {
*heaterStatus = true;
}

printf("Current Temperature: %.2f°C, Heater Status: %s\n", currentTemperature,
*heaterStatus ? "ON" : "OFF");
}

int main() {
float setpoint = 22.0;
bool heaterStatus = false;

for ( i = 0; i < 10; ++i) {
float currentTemperature = readTemperature();
controlHeater(currentTemperature, setpoint, &heaterStatus);
for ( delayCount = 0; delayCount < 1000000; ++delayCount) {

}
}

return 0;
})
float readTemperature() {
return 25.0;
}

void controlHeater(float currentTemperature, float setpoint, bool *heaterStatus) {

float tolerance = 0.5;

float temperatureDifference = currentTemperature - setpoint;

if (temperatureDifference > tolerance) {
*heaterStatus = false;
} else if (temperatureDifference < -tolerance) {
*heaterStatus = true;
}

printf("Current Temperature: %.2f°C, Heater Status: %s\n", currentTemperature,
*heaterStatus ? "ON" : "OFF");
}

int main() {
float setpoint = 22.0;
bool heaterStatus = false;

for ( i = 0; i < 10; ++i) {
float currentTemperature = readTemperature();
controlHeater(currentTemperature, setpoint, &heaterStatus);
for ( delayCount = 0; delayCount < 1000000; ++delayCount) {

}
}

return 0;
})
3 Replies
Yash Naidu
Yash Naidu2y ago
Upon initial analysis, the errors lie here Undeclared 'i' , Undeclared 'delayCount' , Need to include stdbool.h to recognize 'bool' type, The ending bracket int the last line ')' (maybe a typo) Always prints the temp is 25.00 C and the status as OFF as the value of temperatureDiferrence would be 3 which is greater than the tolerance.
nour_oud
nour_oudOP2y ago
Nice, you got it
Saßì
Saßì2y ago
Variables 'i' and delayCount in the for loop inside the main function are used without being declared. You should declare them before using them in the loop.

Did you find this page helpful?