Professional Documents
Culture Documents
INSTRUCTIONS TO CANDIDATES
NAME
STUDENT ID GROUP
LECTURER
1 CLO1-PLO1
2 CLO1-PLO1
3 CLO1-PLO1
4 CLO1-PLO1
TOTAL MARKS
QUESTION 1
(2 marks)
Answer
(i)
(ii)
(iii)
(iv)
Function
• •
Point/terminal
• •
Condition/Decision
• •
Process/Data
• •
c) Determine the value is assigned for each variable after the execution of each line of the
statement.
(6 marks)
a b c
int a, b, c;
b++;
b=b-2;
++a;
c=7%3+4*2-(1+12)/4;
b=(c++) - a;
QUESTION 2
a) Using if…else if statement, write the source code to complete Listing Q2a to display a
message based on Table Q2a. Display “Invalid value” if the temperature is not in
the list.
(4 marks)
Table Q2a
Temperature Message
<=35 Hypothermia
35 – 37.5 Normal
#include <stdio.h>
int main()
{
float temp;
printf(" Enter your body temperature: ");
scanf("%f", &temp);
printf(" entered value = %f\n", temp);
if ( (i) )
printf(" Hypothermia\n");
else if ( (ii) )
printf(" Normal \n");
else if ( (iii) )
printf("Fever \n");
else
(iv) )
}
Listing Q2a
Answer:
(i)
(ii)
(iii)
(iv)
b) Modify the program in Listing Q2b with an equivalent program by replacing if..else with
switch..case statement.
(8 marks)
#include<stdio.h>
int main(){
int id;
float salary, Annual_Salary;
printf("WELCOME TO BZ TRAVEL\n");
printf("Please key-in your ID:\t");
scanf("%d",&id);
Answer:
#include<stdio.h>
int main(){
int id;
float salary, Annual_Salary;
printf("WELCOME TO BZ TRAVEL\n");
printf("Please key-in your ID:\t");
scanf("%d",&id);
/* Part A: Determine the salary */
QUESTION 3
a) Rewrite a new program that is equivalent to the program in Listing Q3a by replacing for
loop with do...while loop statement.
(6 marks)
void main()
{
int i, j, sum;
for(i=1; i<=3; i++)
{
sum=0;
do
{
sum +=5;
printf(“Sum = %d\n”,sum);
}
while (sum<=10);
printf(“Loop = %d\n\n”,i);
}
return 0;
}
Listing Q3a
Answer:
b) Figure Q3b shows the flow chart of the program to detect value seven (7) when the user
enters a number between one (1) until ninety-nine (99). Based on Figure Q3b construct
the complete C statement by using while loop and break statement.
(6 marks)
START
Display Enter
any value
between 1 - 99
Store value in
valCheck variable.
NO
END
Display Enter
again!
Figure Q3b
Answer:
int main()
{
{
if
else
{
}
}
return 0;
}
QUESTION 4
Listing Q4 shows a simple source code using a selective statement. Re-write the source code
by splitting the selective statement into a function named display(int choice).
(6 marks)
#include <stdio.h>
int main()
{
int choice;
printf("Enter your choice:\n");
scanf("%d",&choice);
/* Part A: DISPLAY */
if (choice ==1)
printf("Booking\n");
else if (choice== 2)
printf("Cancel Booking\n");
else
printf("None of the above\n");
return 0;
}
Listing Q4
Answer:
#include <stdio.h>
int main()
{
int choice;
printf("Enter your choice:\n");
scanf("%d",&choice);
return 0;
}