Professional Documents
Culture Documents
INSTRUCTIONS TO CANDIDATES
NAME
STUDENT ID GROUP
LECTURER
QUESTION CLO-PLO MARKS
1 CLO1-PLO1
2 CLO1-PLO1
3 CLO1-PLO1
4 CLO1-PLO1
5 CLO1-PLO1
6 CLO1-PLO1
TOTAL MARKS
QUESTION 1
Refer to the Listing Q1, identify the missing statement or expression inside each of the
following programs so that the given output as given in Figure Q1 is obtained.
(4 marks)
QUESTION 2
a) Consider the following program in Listing Q2a. Draw a flowchart to represent the if
statement used in the blue box given.
(6 marks)
int main()
{
int x;
printf("Please enter a number:");
scanf ("%d",&x);
if(x > 0)
{
printf("\nThe number %d is greater than 0",x);
}
else
{
printf("\nThe number %d is less than or equal zero",x);
}
return 0;
}
Listing Q2a
Answer:
b) Modify the program in Listing Q2b with equivalent program by replacing switch with if..else
statement.
(5 marks)
int main() {
int input;
printf( "1. Play game\n" );
printf( "2. Play multiplayer\n" );
printf( "3. Exit\n" );
printf( "Selection: " );
scanf( "%d", &input );
switch ( input ) {
case 1:
printf( "Option 1 selected\n" );
break;
case 2:
printf( "Option 12 selected\n" );
break;
case 3:
printf( "Option 3 selected\n" );
break;
default:
printf( "Bad input, quitting!\n" );
break; }
return 0; }
Listing Q2b
Answer:
QUESTION 3
Repetition is a sequence of statements that keep repeating until certain condition is reached.
Listing Q3 shows a source code that used repetition statement to accomplish a specific task.
a) List and describe two types of loop.
(3 marks)
b) With the aid of a diagram, illustrate the flowchart of the repetitive statement in Listing Q3.
(3 marks)
c) As referring to Listing Q3, if a user enters value ‘10’ during line 5 execution, predict the
answer that will be displayed as output during line 10 execution.
(2 marks)
d) Explain your answer in Question 3c, hence, find an appropriate word/sentence for the
blank part in line 6.
(3 marks)
1. int main()
2. {
3. int i,n;
4. printf(“\nEnter A Number: “);
5. scanf(“%d”,&n);
6. printf(“\n BETWEEN 1 AND %d ARE: \n”,n);
7.
8. for(i=1;i<n;i+=2)
9. {
10. printf(“%d”,i);
11. }
12. }
Listing Q3
Answer:
a)
b)
c)
d)
QUESTION 4
Answer:
b) Find the missing statement in the blanks (i) – (ii) in Listing Q4b to produce the output as
shown in Figure Q4b. Hence, identify a global variable from the listing.
(4 marks)
#include <stdio.h> Answer:
#include <stdlib.h>
int x = 5; i)
void dip1(); ii)
void dip2(int);
int main()
{
global variable:
printf("ECE128: C Programming\n");
(i) ;
for (int i = 1; i <6;i++)
{
if (i==2)
continue;
(ii) ;
}
return 0;
}
void dip1()
{
printf("Hello All %d Students\n\n",x);
}
void dip2(int i)
{
printf("Hello Student %d\n",i);
}
Listing Q4b
ECE128: C Programming
Hello All 5 Students
Hello Student 1
Hello Student 3
Hello Student 4
Hello Student 5
Figure Q4b
c) By referring to Figure Q4c(i), write the missing statement in Listing Q4c using for loop to
ask the user to enter any three (3) integers between 10 and 50 inclusively. All entered
integers will be passed to a function named "MULL" that will multiply all three integers that
has been entered by the user. The function returns the result to the main function and
displayed the output shown in Figure Q4c(ii).
(5 marks)
start
Start MULL(int
x)
Function MULL
declaration
Calculate total
Declare global variable
total as integer and
initialize with 1
Return total
Declare int result, and
temp. Initilize temp = 0
No
Display
i<4?
Result
Yes
Result=MULL(temp)
i++
Figure Q4c(i)
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 9 EE/JAN 2021/ECE128
#include <stdio.h>
#include <stdlib.h>
int ;
int total = 1;
int main()
{
int result,temp = 0;
printf(“Please enter any 3 numbers between 10 to 50\n”);
for
{
printf(“Please enter %d number = “,i);
scanf(“%d”,&temp);
result= MULL(temp);
}
printf(“Your result = %d”,result);
}
{
total = total * x;
return total;
}
Listing Q4c
Figure Q4c(ii)
QUESTION 5
(6 marks)
Answer:
i)
ii)
iii)
b) Based on Figure Q5b(i), construct the statement to display each element in array arr[ ]
as shown in Figure Q5b(ii) using for loop statement.
(6 marks)
Figure Q5b(ii)
Answer:
QUESTION 6
Answer:
i)
ii)
b) Write the printf statement in Listing Q6b to display each array element’s value of price
as shown in Figure Q6b.
(3 marks)
int main () {
p = price;
Listing Q6b
Figure Q6b
c) Complete the program in Listing Q6c to produce an expected output as shown in
Figure Q6c:
(5 marks)
int main()
{
int *ptr;
int x;
Listing Q6c
x=0
*ptr = 0
x =5
*ptr = 5
x=6
*ptr = 6
x=5
*ptr = 5
Figure Q6c
Answer:
i)
ii)
iii)
iv)
v)