Professional Documents
Culture Documents
EE/APR2011/ECE246
INSTRUCTIONS TO CANDIDATES 1. This question paper consists of three (3) parts. PART A (1 Question) PART B (3 Questions) PART C (2 Questions)
2.
Answer ALL questions in the Answer Booklet. Start each answer on a new page. i) ii) Answer PART A in the True / False Answer Sheet. Answer PART B and C in the Answer Booklet. Start each answer.
3.
Do not bring any material into the examination room unless permission is given by the invigilator. Please check to make sure that this examination pack consists of: i) the Question Paper ii) an Answer Booklet - provided by the Faculty iii) A True/ False Answer Sheet - provided by the Faculty
4.
CONFIDENTIAL PART A
EE/APR2011/ECE246
QUESTION 1 Answer as TRUE or FALSE a) Lines beginning with a # are processed by the preprocessor before the program is compiled. In the nested i f e l s e statement, once the condition is met, the rest of the statements are skipped. The following function prototype corresponds with a function that accepts a float as a parameter and returns an integer. int square( int ) ; d) Initializing an integer array called numbers with 5 elements. i n t numbers [ 5 ] = { 2 , 3, 4, e) f) g) h) 5};
b)
c)
Functions are invoked by arguments. Pointers require using a * before each variable declaration. It is an error to use the f o r statement within a w h i l e loop. The following code defines and initializes a double-subscripted array of 4 integers. . i n t b [ 2 ] [ 1 ] = { { 1,4 }, { 3 , 4 } };
i) j)
The index value specifies the position of the element in the array. If p t r is declared as an double, 4 bytes would be copied into that memory location pointed to by p t r . (20 marks)
CONFIDENTIAL
CONFIDENTIAL PARTB
EE/APR2011/ECE246
QUESTION 1 a) Define all data types that are associated with variables. (5 marks) b) Consider the following pseudocode: To test an integer number is a multiple of 2 1. Start 2. Get input read number 3. Check number IF number % 2 == 0 Print "This number is a multiple of 2" ELSE Print "This number is not a multiple of 2" END IF 4. End
c) d)
State 3 differences between Recursion and Iteration. (5 marks) Fill in the blanks with a C statement or expression inside each of the following program so that the given output is obtained. i)
r e t u r n 0;
I
Output:
6 9 1
(3 marks)
Hak Cipta Universiti Teknologi MARA
CONFIDENTIAL
CONFIDENTIAL
EE/APR2011/ECE246
ii)
10
17
18
QUESTION 2 Write down the expected output for the following programs: a)
#include <stdio.h> int main (void)
{
int y=0; printf ("\tY\t\tlO*Y\t\tY*Y\n\n");
{
printf("\t%d\t\t%d\t\t%d\n",y,10*y,y*y);
}
return 0;
(5 marks)
CONFIDENTIAL
CONFIDENTIAL
EE/APR2011/ECE246
{
int sum=0; int i ; int a[3]={32,27,64}; int *aptr; aptr=a; for (i=0;i<3;i++)
{
printf ("The value of array a is %d\n" ,a[i]) ; printf("aptr + %d = %d\n",i, *aptr+i);
}
return 0;
}
(5 marks) QUESTION 3 a) Write a C statement that performs each indicated task. Assume that floating-point variables v a r i and var2 are defined and that v a r i is initialized to 3 .8 . i) ii) iii) iv) v) vi) Define the variable f p t r r to be pointer to an object of type f l o a t . Assign the address of variable v a n to pointer variable f P t r . Print the value of the object pointed to by f p t r . Assign the value of the object pointed to by f P t r to variable var2 . Display the address of v a r i . Use the %p conversion specifier. Display the address stored in f P t r . Use the %p conversion specifier. Is the value printed the same as the address of v a r i ? If yes, why? (12 marks)
CONFIDENTIAL
CONFIDENTIAL
EE/APR2011/ECE246
b)
ii)
M=
iii) z -
4(* + y) 2mn
(6 marks) c) Write statements to accomplish the following: i) Define integer array named myArray which has 2 rows and 2 columns. Assume the symbolic constant S I Z E has been defined to be 2. How many elements does the array myArray contain? Display the total number of elements. Use a for repetition statement to initialize each element of myArray to the sum of its sub-scripts. Assume the integer variables a and b are defined as control variables.
ii)
iii)
iv)
CONFIDENTIAL
CONFIDENTIAL PARTC
EE/APR2011/ECE246
QUESTION 1 Write a program that use a while loop to print integer values from start to end value. The user has to input the start and end value. For example, if user input start v a l u e = 4 and end v a l u e = 12, the output will be as follows: Input start value: 4 Input end value: 12 Output: 4 5 6 7 8 9 10 11 12
(20 marks)
QUESTION 2 Write C program that asks the user to enter his/her age. Use function f i n d c a t e g o r y to determine the category based on the user's age. Refer the following output.
Output: Age Category <2 Baby 2-14 Child 15-21 Teenagers >21 Adult Please enter your age: Your category is Child 7
(20 marks)
CONFIDENTIAL