CS 156 : COMPUTER SYSTEM CONCEPTS TEST 1 (C PROGRAMMING PART) FEBRUARY 6, 2001 Student s Name: MAXIMUM MARK: 100 Time allowed: 45 minutes Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be. Q1 (30 marks) In each of the following program segments, find the error(s) in the program. (a) For ( x = 100, x >= 1, x++ ) printf( %d\n, x); (b) The following code should print whether a given integer is odd or even: switch (value %2 ) case 0: printf ( Even integer \n ); case 1: printf( Odd integer ); (c) The following code should input an integer and a character and print them. Assume the user types as input 100 A. scanf( %d, intval ); charval = getchar(); printf( Integer : %d \ncharacter : %c \n, intval, charval); (d) for (x = 0.000001; x =< 0.0001; x+= 0.000001) printf( %.7f\n, x); (e) The following code should output the odd integers from 999 to 1 for (x = 999; x >= 1; x =+ 2) printf( %d\n, x); 1
(f) The following code should output the even integers from 2 to 100 (inclusive): counter = 2; do if (counter % 2 = 0) ; printf( %d\n, counter); counter ++; while (counter < 100); (f) The following code should sum the integers from 100 to 150. Total=0; for (x = 100; x <= 150; x++); total =+ 150; Q2 (24 marks). For each of the following, indicate whether the assertion is TRUE or FALSE. s/n Assertion True or False 1 In C, it is illegal for a function to call itself 2 Static variables are variables whose values cannot change during program execution 3 Variables declared as static within a function are not accessible from outside the function where they are declared 4 In general, C functions are only accessible from within the file where they are defined 5 A function prototype is always necessary before a function can be invoked in a program 6 Any selection structure (if, if/else, and switch) can always be replaced by one or more simple if structures 7 In a repetition structure, the continue statement always forces execution to continue from the next statement after the repetition structure 8 Counter-controlled repetition is typically used for indefinite repetition while sentinel-controlled repetition is typically used for definite repetition 9 In C, all variables must be declared before they are used, however such declarations can appear anywhere within the program 10 The parenthesis operator ( ) has the highest precedence of all C operators 11 In general, the unary forms of the arithmetic operators (+, -, *) always have a higher precedence than the corresponding binary operators 12 Every C program contains the function main() which must always come before other functions in the program text 2
Q3 (20 marks) For each of the following program segments, list the results that will be produced if the program were to be executed. (a) What is the output of the following program? #include <stdio.h> int main() int x = 1, total = 0, y; while (x <= 5) y = x * x; printf( %d \n, y); total += y; ++x; printf( Total is %d\, total); return 0; (b) What is the output of the following program? #include <stdio.h> int main() int row=6, column; while (row >= 1) column =1 ; while (column <= 6) printf( %s, row % 2? < : > ); ++column; --row; printf( \n ); return 0; 3
Q4 (26 marks) (a) A point is usually defined by its x- and y-coordinates. Given two points p 1 = (x 1,x 2 ) and p 2 = (x 2,y 2 ), the distance between them is defined by: d(p 1,p 2 ) = [(x 1 -x 2 ) 2 + (y 1 -y 2 ) 2 ] ½. Assume we have 7 pairs of points and we want to find the distance between each pair. By making use of a function distance(), write a program that reads each of the pair of points from the standard input, and prints out the distance between them. Remember that each pair of points will contain 4 real numbers. You will need to write a function distance(x 1,y 1, x 2,y 2 ) that takes the coordinates of two points as its inputs and then returns the distance between the input positions. The function should accept real numbers as inputs and should also return real numbers as the output. You can assume the existence of two standard functions: pow(x, n) which returns the value of x n, and sqrt(n) which returns the square root of n. Assume also that the input and outputs for the two functions are all real numbers. 4
(b) Given the following declarations, answer the following questions: int a; float b; double c; c=a+b; a=b+c; b=a+c; What will be the data type for the result of the following statement? a*b*c; 5