Finding Two Interacting Implementation Errors

A device operates at 90 W, 150 W, and 120 W during three consecutive intervals. Each power value is constant throughout its complete 250 ms interval. The required energy calculation is


E=∑i=02PiΔt,E=\sum_{i=0}^{2}P_i\Delta t,


with time expressed in seconds so that energy is in joules. A programmer writes:


C
const int power_W[] = {90, 150, 120};
int dt_ms = 250;
double energy_J = 0.0;

for (int i = 0; i < 2; ++i) {
    energy_J += power_W[i] * (dt_ms / 1000);
}


Which pair of edits correctly implements the specified calculation without changing the declared variables?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Implementation of Algorithms Using C Questions: