You are on page 1of 5

1

STPM 2012 – ICT

1. A code segment in C that uses a while loop add consecutive integers from 50 to 100 and
(1) display the sum is given as follows :

int i = 50, sum = 0;


while (i <= 100) {
sum = sum + 1;
i = i + 1;
}
printf(“Sum = %d\n”, sum);

Draw a flowchart to represent the above code segment. [4 marks]

Answer
Start

Variables:
i = 50
sum = 0

i <= 100 No

Yes

sum = sum + 1
Print sum
i = i + 1

End

2. A program in C is given as follows :


(4)
#include <stdio.h>
void main () {
int x = 2, y, k, j=3;
float m;
y = x + x * 5;
printf(“x = %d y = %d\n”, x, y);
k = j / 5;
m = j / 3.0;
printf(“k = %d m = %.2f\n”, k, m);
x++;
j += j;
printf(“x = %d j = %d\n”, x, j);
}

Write the output for the above program. [6 marks]


2

Answer

x = 2 y = 12
k = 0 m = 1.00
x = 3 j = 6

3. The declarations in C for an array of subject is given as follows :


(5)
char subject[12];

a) Copy and complete the following table for the array subject by filling in the element index
and value for Computing958. [1 marks]

Array subject
Element index

Element Value

b) Write a declaration statement in C to assign the initial values of Computing958 in the


array subject. [2 marks]
c) Write a code segment in C using for statement which displays the following output :

Computing958
[3 marks]

Answer
a)
Array subject
Element index
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Element Value
C o m p u t i n g 9 5 8

b)
char subject[12] = {'C','o','m','p','u','t','i','n','g','9','5','8'};

c)
void main () {
int i;
for (i = 0; i < 12; i++)
printf(“%c“,subject[i]);

}
3

4. A code segment in C thet reads the total number of cars that pass through a toll lane in a day is
(9) given as follows :

#include <stdio.h>
main( )
{

int totCars, thousands, hundreds, tens, ones;

printf(“please enter the total number of cars :”);


scanf(“%d”, &totCars);
.
.
.
return 0;
}

a) Write a pseudocode to separate the total number of cars into individual digits. The psedocode
should include each of the following :
Read the total number of cars into individuals digits. The pseudocode should include each of the
following :
 Read the total number of cars.
 Calculate thousands digit of cars.
 Calculate hundreds digit of cars.
 Calculate tens digits of cars.
 Calculate ones digits of cars,
 Print thousands digits, hundreds digit, tens digits and ones digits of cars.
[6 marks]

b) Assume that the total numbers of cars are not more than 9999. Based on pseudocode in (a),
write a program in C using the above code segment to produce the following output.

Please enter the total number of cars : 3697


The total number of cars in its corresponding units
Thousands = 3, Hundreds = 6, Tens = 9, Ones = 7

[6 marks]

Answer
a)

1. START
2. Variables : totCars, totCars1, totCars2, totCars3, thousands, hundreds, tens, ones
3. Input totCars
4. thousands = totCars/1000
5. totCars1 = totCars % 1000
6. hundreds = totCars1/100
7. totCars2 = totCars1 % 100
8. tens = totCars2/10
9. totCars3 = totCars2 % 10
10. ones = totCars3
11. printf Thousands, Hundreds, Tens, Ones
12. END
4

b)
#include <stdio.h>
main( ){

int totCars, totCars1, totCars2, totCars3, thousands, hundreds,


tens, ones;

printf("Please enter the total number of cars : ");


scanf("%d", &totCars);

thousands = (totCars/1000);
totCars1 = totCars % 1000;

hundreds = (totCars1/100);
totCars2 = totCars1 % 100;

tens = (totCars2/10);
totCars3 = totCars2 % 10;

ones = totCars3;

printf ("The total number of cars in its corresponding units\n");


printf ("Thousands = %d, Hundreds = %d, Tens = %d, Ones = %d\n",
thousands, hundreds, tens, ones);

return 0;
}

5. A declaration in C is given as follows :


(10)
int num[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int i, k;

a) Determine the value of k for each of the following C statements :


(i) k = num[5]; [1 marks]
(ii) k = num[1] * num[4]; [1 marks]
(iii) k = 0;
for (i = 0; i < 5; i++) {
k += num[i];
}

[2 marks]
b) Give the output for the following code segment in C.

int i, j, result;
result = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i; j++) {
result += num[j];
printf(“i = %d, j = %d, result = %d\n”, i, j,
result);
}
} [9 marks]
5

Answer
a)
a) k = num[5];
= 6

ii)k = num[1] * num[4];


= 2 * 5
= 10

iii)k = 15

b)

i = 1, j = 0, result = 1
i = 2, j = 0, result = 2
i = 2, j = 1, result = 4
i = 3, j = 0, result = 5
i = 3, j = 1, result = 7
i = 3, j = 2, result = 10

You might also like