STPM 2010 - ICT: Answer

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

1

STPM 2010 – ICT

1. Describe the evolution of programming languages from the first generation to the fifth
(1) generation. [5 marks]

Answer
First Generation
- Used low-level language for computer programming language; machine language.
- Computers had to be programmed in machine language; is the only language the computer
directly recognizes.
- Machine language instructions use a series of binary digits (0 and 1); or a combination of
numbers and letters that represents binary digits.
- Machine-dependent; programmers need to know exactly how the computer works.

Second Generation
- Used low-level language for computer programming language; assembly language (one that
not far removed from machine language).
- Computers had to be programmed in assembly language; closely resembles machine
language.
- Coded in assembly language using symbolic instruction code; is an abbreviation code that
gives specific meaning.
- The assembler is used to convert assembly language programs to machine language before
the computer can execute.
- Programmer still needs to know exactly how the computer works.
- Assembly language is a little easier than machine language.

Third Generation
- Used high-level languages for computer programming language.
- Third-generation language (3GL) is a procedural language; the programmer writes
instructions that tell the computer what to accomplish and how to do it.
- Programmers need to understand the intimate details of how the computer processes data.
- Instructions written using a series English-likes word.
- Compiler or interpreter is used to convert the source program into machine language before
the computer can execute.
- 3GL are much easier to read, write and maintain than Assembly language.
- Examples : BASIC, FORTRAN, COBOL, PASCAL, C, C++.

Fourth Generation
- Used very high-level languages for computer programming languages.
- Fourth-generation language (4GL) is a nonprocedural language; getting away from
procedure that enables users and programmers to access data in a database.
- The programmer writes English-like instructions or interacts with a graphical environment
to retrieve data from files or a database.
- Nonprocedural languages typically are easier to use than procedural languages.
- Example : SQL (Structured Query Language)

Fifth Generation
- Used very high-level languages for computer programming languages.
- Used an object-oriented programming (OOP) language to implement an object-oriented
design.
- A major benefit of OOP is the ability to reuse and modify existing objects.
- Programmers do not have to write code for diagram on windows form because they already
exist in the programming language or tools provided with the language.
- Example : Java, C#, Visual Studio, PowerBuider, Visual Programming Languages.
2

Generasi pertama
 Menggunakan bahasa aras rendah sebagai bahasa pengaturcaraan komputer iaitu bahasa
mesin.
 Komputer diprogramkan dalam bahasa mesin; adalah merupakan satu-satunya bahasa
berasaskan komputer secara terus.
 Arahan dalam bahasa mesin menggunakan satu siri digit perduaan (0 dan 1); atau gabungan
huruf dan angka yang mewakili nilai perduaan.
 Bahasa mesin bergantung kepada mesin komputer; pengaturcara perlu mengetahui tentang
bagaimana komputer bekerja.

Generasi Kedua
 Menggunakan bahasa aras rendah sebagai bahasa pengaturcaraan komputer iaitu bahasa
himpunan(yang tidak jauh bezanya dari bahasa mesin).
 Komputer telah diprogramkan ke dalam bahasa himpunan hampir menyerupai bahasa
mesin.
 Bahasa himpunan dikodkan menggunakan kod arahan yang simbolik; adalah merupakan
singkatan kod yang memberi makna tertentu.
 Penghimpun digunakan untuk menukar atur cara bahasa himpunan kepada bahasa mesin
sebelum komputer boleh melaksanakan sesuatu arahan.
 Pengatucara masih lagi perlu untuk mengetahui dengan tepat bagaimana komputer bekerja.
 Bahasa himpunan lebih mudah sedikit daripada bahasa mesin.

Generasi Ketiga
 Menggunakan bahasa aras tinggi sebagai bahasa pengaturcaraan komputer.
 Bahasa generasi ketiga (3GL) adalah merupakan bahasa prosedur; pengaturcara menulis
arahan untuk memberitahu komputer apa yang perlu dicapai dan bagaimana ianya hendak
dilaksanakan.
 Pengaturcara perlu memahami secara jelas bagaimana komputer memproses data.
 Arahan dikodkan menggunakan bahasa mirip Bahasa Inggeris.
 Pengkompil atau pentafsir digunakan untuk menukar atur cara sumber kepada bahasa
mesin sebelum komputer boleh melaksanakan sesuatu arahan.
 3GL lebih mudah untuk dibaca, ditulis dan diselenggara berbanding dengan bahasa
himpunan. Contoh: BASIC, FORTRAN, COBOL, Pascal, C, C + +.

Generasi Keempat
 Menggunakan bahasa aras sangat tinggi sebagai bahasa pengaturcaraan komputer.
 Bahasa generasi keempat (4GL) adalah bahasa tanpa prosedur; dimana ianya
membolehkan pengguna dan pengaturcara mengakses data dari pangkalan data.
 Pengaturcara menulis arahan mirip Bahasa Inggeris atau berinteraksi dengan persekitaran
grafik untuk mendapatkan data dari fail atau pangkalan data.
 Bahasa tanpa prosedur adalah lebih mudah untuk digunakan berbanding bahasa prosedur.
 Contoh: SQL (Structured Query Language)

Generasi Kelima
 Menggunakan bahasa aras sangat tinggi sebagai bahasa pengaturcaraan komputer.
 Menggunakan bahasa pengaturcaraan berorientasikan objek (OOP); bahasa untuk
melaksanakan reka bentuk berorientasikan objek.
 Kelebihan utama OOP adalah keupayaan untuk menggunakan semula dan mengubah suai
objek yang sedia ada.
 Pengaturcara tidak perlu mengkodkan gambar rajah pada paparan tetingkap kerana kodnya
telah sedia ada dalam bahasa pengaturcaraan atau alatan yang dibekalkan bersama-sama
bahasa pengaturcaraan tersebut.
 Contoh: Java, C#, Visual Studio, PowerBuider, Bahasa Pengaturcaan Visual.
3

2. A for loop in C is given as follows:


(4)
for (i = 10; i >= 0; i -= 2)
printf (“%d\n”, i);

a) Give the output for the above for loop [3 marks]


b) Rewrite the above for loop to its equivalent do…while loop. [3 marks]

Answer

#include <stdio.h>

void main()
{
int i;

for(i = 10; i >= 0; i -= 2)


printf("%d\n", i);

a)

10
8
6
4
2
0

b)
i=10;
do {
printf("%d\n", i);
i -= 2;
}
while (i >= 0);
4

3. A family plans to go to a theme park during school holidays. The family members consist of a
(5) father, a mother, three girls aged 2, 5 and 20, and three boys aged 10, 13 and 15. The ticket
prices to the theme park are as follows:

Age range Ticket price


Age ≥ 18 RM32.00
12 ≤ Age < 18 RM28.00
3 < Age < 12 RM20.00
Age ≤ 3 Free

Draw a flowchart that


a) reads the number of persons in the family.
b) reads the age and determines the ticket price of each person.
c) calculates and prints the total ticket price for the family.
[7 marks]
Answer
#include <stdio.h>

void main(){

int number, i, Age, Age1, Age2, Age3, Age4, Price1,


Price2, Price3, Price4;
float TicketPrice;

Age1 = Age2 = Age3 = Age4 = 0;

printf("Input the number of persons in the family : ");


scanf("%d", &number);

for (i=1; i<=number; i++) {


printf("Input age for persons %d : ", i);
scanf("%d", &Age);

if (Age >= 18)


Age1++;
else if ((Age >= 12) && (Age < 18))
Age2++;
else if ((Age > 3) && (Age < 12))
Age3++;
else if (Age <= 3)
Age4++;
}
Price1 = Age1 * 32;
Price2 = Age2 * 28;
Price3 = Age3 * 20;
Price4 = Age4 * 0;

TicketPrice = Price1 + Price2 + Price3 + Price4;


printf("\nTotal Ticket Price : RM%.2f", TicketPrice);
}
5

4. An expression is given as follow :


(6)
e * 5 + p * 3 + g * 2 >= 120 || e > p + g

Show the operations and results for each of the following C declarations :
a) int e = 5, p = 10, g = 12; [4 marks]
b) int e = 10, p = 5, g = 2; [4 marks]

Answer
a) e * 5 + p * 3 + g * 2 >= 120 || e > p + g
5 * 5 + 10 * 3 + 12 * 2 >= 120 || 5 > 10 + 12
25 + 30 + 24 >= 120 || 5 > 22
79 >= 120 || 5 > 22
0 || 0
0

b) e * 5 + p * 3 + g * 2 >= 120 || e > p + g


10 * 5 + 10 * 3 + 2 * 2 >= 120 || 10 > 5 + 2
50 + 30 + 4 >= 120 || 10 > 7
84 >= 120 || 10 > 7
0 || 1
1

5 The four elements in array S are as follows:


(7)
55 35 64 17

Sort the elements in ascending order using bubble sort method. Show the elements of array S
after each pass. [5 marks]

Answer
S
55 35 64 17

[0] [1] [2] [3]


S
35 55 17 64

[0] [1] [2] [3]


S
35 17 55 64

[0] [1] [2] [3]


S
17 35 55 64

[0] [1] [2] [3]


6

6 A form six student took five subjects for his STPM examination. The marks he obtained for the
(9) five subjects are 86, 75, 79, 75 and 89.

a) Write a statement in C to declare and initialise the five marks using a one-dimensional
integer array of size five.
b) Write another code segment in C to declare elements in the one-dimensional integer array
marks and read the five marks using for loop.
c) Grades given for the marks are as follows:

Mark range Grade


Marks ≥ 75 A
65 ≤ Marks < 75 B
50 < Marks < 65 C
Marks ≤ 50 D

Write a code segment in C to


i. determine the grade for each subject. [4 marks]
ii. print the marks and grade for each subject. [1 marks]

Answer
a)
int Marks[5] = {86,75,79,75,89};

b)
int Marks[5];
for (j=0; j < 5; j++)
scanf("%d", &Marks[j]);

c)
for (j=0; j < 5; j++) {
printf("\nsubject %d : %d", j+1, Marks[j]);

if (Marks[j] >= 75)


Grade = 'A';
else if ((Marks[j] >= 65) && (Marks[j] < 75))
Grade = 'B';
else if ((Marks[j] > 50) && (Marks[j] < 65))
Grade = 'C';
else if (Marks[j] <= 50)
Grade = 'D';
printf("\nGrade %c : ", Grade);

}
7

C Programming
#include <stdio.h>

void main()
{

int i, j, Marks[5];
char Grade;

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


printf("Input marks for subject %d : ", i+1);
scanf("%d", &Marks[i]);
}

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


printf("\n\nMarks for subject %d : %d", j, Marks[j]);

if (Marks[j] >= 75)


Grade = 'A';
else if ((Marks[j] >= 65) && (Marks[j] < 75))
Grade = 'B';
else if ((Marks[j] > 50) && (Marks[j] < 65))
Grade = 'C';
else if (Marks[j] <= 50)
Grade = 'D';
printf("\nGrade %c : ", Grade);

}
}