August 1999
CA208 : 'C' PROGRAMMING

QUESTION 5

Total Marks: 20 Marks

Click here to access other questions

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

(a) Explain what an expression is in C. [2]
An expression is
  • A compund term
  • which can be reduced to a value;

 

(b) An assignment is one kind of expression in C. Explain what is meant by the term assignment. [2]
An assignment is the way in which
  • A variable
  • Is given a value;

 

(c) Write a function, called RunningTotal, which takes on value parameter, x, and returns a running total of all the values passed to the function during the execution of the program so far. [4]
A sample definition of RunningTotal follows:

int RunningTotal(int x) {
   static int total=0;
   total = total + x;
   return total;
}

 

(d) Using a switch statement, write a function foobar, which takes a character as a parameter. If the character passed in is "a" or "A", the function should exit immediately. If the character passed in is "b" or "B", nothing should happen. For any other value, the function should cause the program to exit immediately with a value of -1. [6]
A sample definition of foobar follows:

void foobar(char x) {
   switch(x)
     {
       case 'a' : case 'A' :
        return;
       case 'b' : case 'B' :
        break;
       default;
          exit(-1);
     }
}

 

(e) Write a function called fun that prompts the user to enter a number in the range 0-9. If the number entered is outside the required range, the prompt to the user should be repeated, otherwise the function will return the integer selected by the user. [6]
A sample definition of fun follows:

int fun( ) {
   int i;
   do {
       printf("Enter a number in the range 0-9:" );
       scanf("%d", &i);
     } while (i<0 || i>9);
    return i;
}