Professional Documents
Culture Documents
Section A (35 Marks) : Answer
Section A (35 Marks) : Answer
Section A (35 Marks) : Answer
1. Explain what is a computer program, and state two reasons why a computer program is
developed.
[4 marks]
Answer
A Computer Program is a set of instructions that directs a computer to perform task.
2. Fatimah receives a fixed amount of pocket money from her father every month. She keeps a
record of her daily expenditure. Design an algorithm to
read the amount of pocket money which Fatimah receives for the month of January,
read and store the amount of pocket money which Fatimah spends each day for the month of
January,
calculate and display the average, maximum amount and minimum amount of pocket money
which Fatimah spends for the month of January,
calculate and print the balance of Fatimah’s pocket money for the month of January.
[4 marks]
Answer
Pseudocode
1. START
2. Variables : pocketMoney, spend[31], totalSpend, maxSpend, minSpend, avgSpend, balance
3. Read pocketMoney
4. Read amount spend Fatimah daily
5. Calculate totalSpend
6. If maxSpend < amount spend then assign amount spend to maxSpend
7. If minSpend > amount spend then assign amount spent to minSpend
8. Repeat step 4 to step 7 until control loop variable equal to 31
9. avgSpend = totalSpend/31
10. balance = pocketMoney – totalSpend
11. Print maxSpend, minSpend, avgSpend and balance
12. END
2
Flowchart
Program
#include <stdio.h>
void main()
{
int i;
float pocketMoney, spend[31], totalSpend = 0, maxSpend = 0,
minSpend = 0, avgSpend, balance;
maxSpend = spend[0];
for(i=0; i<31; i++) {
if (maxSpend < spend[i])
maxSpend = spend[i];
}
minSpend = spend[0];
for(i=0; i<31; i++) {
if (minSpend > spend[i])
minSpend = spend[i];
}
avgSpend = totalSpend/31;
balance = pocketMoney - totalSpend;
}
3
int x = 15;
int y = 45;
int z = 20;
int answer;
a) Show the operations to determine the output of the above code segment. [2
marks]
b) Rewrite the above code segment in C using the if...else statement by excluding the
logical operators. Display YES if answer is 1 or NO if answer is 0. [5 marks]
Answer
a)
answer = (x == 10 && y < 50 && z > 25)
= (15 == 10 && 45 < 50 && 20 > 25)
= ( 0 && 1 && 0)
= ( 0 && 0)
= 0
= 0
Output
Answer is 0
b)
if (x == 10) {
if (y < 50)
if (z > 25)
printf(“YES”);
}
else
printf(“NO”);
x1 = initialNumber + incrementalValue x i
where 1 ≤ i ≤ n.
[6 marks]
4
arithmeticSequence(2,3,6);
[2 marks]
Answer
a)
void arithmeticSequence(int initialNumber, int incrementalValue, int n)
{
int i, seq = 0;
for (i = 1; i <= n; i++) {
seq = initialNumber + incrementalValue * i;
printf(“%d, “, seq);
}
}
b)Output
5, 8, 11, 14, 17, 20
int i;
for (i=1; i <= 4; i++)
scanf(“%d”, &num[i]);
sum [1] = num [l];
for (i=2; i <= 4; i++)
sum[i] = sum[i – 1] + num[i];
a) Write declaration statements in C to declare the required variables of the above code
segment. [2 marks]
b) Determine the values of sum[] when the input numbers are 25, 10, 31 and 14.
[4 marks]
c) Write a for loop statement in C to print the final values of sum[]. An example of the
display is given as follows:
sum[1] = …
sum[2] = …
sum[3] = …
sum[4] = …
[2 marks]
5
Answer
a)
int num[5], sum[5];
b)
sum[1] = 25
sum[2] = 35
sum[3] = 66
sum[4] = 80
c)
for (i=1, i <= 4; i++)
printf(“sum[%d] = %d\n”, i,sum[i]);
6. The body mass index (BMI) to measure overweight and obesity of a person is given by
weight in kilogrammes
BMI =
(height in metres)2
The BMI standards and its corresponding category are as follows:
BMI Category
≤ 18.4 Underweight
18.5 – 24.9 Normal
25.0 – 29.9 Overweight
30.0 – 39.9 Obese
≥ 40.0 Extremely obese
b) Write a program in C for the flowchart that you have drawn in (a). [8 marks]
6
Answer
a) Flowchart
b) Program
#include <stdio.h>
void main()
{
float weight, height, bmi;
7. The information about the number of contract employees of a supermarket for the year 2012 is as
follows:
[15 marks]
Answer
#include <stdio.h>
void main(){
int employee2012[4] = {64, 122, 70, 23};
int i, quarter, maxEmployee = 0;
switch(quarter) {
case 0 :
printf(" Jan - Mar \t");
break;
case 1 :
printf(" Apr - Jun \t");
break;
case 2 :
printf(" Jul - Sep \t");
break;
case 3 :
printf(" Oct - Dec \t");
break;
}
printf("\t%d\n", maxEmployee);
}
8