computer science

kritha

Here is the grading matrix where the TA will leave feedback. If you scored 100%, you will most likely not see any feedback J

 

Question

# points

Feedback

Max

Scored

1

Tracing

3

 

 

2

Testing

2

 

 

3

Refactoring

2

 

 

4

Debugging

3

 

 

Question #1 – Tracing Programs

We are going to trace the following program, i.e. simulate in your head how it would execute on a computer. To help you, a trace table is provided for you to fill.

For each “execution step” of the program, write down in the table the line being executed along with the value of each variable which was modified.

Please note that we omitted the usual main function definition and other #include statements. We assume the following fragment to be inside a main function.

Program to Trace

1      int something = 5;

2      int data = 42;

3      int n = 0;

4     

5      for( n = 0 ; data > 0 ; n++){

6           data -= something;

7      }

8     

9      if( data < 0) n--;

Trace Table to Fill

Feel free to add / remove rows if necessary

Step #

Line #

Variables Values

Remarks

something

data

n

1

 

 

 

 

 

2

 

 

 

 

 

3

 

 

 

 

 

 

 

 

 

 

 

Question #2 – Testing Programs

You are going to write tests for the following program fragment which we assume to be inside a main function. Its requirements are to

-          Prompt the user for two int numbers X and Y

-          If Y is zero the program should display “Error #1” then exit.

-          Otherwise, if any of the two input value is less or equal to zero, it should display “Error #2” then exit.

-          Otherwise, it should display how many times Y goes into X. 

 

This is the equivalent of the integer division of X by Y.

 

Your objective is to write tests which will guarantee

-          The program conforms to the requirements

-          All possible execution paths have been tested

-          Your program does not feature any errors

Program to Test

1      int div = 0;

2      int data = 0;

3      int n = 0;

4      printf("Enter positive int to divide: ");

5      scanf("%d", &data);

6

7      printf("Enter positive int to divide it by: ");

8      scanf("%d", &div);

9     

10     if(div == 0){

11          printf("Error #1\n");

12          exit(EXIT_FAILURE);

13     }

14

15     if((data <= 0) || (div <= 0)){

16          printf("Error #2\n");

17          exit(EXIT_FAILURE);

18     }   

19

20     for( n = 0 ; data > 0 ; n++){

21          data -= div;

22     }

23

24     if( data < 0) n--;

25

26     printf("Ouput value is %d\n", n);

Tests Table to Fill

Feel free to add / remove rows if necessary

Test #

Inputs’ Values

Expected Results

Explanations;

What did you use this test for?

Why is it not redundant with others?

div

data

n

Error

1

 

 

 

 

 

2

 

 

 

 

 

3

 

 

 

 

 

 

 

 

 

 

Question #3 – Refactoring Programs

Refactor the following code to improve it in terms of readability, documentation, redundancy. Regarding redundancy, we really don’t like having twice the same printf/scanf but we don’t want a solution where the loop’s condition would be repeated twice either.

You will provide your version in the “Your Modified Version” subsection which is already pre-filled with the original. Then, for each thing you modified, use the table in the “Summary” subsection to summarize what you did & why you did it.

Program to Refactor

int grade = 0, total = 0, number = 0;

 

printf("Grade Average Program\n");

printf("Enter grade outside of [0..100] to stop\n");

 

printf("Enter a grade between 0 and 100 inclusive: ");

scanf("%d", &grade);

 

while((grade >0) && (grade <=100)){

total += grade; number += 1;

printf("Enter a grade between 0 and 100 inclusive: ");

scanf("%d", &grade);

}

Your Improved Version

int grade = 0, total = 0, number = 0;

 

printf("Grade Average Program\n");

printf("Enter grade outside of [0..100] to stop\n");

 

printf("Enter a grade between 0 and 100 inclusive: ");

scanf("%d", &grade);

 

while((grade >0) && (grade <=100)){

total += grade; number += 1;

printf("Enter a grade between 0 and 100 inclusive: ");

scanf("%d", &grade);

}


Summary of Improvements

Feel free to add rows to, or remove rows from, this table as needed;

 

What did you modify

How did you modify it?

Why did you modify it?

 

 

 

 

 

 

 

 

 

Question #4 – Debugging Programs

The following program has build-time errors – i.e. syntax, linker – along with runtime errors – i.e. logic of the program, crashes. Some statements might also be simply useless.

We need you to list all of these in the table below along with how you would fix them.

Program to Debug

1#include "stdio.c"

2#include "stdlib.c"

3     

4int main(){

5      int mystery = 5;

6      int tmp = 0;

7      double data = 0.0;

8     

9      do{

10          prinf("Enter a floating point value: ");

11          scanf("%d", data);

12    

13          tmp = (int)data;

14          if( tmp % 2)

15              prinf("The int part of %d", data);

16              prinf(" is an even number\n");

17          else

18              prinf("The int part of %d", data);

19              prinf(" is an odd number\n");    

20         

21          prinf("Enter 1.0 to enter another value:");

22          scanf("%d", data);

23     }while( data = 1.0 )

24 }

25    

Bugs Table

Identify each bug you find & provide the lines # where it happens. In addition, explain what the problem was, then how you fixed it. If the same bug appears at different line, just enter it one time in the table but specify all the lines where it happens.

 

Bug #

Lines #

Problem you identified

How you fixed it

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Your Fixed Version

Apply all the fixes you listed in the table to the code below to make it your fixed version. Please note that if a bug is fixed below but not in the table, or the other way around, you won’t get credit for it. You need to have both the bug and its explanations in the table and have it fixed below.

 

#include "stdio.c"

#include "stdlib.c"

 

int main(){

  int mystery = 5;

  int tmp = 0;

  double data = 0.0;

 

  do{

       prinf("Enter a floating point value: ");

       scanf("%d", data);

 

       tmp = (int)data;

       if( tmp % 2)

            prinf("The int part of %d", data);

            prinf(" is an even number\n");

       else

            prinf("The int part of %d", data);

            prinf(" is an odd number\n");    

      

       prinf("Enter 1.0 to enter another value:");

       scanf("%d", data);

  }while( data = 1.0 )

}     

  • 12 years ago
  • 20
Answer(0)
Bids(0)