Section A (35 Marks) : Answer

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

1

UP2 STPM 2013 – ICT


Section A [35 marks]
Answer all questions in this section.

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.

Two reasons why a computer program is developed are :


1. To solve problems.
2. To automate process.
3. To compute faster

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;

printf("Input Pocket money : ");


scanf("%f", &pocketMoney);

for(i=0; i<31; i++) {


printf("Input Daily spend amount day %d : ", i+1);
scanf("%f", &spend[i]);
totalSpend += spend[i];
}

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;

printf("Average spend amount : RM%.2f\n", avgSpend);


printf("Maximum spend amount : RM%.2f\n", maxSpend);
printf("Minimum spend amount : RM%.2f\n", minSpend);
printf("Balance of pocket money : RM%.2f\n", balance);

}
3

3. A code segment in C is given as follows:

int x = 15;
int y = 45;
int z = 20;
int answer;

answer = (x == 10 && y < 50 && z > 25);


printf(”Answer is %d”, 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”);

4. A sequence of numbers, x1,...,xn, is generated by the following expression:

x1 = initialNumber + incrementalValue x i
where 1 ≤ i ≤ n.

a) Write an arithmeticSequence function which accepts the variables


initialNumber, incrementalValue and n, and print n sequence of numbers.

[6 marks]
4

b) Determine the output for the following function call:

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

5. A code segment in C is given as follows:

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]);

Section B [15 marks]


Answer any one question in this section.

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

a) Draw a flowchart that


• reads the weight in kilogrammes and the height in metres,
• calculates and prints the BMI,
• determines and prints the category.
[7 marks]

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;

printf("Weight in kilogram : ");


scanf("%f", &weight);
printf("Height in metre : ");
scanf("%f", &height);

bmi = weight / (height * height);


printf("BMI : %.2f\n", bmi);

if (bmi <= 18.4)


printf("Underweight\n");
else if (bmi > 18.5 && bmi <= 25.0)
printf("Normal\n");
else if (bmi > 25.0 && bmi <= 30.0)
printf("Overweight\n");
else if (bmi > 30.0 && bmi < 40.0)
printf("Obese\n");
else
printf("Exremely Obese\n");
}
7

7. The information about the number of contract employees of a supermarket for the year 2012 is as
follows:

Quarter Jan – Mar Apr – Jun Jul – Sep Oct – Dec


Number of employees 64 122 70 23

Write a program in C that declares and initialises a one-dimensional array of employee2012.


The program searches which quarter of the year has the highest number of contract employees
for that year, and display it using a switch...case statement. An example of the display is
shown below.

Year Quarter Highest number of employees

2012 Apr – Jun 122

[15 marks]

Answer

#include <stdio.h>
void main(){
int employee2012[4] = {64, 122, 70, 23};
int i, quarter, maxEmployee = 0;

printf("Year\t Quarter\t Highest number of employees\n");

for(i=0; i<4; i++)


if(employee2012[i] > maxEmployee){
maxEmployee = employee2012[i];
quarter = i;
}
printf("\n2012\t");

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

You might also like