Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help me Code Error Correction / Comments for absent minded professor Example 7-1

ID: 2079615 • Letter: H

Question

Help me

Code Error Correction / Comments for absent minded professor

Example 7-17 on page 265 has code as follows:

void main(void)
{
unsigned int z;
TRISBbits.TRISB5 = 0; //make PORTB.5 an output
for(z=0;z<50000;z++)
{
MYBIT = 1;
MYBIT = 0;
}
while(1); //stay here forever
}

Professor Ab Sent Mind Ed tried to cover this example in the class, but he forgot to bring the book and he wrote the code as follows (note what follows the line numbers are the professor’s code)

Line 1                     int main () {

Line 2                    {

Line 3                      signed int z;

Line 4                      0 = TRISBbits.TRISD;

Line 5                      For (z = 0, z < 50000, z = z + 1)

Line 6                      {

Line 7                       mybit = 1;

Line 8                       MYBIT = 0,

Line 9                        }

Line 10                     While (1);

Line 11                      }

Student Picky raised question in professor Ed’s class and said that there are at least 5 mistakes in these 11 lines of code. (we assume the #include and #define statements are before the main function).

Try to concur with Picky on at least 5 mistakes for this piece of code. You’ll get 4 points each for each correct comment (or more than 20 if you find more than 5 mistakes).

You can also concur with professor Ed and state that there is no error or there are less than 5 mistakes in this piece of code.

Explanation / Answer

The following are the mistakes:

1.      On line 1: The parantheses are not matched. Remove the left parantheses in line 1.

2.      On line 3: There is nothing like signed int. It should be unsigned int.

3.      On line 4: In assignment, the value to be assigned should on the left.

4.      On line 4: Since the pin5 on port B has to be made output, it should be TRISbits.TRISB5 not TRISbits.TRISD

5.      On line5: the for statement syntax is for( variable = initial value; variable = final value; vaiable update). Here, instead of semi colon, coma is put

6.      On line 7 and line 8: Variable names are case sensitive in C. Hence MYBIT and mybit refer two different variables and cannot refer to the same variable.

7. On line 8, after the variable assignment, the line should be terminated by semicolon and not a coma