Coding for Fault Tolerance What is an advantage of coding using A vs B? Code A:
ID: 3725845 • Letter: C
Question
Coding for Fault Tolerance
What is an advantage of coding using A vs B?
Code A:
for (i = 0; i != 10; i++)
myFunction();
Code B:
for (i = 0; i < 10; i++)
myFunction();
What is wrong with this simplified Embedded C code?
void interruptHandler()
{
// write to a register on hardware
// to enable functionality
delayInSeconds(1); // delay for 1 second
// read value back
}
void main()
{
while (1)
{
// write to a register on hardware
// to enable functionality
delayInSeconds(1); // delay for 1 second
// read value back
// do something else
}
}
unsigned int timerTicks = 0;
void timerInterruptHander(void)
{
timerTicks++;
}
void main()
{
unsigned int initialTicks = timerTicks;
while (1)
{
if ((initialTicks + 10) < timerTicks)
flashLED(); // function defined elsewhere
}
}
Explanation / Answer
a) Advantage of coding using A vs B
Bith code has a for loop and a function.
Code A :
For(i=0; i != 10; i++)
MyFunction() ;
In this for loop i is initialized with 0. The condition is given as i! =10, which means this loop will repeatedly execute its statement whenever value of i is not equals to 10. When value of i becomes 10, this loop will terminate. That is loop will execute 10 times from i=0 to 9.
Code B :
For( i=0; i <10; i++)
MyFunction() ;
Here also i is initialized to 0. But the condition is different, i<10. Which means for loop will repeatedly execute its statement MyFunction() untill the condition remains true. That is for loop will execute from i=0 to the value of i=9. Which means loop will execute 10 times.
Both function have same working. There is no difference in working.