December 1998
CA208: 'C' PROGRAMMING

QUESTION 4

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to Question 4

 

(a) What is the effect of adding the keyword static to a local varibale in a procedure? [2]
  • the variable retains its value between function calls (1 mark),
  • and exists throughout the lifetime of the program execution (1 mark).

 

(b) Describe the difference between call-by-reference and call-by-value parameter passing in C. [4]
  • If a formal parameter of a function or procedure is passed via a call-by-reference parameter passing scheme, then any changes to that variable during the lifetime of the procedure, invocation will carry through after the procedure has returned (2 marks).
  • In contrast, changes to variables which are passed using the call-by-value scheme are only retained during the lifetime of the procedure/function invocation (2 marks).
  • call-by-reference parameter passing is achieved in C by using pointers as the formal arguments to functions (2 marks)
  • 2 marks for each point above, to a maximum of four marks.

 

(c) Find the eight syntax errors in the following fragment of code:

include <stdio.h>

void func(int x[], int y)

main() {
   int a(5), b, i;

   for (i=0;i<5,i++) a[i] = 2*1;

   b==16;
   for(i=0;i<5;i++) printf("%d\n,a[i]);
   return;
}

void func(int x[], int y);
{
   integer i;
   for(i=0;i<5;i++) {
      x[i]+=2;
   y+=2;

}

 

[8]
include <stdio.h>                 /* missing # 1 mark */

void func(int x[], int y) /* missing ; 1 mark */

main() {                          /* ok missing void(s) 0 marks */
   int a(5), b, i; 
     /* (5) should be [5] 1 mark */

   for (i=0;i<5,i++) a[i] = 2*1; /* last, should be ; 1 mark */

   b==16;             /* ok null statement, 0 mark */
   for(i=0;i<5;i++) printf("%d\n,a[i]);
/* missing " 1 mark */
   return;
                        /* ok return, 0 mark */
}

void func(int x[], int y); /* extra ; 1 mark */
{
   integer i;        
/* integer should be int 1 mark */
   for(i=0;i<5;i++) { 
/* no close } 1 mark */
      x[i]+=2;
   y+=2;
                                              /* ok missing return 0 marks */
}

(d) Write a function that prompts the user to enter a number in the range 0-4. If the number entered is outside the required range, a prompt for user input should be repeated; otherwise the function returns the integer selected by the user.

 

[6]
A definition of the function menu (the students may have used an alternative function name) follows:

int menu() {
   int i=-1;

   while (i<0 || i>4) {
      printf("Enter a digit in the range
              0--4?");
      scanf("%d", &i);
   }
   return i;
}

an alternative definition that uses a do-while is shown below:

int menu() {
    int i;

    do {
       printf("Enter a digit in the range
               0--4?");
       scanf("%d", &i);
    } while (i<0 || i>4)
    return i;
}

and use the following marking scheme should be used for either solutions:

  • one mark for a correct procedure definition.
  • one mark for using an iteration statement.
  • two mark for repeatedly iterating while the entered value is not in the range 0-4. i.e., one mark for the expressions i<0 and i>4, and one mark for the use of ||. Alteratively, the students may have used (i>=0 && i<=4), which should also be awarded two marks.
  • one mark for printing a prompt (any text will do) with printf.
  • one mark for using scanf to read in a digit.
  • no marks should be deducted for trivial syntactic errors.