You are on page 1of 7

CONFIDENTIAL

EE/APR2011/ECE246

UNIVERSITI TEKNOLOGI MARA FINAL EXAMINATION

COURSE COURSE CODE EXAMINATION TIME

INTRODUCTION TO C PROGRAMMING ECE246 APRIL 2011 2 HOURS

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.

DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO


This examination paper consists of 7 printed pages
Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

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)

Hak Cipta Universiti Teknologi MARA

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

Convert the pseudocode to flowchart form. (4 marks)

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)

i n t main () { i n t b [ ? ]={ 4, 6, 9 , 1 }; i n t x; for (x= ? ; X<4; X++) printf("%d ? " , b [x] ) ;

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)

I n t main () { i n t i= ? i n t my_array[5]; while (i < { ? )

my_array[i]=4 * i + 5 ; i f (i%2==0) my_array[i]=my_array[i]- 3; p r i n t f ("%d\t", ? ); i = i +1; { r e t u r n 0;


{ Output

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");

while (++y <=7)

{
printf("\t%d\t\t%d\t\t%d\n",y,10*y,y*y);

}
return 0;

(5 marks)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

EE/APR2011/ECE246

#include <stdio.h> int main ()

{
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)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

EE/APR2011/ECE246

b)

Write a single c statement that accomplish each of the following: i) Total

_g(r"-l) r-1 b + Aac 2a


,x+3

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)

Assume t h e a r r a y was i n i t i a l i z e d w i t h definition: i n t myArray [SIZE] [SIZE]


Output: myArray myArray myArray myArray

[0] [0] [1] [1]

[0]=2 [1]=1 [0]=8 [1]=0

iv)

Display the values of each element of myArray (12 marks)

Hak Cipta Universiti Teknologi MARA

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)

END OF QUESTION PAPER

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

You might also like