You are on page 1of 6

Universiti Malaysia Perlis

EKT 120 COMPUTER PROGRAMMING

TEST I + (ANSWER SCHEME)

DATE : 22th October 2012


TIME : 8.00 AM – 9.30 AM

NAME :

MATRIX NO :

PROGRAM : VENUE:

LECTURER :

Instructions:

1. This is a close book test. No books and notes are allowed to be referred to during the test.
2. There are TWO (2) parts of questions. Answer ALL questions. Please write your answer
in the column provided.
3. Time given: 1.5 Hours.
4. Number of pages (including this page) : 6

PART A / 30 PART B/ 20 TOTAL MARKS / 50

1
PART A: SHORT ANSWER [30MARKS]

1. Based on the LinuX environment programming, compile the "name.c" program to result the
output to a file name "name.out". State three (3) methods of command that can be use for
both compiling and execution/running the “.c” file and “.out” file. [6 marks]

gcc name.c [1mark]


./a.out [1mark]

gcc –o name.out name.c [1Mark]


./name.out [1Mark]

gcc name.c –o name.out [1 Mark]


./name.out [1 Mark]

2. Write a single printf statement to accomplish the following output:[5marks]


a. Happy Holiday printf(“Happy Holiday”); [1Mark]
b. Happy Holiday printf(“Happy\tHoliday); [1Mark]
c. Happy
Holiday printf(“Happy\nHoliday”); [1Mark]
d. -------------------- printf(“----------------------”); [1Mark]
e. Total marks is 50% printf(“Total marks is 50%%”); [1Mark]

3. Rewrite the mathematical expressions below into a correct C statement. [5marks]


x2
a. 𝑎 = y3 a=(x*x)/(y*y*y) [1Mark]
4
b. 𝑣𝑜𝑙𝑢𝑚𝑒 = 3 𝜋𝑟 3 volume=(4.0/3.0)*PI*r*r*r [1Mark]

c. 𝑎 ≠ 𝑏 a!=b [1Mark]
d. 𝑧 ≤ 𝑥 z<=x [1Mark]
e. 𝑚 ≥ 𝑛 m>=n [1Mark]

2
4. double dDistance = 52.3134;
Given the declaration and initialization of variable dDistance as above, write the output of
the following printf() statement:-

a. prinf(“%f”, dDistance); [1 mark]

5 2 . 3 1 3 4 0 0

b. printf(“%10f”, dDistance); [1 mark]

5 2 . 3 1 3 4 0 0

c. prinf(“%.2f”, dDistance); [1 mark]

5 2 . 3 1

d. prinf(“%9.3lf”, dDistance); [1 mark]

5 2 . 3 1 3

3
5. Write a code segment based on the flowchart shown in Figure 1. [10marks]

Initialize flag = 0

true
iSpeed >50? Set flag = 1

false

Decrease iIndex by 5
Increase iSpeed by 1

true flag !=1?

false

Figure 1
[Answer]
flag = 0; [1M]
do [1.5M]
{
if(fSpeed >50) [1.5M]
flag=1; [1M]
else [1.5M]
{
iIndex -=5; [1M]
iSpeed++; [1M]
}
}while(flag != 1); [1.5M]

4
PART B: PROGRAMMING [20MARKS]

1. Write a program that will display a conversion table of temperature in Celsius to Fahrenheit.
The program requires the user to enter initial Celsius temperature, Celsius temperature limit
and the increment of Celsius value. The formula for conversion:
Fahrenheit=1.8×Celsius+32.0. Please use for.. statement.
Sample output:
Table of Celsius to Fahrenheit Conversion
Please enter the initial Celsius value:
5
Please enter the limit Celsius value:
25
Please enter the increment Celsius value:
5
Celsius Fahrenheit
5 41.00
10 50.00
15 59.00
20 68.00
25 77.00
a) Draw the flowchart for the program.[8Marks]

Start [0.5Mark]

Read initial,limit,increment
value in Celsius [1Mark]

Display table heading


[1Mark]

C=begin [1Mark]

[1Mark] [1Mark] [1Mark]


T
C<=limit Conversion from C Display Celsius value [1Mark]
to F using formula with corresponding Increment C based
Fahrenheit value on increment value
F

End [0.5Mark]

5
b) Write your program based on flowchart in (a).[12Marks]

#include<stdio.h> [0.5mark]
int main ()
{
int C,begin,limit,step; [1.5mark]
float F; [0.5mark]

printf("Table of Celsius to Fahrenheit Conversion\n"); [0.5mark]


printf("Please enter the initial Celsius value:\n"); [0.5mark]
scanf("%d",&begin); [0.5mark]
printf("Please enter the limit Celsius value:\n"); [0.5mark]
scanf("%d",&limit); [0.5mark]
printf("Please enter the increment Celsius value:\n"); [0.5mark]
scanf("%d",&step); [0.5mark]

printf("Celsius Fahrenheit\n"); [0.5mark]

for(C=begin;C<=limit;C+=step) [1.5mark]
{
F=1.8*C+32.0; [1mark]
printf("%3d%12.2f\n",C,F); [2mark]
}
return 0; [0.5mark]
} [0.5mark]

You might also like