Professional Documents
Culture Documents
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 :
Answer
Start
Variables:
i = 50
sum = 0
i <= 100 No
Yes
sum = sum + 1
Print sum
i = i + 1
End
Answer
x = 2 y = 12
k = 0 m = 1.00
x = 3 j = 6
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
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( )
{
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.
[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( ){
thousands = (totCars/1000);
totCars1 = totCars % 1000;
hundreds = (totCars1/100);
totCars2 = totCars1 % 100;
tens = (totCars2/10);
totCars3 = totCars2 % 10;
ones = totCars3;
return 0;
}
[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
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