You are on page 1of 9

2

Section A [35 marks] – STPM 2013


Answer all questions in this section.

1. The fourth generation languages are easier to use than the third generation languages.
a) State two examples of the fourth generation languages. [2 marks]
b) Describe two characteristics of the fourth generation languages. [4 marks]
c) State the similarity between the third generation languages and the fourth generation
languages. [1 mark]

Answer
a) Examples of the fourth generation languages:
1. SQL (Structured Query Language)
2. NOMAD
3. FOCUS
4. PROLOG
5. Smalltalk
6. Ada-95
7. Visual C++

b) Characteristics of the fourth generation languages:


1. A non-procedural language.
2. Enables users to access data in a database.
3. User-friendly.
4. Non-professional programmer can get the results.
5. Programs for all applications can be made with a single command fewer instructions.

c) Similarity between the third generation languages and the fourth generation languages:
1. Third generation languages and the fourth generation languages are a high-level
programming languages.
2. Third generation languages and the fourth generation languages can be translated into
machine language by interpreter.

2. The declaration statements in C are given as follows:

char a = ‘a’;
char n = ‘n’;
char r = ‘r’;
char s =‘s’;
char ch;
int v, w, x, y, z;

a) Determine the values of w, x, y and z for each of the expressions below.


i. w = n > a
ii. x = a == ‘A’
iii. y = s < ‘S’
iv. z = (r + 1) != s
[2 marks]
b) Determine a possible value of ch that will assign v=1 for the following expression:
v = (ch > r) && (ch <= s) || (ch > n) && (ch < r)
[2 marks]
3

Answer
a)
i. w=1
ii. x=0
iii. y=0
iv. z=0

Programming
#include <stdio.h>

char a = 'a';
char n = 'n';
char r = 'r';
char s ='s';
char ch;
int v, w, x, y, z;

main() {
w = n > a;
x = a == 'A';
y = s < 'S';
z = (r + 1) != s;

printf("w = %d \nx = %d \ny = %d \nz = %d", w, x, y, z);


return 0;
}

b) ch = ‘s’

3. A program in C is given as follows:

#include <stdio.h>
void main() {
int number, d;
scanf(”%d”, &number);
while (number > 0) {
d = number % 10;
printf(”%d “, d);
number = number / 10; /*A*/
}
printf(”\n”);
}

a) Determine the output of the program if number is 285. [3 marks]


b) Determine the output of the program if the statement labelled /*A* / is removed and
number is 536. [2 marks]
c) Rewrite the program in C which can read any number repeatedly. The program will end
when a negative number is entered. [5 marks]
4

Answer
a)
285
5 8 2

b)

536
6 6 6 …………

Notes : non-stop loop

Programming
#include <stdio.h>

void main() {
int number, d;
scanf("%d", &number);
while (number > 0) {
d = number % 10;
printf("%d ", d);
}
printf("\n");
}

c)
#include <stdio.h>

void main() {
int number, d;
printf("Input Number : ");
scanf("%d", &number);
while (number > 0) {
d = number % 10;
printf("%d \n", d);
number = number / 10;
printf("Enter negative number to end the program\n");
printf("Input Number: ");
scanf("%d", &number);
}
printf("\n");
}
5

4. a) What is meant by programmer-defined function, function prototype, and function call.


b) A countTotal function in C is given as follows :

int countTotal(int m)
{
int n, total=0;
for (n=m; n>0; --n)
total += n;
printf ("%d\n", total);
return total;
}
i. Write a prototype for the countTotal function. [1 mark]
ii. Write a statement to call the countTotal function from main( ). [1 mark]
iii. List the value that will be returned if the value of m are 2 and 3. State the purpose of the
countTotal function. [3 marks]

Answer
a)
Programmer-defined function
A function is a self-contained named segment of program code. This code then can be called by
its name.

Function prototype
Function prototype is a declaration of a function that provides information of a function to the C
compiler to validate any function call.

Function call
A function call is an expression that passes control and arguments (if any) to a function.

b)
i)
int countTotal(int);

ii)
void main() {
scanf("%d", &value);
countTotal(value);
}

iii)
m = 2
total = 3 (value will be returned)

m = 3
total = 6 (value will be returned)

count the total of n


6

5. a) Write an array declaration statement in C for each of the following:


i. Store the number of pages for 10 chapters in a book.
ii. Store the number of days for each month in a year.
[2 marks]

b) MyKad consists of a 12-digit identification numbering system in yymmdd-sb-###g


format. The description for the format is shown below.

Format Description
yy year of birth
mm month of birth
dd date of birth
sb state of birth
### serial number
g gender

Write a struct myKad statement in C using the information given in the above table.
[4 marks]

Answer
a)
int PAGE[10];
int DAY[12];

b)
struct MyKad {
char YearOfBirth[2];
char MonthOfBirth[2];
char DateOfBirth[2];
char StateOfBirth[2];
char SerialNumber[3];
char Gender[1];
};
7

Section B [15 marks]

Answer any one question in this section.

6. All members of the FZ Fitness Centre are required to have their weights recorded when they
register to use the facilities at the centre.

a) Write a pseudocode that calculates and displays the average weight of the members who
registered at the centre in a day. The total number of members who registered per day is
only known until the last member registers for the day. [7 marks]

b) Write a code segment in C for the pseudocode in (a) by using the following declarations:
int totalMember = 0;
int registeredMember;
float totalWeight = 0.0;
float memberWeight;
float averageWeight = 0.0;
[8 marks]

Answer
a)
1. START
2. Input registeredMember
3. i=0;
4. for (i<registeredMember)
input memberWeight
totalWeight = totalWeight + memberWeight
i=i+1
5. Loop 4.
6. totalMember = registeredMember
7. averageWeight = totalWeight/totalMember
8. print averageWeight
9. END

b)
#include <stdio.h>
void main() {
int totalMember = 0;
int registeredMember;
float totalWeight = 0.0;
float memberWeight;
float averageWeight = 0.0;
int i;

printf("Number of member : ");


scanf("%d", &registeredMember);
for(i=0; i<registeredMember; i++) {
printf("\nMember %d \n",i+1);
printf("Input Member Weight : ");
scanf("%f", &memberWeight);
8

totalWeight = totalWeight + memberWeight;


}
totalMember = registeredMember;
averageWeight = totalWeight/totalMember;
printf("\nTotal Number of member who registered : %d \n",
totalMember);
printf("Average weight of the member : %.2f", averageWeight);
}

7. A shoe store manager determines the selling price of 50 pairs of shoes based on their cost prices
and corresponding mark-up rates as shown in the following table:

Price range Mark-up rate


cost price < RM65.00 5%
RM65.00 ≤ cost price ≤ RM85.00 7%
cost price > RM85.00 10%

a) Write a define statement in C to declare a suitable constant value to store the total number
of shoes. Use this constant value to write a declaration statement to store the cost price of the
shoes in a one-dimensional array. [2 marks]

b) Write a code segment in C using a for loop to prompt the user to enter the cost price, and
reads the cost price of 50 pairs of shoes into a one-dimensional array. [2 marks]

c) Write a code segment in C with appropriate variable declaration statements to


 calculate the selling price of 50 pairs of shoes based on their cost prices and
corresponding markup rates as in the above table,
 total the cost prices and the total mark-up prices of 50 pairs of shoes,
 display the cost prices, mark-up prices, selling prices, total cost price and total markup
price.
[11 marks]

Answer
a)
#define TotalShoes 50

float CostPrice[TotalShoes];

b)
for (i=0; i<TotalShoes; i++)
scanf("%f", &CostPrice[i];
9

c)
#include <stdio.h>
#define TotalShoes 50

float CostPrice[TotalShoes], MarkUpPrice[TotalShoes],


SellingPrice[TotalShoes];
float TotalCostPrice, TotalMarkUpPrice;
int i, j;

void main() {

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


printf("Input Cost Price %d : RM", i+1);
scanf("%f", &CostPrice[i]);

if (CostPrice[i] < 65.00) {


MarkUpPrice[i] = 0.05 * CostPrice[i];
SellingPrice[i] = CostPrice[i] + MarkUpPrice[i];
}
else if ((CostPrice[i] >= 65.00) && (CostPrice[i] <= 85.00)) {
MarkUpPrice[i] = 0.07 * CostPrice[i];
SellingPrice[i] = CostPrice[i] + MarkUpPrice[i];
}
else if (CostPrice[i] > 85.00) {
MarkUpPrice[i] = 0.10 * CostPrice[i];
SellingPrice[i] = CostPrice[i] + MarkUpPrice[i];
}
TotalCostPrice = TotalCostPrice + CostPrice[i];
TotalMarkUpPrice = TotalMarkUpPrice + MarkUpPrice[i];
}

for (j=0; j<50; j++) {


printf("\nSHOES %d", j+1);
printf("\nCost Price : RM%.2f", CostPrice[j]);
printf("\nMark-Up Price : RM%.2f", MarkUpPrice[j]);
printf("\nSelling Price : RM%.2f", SellingPrice[j]);
printf("\n");
}
printf("\n\nTotal Cost Price : RM%.2f", TotalCostPrice);
printf("\n\nTotal Mark-Up Price : RM%.2f", TotalMarkUpPrice);
}
10

You might also like