Professional Documents
Culture Documents
0
1.0Introduction to Programming
01. Jelaskan definisi Program Komputer (computer program).
02. Bahasa pengaturcara komputer boleh dibahagi kepada bahasa pengaturcaraan aras tinggi dan
bahasa pengaturcaraan rendah.
03. Kompiler (compilers) dan Pentaksiran (interpreters) merupakah dua jenis penterjemahan bahasa
(language translator).
04. Apakah persaamaan di antara Bahasa mesin (machine languages)dan bahasa himpunan (assembly
language)
2. John seorang pengaturcara telah membina sebuah program C yang ringkas dengan menggunakan
perisian pengaturcara “Turbo C” dan kod aturcaranya adalah seperti dibawah.
#include<stdio.h>
void main()
{
printf(“Malaysia Maju Jaya”);
}
Jelaskan setiap fasa persekitaran pembangunan dengan mengkaitkan program C yang dibina oleh
John.
1
Basic data types
2. What are 4 basic data type specifiers? To which data type each specifier can be applied?
4. What rules apply to integer constants? How are decimal, octal, hexadecimal constants
distinguished from each other.
a. If the integer literal exceeds the range of byte, a compilation error will occur
b. We cannot implicitly convert no-literal numeric types of larger storage size to byte
c. Byte cannot be implicitly converted to float.
d. A char can be implicitly converted to only int data type
e. We can cast the integral character codes.
a. Integer(T)
b. Array
c. Single(T)
d. String
e. Long
8. Which of the following is the correct ways to set a value 3.14 in a variable pi such that it cannot
be modified?
a. Float pi = 3.14
b. #define pi 3.14
c. Const float pi = 3.14
d. Const float pi; pi = 3.14
e. Pi = 3.14
a. 0
b. 1
c. TRUE
d. FALSE
e. -1
2
10. Which of the following is an 8-byte integer?
a. Char
b. Long
c. Short
d. Byte
e. Integer
12. Write a single C statement or line that accomplishes each of the following:
a. Input three integer values from the keyboard and place these values in
integer variables a, b and c.
b. Define the variables c, thisVariable, q76354 and number to be of type int.
c. Define the variables x, y, z and result to be of type float
14. Which of the following constant are valid. State the data type for each valid constant:
a. 15 int
b. 15.0 f
c. ‘XYZ’
d. *
e. “X”
f. 32e-4
#include<stdio.h>
void main()
{
int i=777;
float x=12.345678;
float y=555.55555;
printf(“%d\n%2d\n%8d\n”,i,i,i);
printf(“%f\n%6.lf\n%.4f\n%8.3f\n”,x,x,x,x);
printf(“%f\n%6.lf\n%8.3f\n”,y,y,y,y)
}
3
16. Name the data type for each of the following constant. Explain your answer :
a. 5.38
b. “87654”
c. “True”
d. “A”
e. 05390
f. “New York”
g. -389
19. Fill in the following table with the variable name and data type needed to solve an inventory
problem for the University of Malaysia Sabah:
4
Expressions and operators
OPERATOR ARITMETIK
int a, b;
float c;
a) c = 3 + 3 - 8 * 2 / 4;
b) b = 8 / 4 + 7 * 2 - 4.5 + 7.5;
d) c = 7 * 6 % 15 / 9;
2. Di manakah antara berikut menunjukkan paras keutamaan dalam operator aritmetik dalam C.
a) / + * - b) * - / +
c) + - / * d) / * + -
3. Untuk setiap ungkapan algebra berikut, tuliskan ungkapan aritmetik C yang setara.
a) e=mc2
b) v = 2x + v2
t
c) f=gmm
d2
d) a = 1 + mv2
2
4. Untuk setiap ungkapan algebra berikut, tuliskan ungkapan aritmetik yang setara dengannya.
Kemudian tentukan nilai bagi ungkapan tersebut jika diberi b=4, c=15 dan d=2.
a) 3bc + 4b2
c+ 4b2
b) 3bc + 4b2
d
5
5. Tuliskan output bagi kenyataan C di bawah.
# include <stdio.h>
void main() {
int x = 3, y = 5, z = 7, w;
w = x % y + y % x - z % x - x % z;
printf("%d\n", w);
w = x / z + y / z + (z + y) / z;
printf("%d\n", w);
w = x / z * y / z + x * y / z;
printf("%d\n", w);
w = x % y % z + z % y % (y % x);
printf("%d\n", w);
w = z / y / y / x + z / y / (y / x);
printf("%d\n", w);
1. Andaikan i, j dan k adalah pemboleh ubah integer dengan i = 5, j = 10. Apakah nilai bagi
kenyataan yang berikut :
a) k = i++;
b) k = j++;
c) k = ++j;
d) k = ++i;
e) k = ++i + ++j;
g) k = i * j++ - --i;
h) k = 36 / j++ - 10% i;
6
2. Apakah output bagi kenyataan C di bawah.
#include <stdio.h>
void main()
{
int x = 10, y = 11, z = 12, w;
w = ++x - y++;
printf("w = %d, x = %d, y = %d\n", w, x, y);
w = ++z % - --y;
printf("w = %d, z = %d, y = %d\n", w, z, y);
w = ++y + x++ * z--;
printf("w = %d, y = %d, x = %d, Z = %d\n", w, y, x, z);
w = ++x % ++y % ++z % w--;
printf("w = %d, x = %d, y = %d, Z = %d\n", w, y, x, z);
w = ++w / ++x / y--;
printf("w = %d, x = %d, y = %d\n", w, x, y);
}
void main()
{
int w, x, y, z, keputusan;
w = x = y = z = 1;
7
OPERATOR HUBUNGAN DAN MANTIK
int x = 10;
int y = 15;
int z = 20;
b. x <= 5 || y > 15
c. (x != 5) && (y == z)
void main() {
int x, y;
double z;
x = 7;
y = 25;
z = 24.46;
8
Input and output
1. Write the output of the C program below:
main ()
{
printf(“%d\n”, 455);
printf(“%i\n”, 455); /* i same as d in printf */
printf(“%d\n”, +455);
printf(“%d\n”, -455);
printf(“%hd\n”, 32000);
printf(“%ld\n”, 2000000000);
printf(“%o\n”, 455);
printf(“%u\n”, 455);
printf(“%u\n”, -455);
printf(“%x\n”, 455);
printf(“%X\n”, 455);
return 0;
}
main ()
{
printf(“%e\n”, 1234567.89);
printf(“%e\n”, +1234567.89);
printf(“%e\n”, -1234567.89);
printf(“%E\n”, 1234567.89);
printf(“%f\n”, 1234567.89);
printf(“%g\n”, 1234567.89);
printf(“%G\n”, 1234567.89);
return 0;
}
9
Program 3 (Formatted output –String and character conversion specifiers):
main ()
{
char characterAbjad = ‘A’;
char stringRentetan[ ] = “This is a string”;
char *stringPtr = “This is also a string”;
printf(“%c\n”, characterAbjad);
printf(“%s\n”, “This is a string”);
printf(“%s\n”, stringRentetan);
printf(“%s\n”, stringPtr);
return 0;
}
main ()
{
int *ptr;
int x = 12345, y;
ptr = &x;
printf(“The value of ptr is %p\n”, ptr);
printf(“The address of x is %p\n\n”,&x);
10
Program 5 (Formatted output –Right-justifying integers in a field):
main ()
{
printf(“%4d\n”,1);
printf(“%4d\n”,12);
printf(“%4d\n”,123);
printf(“%4d\n”,1234);
printf(“%4d\n”,12345);
printf(“%4d\n”,-1);
printf(“%4d\n”,-12);
printf(“%4d\n”,-123);
printf(“%4d\n”,-1234);
printf(“%4d\n”,-12345);
return 0;
}
2. Dengan menggunakan fungsi printf().Tulis satu aturcara untuk menghasilkan output seperti
di bawah.
3. Dengan menggunakan fungsi printf().Tulis satu aturcara untuk menghasilkan output seperti
di bawah.
Nama : Siti Nurhaliza
Pekerjaan : Penyanyi
int umur=23;
Ungkapan di atas merupakan dua pembolehubah yang digunakan dalam satu aturcara C.
11
Nama saya John Kong dan saya berumur 23 tahun
a) Denganmenggunakan fungsi printf() melibatkan format output %c, paparkan huruf “T”.
b) Dengan menggunakan fungsi printf() melibatkan format output %c. Paparkan perkataan
char subjek[12];
a) Salin dan lengkapkan jadual berikut untuk tatasusun subjek dengan mengisikan indek
Tatasusun subjek
Indeks elemen
Nilai elemen
c) Tulis satu segmen kod dalam C menggunakan kenyataan “for” untuk memaparkan output
yang berikut :
Computing958
12
10. Kod di bawah menunjukkan satu segmen aturcara C.
printf(“%d %d %d\n”,a[0],a[2],a[4]);
a) printf(“%c\n”,tek[4]);
b) printf(“%c %c %c %c”,teks[0],tek[5],tek[15],tek[3]);
c) int i=8;
while(i<13)
{
printf(“%c”,tek[i]);
i=i+1;
}
int umur;
Sila Masukan …
13
3. Kod Jantina : L
Pembolehubah Nilai
NamaPemandu Nazurah Ahmad
NoKereta SAA7865G
NoKP 780912127895
noKumpulan 7
struct infoPelajar
{
char nama[30];
int umur;
char kodKelas;
float Pngk;
};
Pembolehubah Nilai
nama John Tinggang
umur 19
kodKelas A
Pngk 3.45
14
3.0Problem Solving in Programming
1. Dengan menggunakan carta alir, bina algoritma bagi masalah berikut :
Anda diminta untuk membina satu sistem yang membenarkan pelanggan memesen barang,
dengan memberikan kod item barang yang dipesan dan kuantiti yang dipesan. Sistem tersebut
kemudian akan menjana jumlah harga barang yang dipesan.
3. Bina satu carta alir yang boleh menerima input dalam sen dan menukarkannya kepada output
dalam ringgit dan sen.
5. Rasmin dtugaskan oleh pengurusnya untuk membangunkan sebuah sistem pembayaran untuk
tempat letak kereta. Bayaran untuk 1 jam pertama ialah RM2.00 dan bayaran tambahan setiapjam
berikutnya ialah RM1.00.
Dengan menggunakan perwakilan psuedo kod, bina algoritma sistem ini berpandukan kepada
penginputan masa masuk dan masa keluar ke dalam sistem.
15
4.0Control Structures
Sequential control structures
1. Berikut merupakan dua buah segiempat yang tidak sama saiz dan masing-masing telah diberi
ukuran panjang dan lebarnya.Anda diminta untuk mencari jumlah luas kedua-dua segiempat
tersebut.
40 cm
45 cm 20 cm
40 cm
a) Dengan menggunakan perwakilan carta alir atau psuedo kod, bina satu algoritma untuk
menyelesaiakan masalah di atas.
b) Tukarkan algoritma yang anda buat di (a) kepada kod sumber aturcara C.
2. Dengan menggunakan perwakilan carta alir,bina satu algoritma untuk menyelesaian masalah bagi
mencari purata untuk tiga nombor yang diinputkan.
#include<stdio.h>
void main()
{
int a ;
int b ;
int c;
c = a % b;
c=c+b;
printf(“%d”, c );
16
4. Berikut merupakan satu psuedo kod untuk mewakili satu algoritma masalah.
Mula
Baca jamKerja dan KadarSejam
Kira GajiKasar
Kira GajiBersih
Cetak GajiKasar
Cetak GajiBersih
Tamat
Subjek 1 : 78
Subjek 2 : 89
Subjek 3 : 100
Subjek 4 : 67
Subjek 5 : 56
Keputusan :
17
Selection control structures
1. Convert flowchart below to a PSEUDOCODE and a C program complete with variable
declaration.
Start
Bil = 0
Total = 0
Read X
Yes
X=0?
No
Average=Total / Bil
Bil = Bil + 1
End
Tips :
Objective of program :
The program will continue asking user to enter numbers until user press 0. Then the
program will display the average from the set of numbers entered by user.
18
2. Convert flowchart below to a PSEUDOCODE and a C program.
Start
Read X
Read Y
Read Z
No
If Y<X and Y <Z ?
Yes
Minimum = Y
No
Minimum = Z
Display Minimum
End
19
3. Change a flowchart below to a relevant PSEUDOCODE and C program
Start
Read X
Read Y
Read Z
TOTAL = X + Y + Z
AVERAGE = TOTAL / 3
no
no
yes
If Z>X and Z>Y
Biggest = Z
no
20
End
4. Convert flowchart below to a C program.
Start
Read X
Total = X
Read X
Total = Total + X
No
Total = 0
End
Tips :
Objective of Program :
The program will ask user to input 2 numbers. If the total of the two numbers more
than 100 then the value of total will be display. If total less than 100 then “Total Not
Available” will be displayed.
21
5. Convert flowchart below to a PSEUDOCODE and C program.
Start
Read X
No
No
No
22
6. Write a program to accept user input of number either 1, 2, 3, 4, 5, 6, 7.
Write a program to accept only one user order and how many that item ordered. Calculate how
much user has to pay and display on screen.
8. For each situation below create flowchart and C program to solve these problems.
a) Read Mark1, Mark2, Mark3, Mark4 and Mark5. From these marks, calculate total marks,
average marks and highest mark. Display all results.
b) Read Age. Display “Adult” if Age more than 21, display “Teenager” if age between 15 and
21, display “Kids” for age between 7 and 13, and display “Baby” for age less than 7.
c) Read code. Display “Penilaian Menengah Rendah” if code is PMR, “Sijil Pelajaran Malaysia”
if code is SPM and “Sijil Tinggi Persekolahan Malaysia” if code is STPM.
d) Write a switch statement that outputs "vowel" if a character is a vowel ('a', 'e', 'i', 'o',
'u'), "consonant" if a character is a consanant (a letter that is not a vowel), "digit" if the
character is a digit ('0', '1', '2', ..., '9'), and "other" if the character is something else.
e) Read distance and time. Calculate the speed with the formula given below. If speed more
than 100 display “Over The Speed Limit”, if speed between 60 and 100 then display
“Acceptable Speed”, for speed less than 60 than display “Very Slow”.
Speed=distance/time
Write a program to calculate Total Rental price for a specific type of house during a certain
period of month. Your program should look like this :
23
Please enter House Number : 5901
Please Enter Duration of Rental in Month : 3
Total Rental Price = RM 1500
Write a program to calculate Total fee for internet usage. Your program should look like this
:
Please enter User Type : C
Please Enter Internet Usage in Hour : 4
Total Fee = RM 12.00
11. Able below shows comments that should be written in students report cards which depends on
their average marks.
Write a program ask user to input their average mark. Then display the students grade and
equivalent comment based on the mark.
24
Repetition control structures
1. A C code segment for calculating the average marks of each student is given below.
int studentNum[SIZE];
int mark1[SIZE], mark2[SIZE], mark3[SIZE], mark4[SIZE];
int total;
float average[SIZE];
int i;
.
.
.
for (i=0; i < SIZE; i++)
{
total = mark1[i] + mark2[i] + mark3[i] + mark4[i];
average[i[ = total/ NUMMARKS;
}
(b) Write define statement to declare SIZE which has value of 20 and NUMMARKS.
ii) Combine the statements that you wrote in (i) into a program that calculates the sum of
integers from 1 to 10. Use the while structure to loop through the calculation and increment
statements. The loop should terminate when the value of x becomes 11.
3. Write a code segment in C that produces a multiplication table for a number entered by a
user. An example of the display is given below. [6]
Enter number: 8
This is a Multiplication 8
1x8=8
2 x 8 = 16
3 x 8 = 24
.
.
.
12 x 8 = 96
25
4. A for loop in C is given as follows:
for (i=10;i>=0;i-3)
printf(“%d\n”,i);
#include<stdio.h>
intmain()
{
intn;
for(n = 9; n!=0; n--)
printf("n = %d", n--);
return0;
}
Masukkan Nombor 1 : 23
Masukkan Nombor 2 : 67
Masukkan Nombor 3 : 94
Masukkan Nombor 4 : 45
Masukkan Nombor 5 : 67
Keputusan :
7. Terdapat enam orang pelajar yang tingginya masing-masing 6.1m, 5.4m, 5.9m, 5.3m, 6.0m dan
5.6m. Anda diminta untuk membangunkan sebuah program C ; yang membenarkan pengguna
memasukkan semua tinggi pelajar tersebut dan kemudian program tersebut boleh menjana
jumlah tinggi semua pelajar tersebut. Contoh hasil output adalah seperti berikut :
Pelajar 1 : 6.1
26
Pelajar 2 : 5.4
Pelajar 3 : 5.9
Pelajar 4 : 5.3
Pelajar 5 : 6.0
Pelajar 6 : 5.6
Barang 1 : RM 56.70
Barang 2 : RM 77.30
Barang 3 : RM 34.80
Barang 4 : RM 59.50
9. Berikut merupakan contoh output bagi satu program C yang ingin dibangunkan oleh Penolong
Kanan Pra-U sekolah anda.
Kelas 1 : 40
Kelas 2 : 34
Kelas 3 : 23
Kelas 4 : 38
Bina satu program C, yang membolehkan pengguna memasukkan markah pelajar dan kemudian
sistem tersebut mampu mengira jumlah markah dan purata markah bagi 6 orang pelajar tersebut.
Contoh hasil output adalah seperti berikut :
Masukkan markah :
27
Pelajar 1 : 34.5
Pelajar 2 : 76.3
Pelajar 3 : 89.3
: : :
Pelajar 6 : 89.2
12. Bina satu program C untuk menghasilkan sifir darab; dimana program ini membenarkan
pengguna memasukkan sifir darab yang diingini. Contoh hasil output adalah seperti berikut:
Sifir Darab 5 :
5 x 1 = 5
5 x 2 = 10
: : : : :
: : : : :
5 x 9 = 45
13. Bina satu program C yang membenarkan pengguna memilih barangan yang hendak dibeli.
Kemudian program tersebut dapat mengira jumlah yang perlu dibayar oleh pengguna. Contoh
paparan output program tersebut adalah seperti berikut :
MENU PILIHAN
1. Baju RM 23.50
2. Kasut RM 45.80
3. Beg RM 76.30
4. Topi RM 7.80
28
Masukkan berapa bilangan barang yang hendak dibeli : 3
14. Bina satu program C untuk mengira jumlah purata markah subjek-subjek yang diambil oleh
pelajar. Program tersebut membenar pelajar memasukkan bilangan subjek dan markah setiap
subjek yang diambil. Contoh hasil output program tersebut adalah seperti berikut :
15. Anda diminta untuk membangunkan satu aturcara C; yang membenarkan pengguna untuk
memasukkan nama dan markah-markah subjek yang diambil. Kemudian program tersebut
mampu mengira purata markah yang diperoleh dan juga dapat memberi pengredan kepada
terhadap pencapaian keseluruhan mereka. Berikut merupakan pengredan berdasarkan purata
pengguna.
Masukkan markah :
Subjek 1 : 67
Subjek 2 : 89
Subjek 3 : 59
Subjek 4 : 99
29
=======================================
KEPUTUSAN :
=======================================
16. Sempena ulang tahun Syarikat senQ yang ke 100. Syarikat tersebut telah menawarkan
pemberian hadiah berdasarkan jumlah pembelian pengguna. Berikut merupakan jadual
pemberian hadiah berdasarkan jumlah pembelian :
Masukkan Harga :
Barang 1 : xxx.xx
Barang 2 : xxx.xx
Barang 3 : xxx.xx
Barang 4 : xxx.xx
Barabg 5 : xxx.xx
Keputusan :
========================================
30
5.0Functions
#include<stdio.h> //baris1
//baris2
void menuUtama(); //baris3
//baris4
void main() //baris5
{ int pilihan; //baris6
//baris7
menuUtama(); //baris8
a) Kenalpasti prototaip fungsi, panggilan fungsi dan takrifan fungsi untuk aturcara di atas.
b) Tuliskan hasil output untuk kod aturcara baris yang ke 8.
31
7. Berikut merupakan satu kod pengturcara.
#include<stdio.h> //baris1
//baris2
int baki(int,int); //baris3
//baris4
void main() //baris5
{ int a=13; //baris6
int b=3; //baris7
int c=23; //baris8
int d=7; //baris9
int x, y; //baris10
//baris11
x=baki(a,b); //baris12
x=x+7; ; //baris13
y=baki(c,d); //baris14
y=y+18; //baris15
y=y+x; //baris16
} //baris17
//baris18
int baki(int a,int b) //baris19
{ //baris20
return a % b; //baris21
} //baris22
a) Kenalpasti prototaip fungsi, panggilan fungsi dan takrifan fungsi untuk aturcara di atas.
b) Apakah nilai x pada baris yang ke 12.
c) Apakah nilai x pada baris yang ke 13.
d) Apakah nilai y pada baris yang ke 14.
e) Apakah nilai y pada baris yang ke 15.
f) Apakah nilai y pada baris yang ke 16.
32
8. Kaji carta alir di bawah yang terdiri fungsi utama dan sub fungsi.
Input a
baki (a , b )
Catak baki
Tamat
b) Tukarkan carta alir di atas kepada segmen kod bahasa C yang melibatkan fungsi uatama dan
sub fungsi.
a) Tulis segmen kod C untuk fungsi ”luasSegitiga” yang akan mengembalikan nilai luas ke fungsi
utama .
b) Tulis segmen kod dalam C untuk fungsi utama yang boleh membaca tinggi dan tapak sebuah
segitiga dan mencetak hasil luas segitiga.
a) Tulis segmen kod dalam C untuk fungsi ”terbesar” yang mengembalikan nilai terbesar antara
integer a, b dan c.
33
b) Kod segmen dalam C yang menggunakan fungsi ”terbesar” diberi seperti berikut :
int p, q;
p=terbesar(m, n,s);
ii. Tentukan nilai untuk parameter a, b dan c bila fungsi ”terbesar” dipanggil dalam baris
yang berlabel /* 1 */
c) Tulis segmen kod C untuk fungsi ”terkecil” yang akan mengembalikan nilai terkecil antara
integer a, b dan c.
d) Tulis segmen kod dalam C untuk ”int main()” yang boleh membaca nilai a, b dan c dan
mencetak hasil terkecil.
34
a) Tuliskan prototaip fungsi untuk fX.
b) Senaraikan semua nilai yang dihantar ke pembolehubah m dan n untuk fungsi fX dalam baris
ke 9.
c) Cetak output untuk baris ke 5 dan ke 6.
d) Berikan satu nama yang sesuai untuk fungsi fX.
e) Gantikan ”MessageA” dalam baris ke 5 dengan teks yang sesuai.
a) Write the definition of caribaki function that performs the division of two integers and
returns the remainder to the caller.
b) Write a main() function that reads two integer values, calls the caribaki function and prints
the remainder returned by caribaki function.
main() //line 1
{ //line 2
int a,b,c; //line 3
printf("Enter first number : \n"); //line 4
scanf(“%d”, &a); //line 5
printf("Enter second number : \n"); //line 6
scanf(“%d”, &b); //line 7
c=kira(a,b); //line 8
printf("\n%d”, c); //line 9
35
} //line 10
//line 11
kira(int x, int y) //line 12
{ //line 13
int m, sum=0; //line 14
for (m=0 ; m<x ; m++) //line 15
sum *= y; //line 16
return sum; //line 17
} //line 18
36
a) Write a code segment in C for the function largest that returns the largest values among the
integers a,b and c.
Int p, q; //line 1
int m = 17, n = 65, s = 41; //line 2
//line 3
p = largest(m, n, s); //line 4
q = p - largest(n-s, s-m, m); //line 5
1. Table below shows Computing Marks for Comel’s student in Form 6A3. Write a C code to display
all the marks.
Array Name = Marks
45 78 65 88 94 77
55 66 79 67 66 87
43 44 64 78 47 85
67 87 99 80 67 26
2. Table below shows total Apples collected in a week. Write a C code to find :
the total of apples collected for the week
mean/average of apples collected for the week
Display the result.
3. Table below shows total of cars sold in a week. Write a C code to calculate and display the total
of cars sold in that week and mean/average.
37
MON TUE WED THUR FRI SAT SUN
4. Table below shows total of registered DiGi customer in year. Write a C code to
calculate and display the highest number of
registered customer
display which month of the highest number
JAN FEB MAC APR MAY JUN JULY OGO SEPT OCT NOV DEC
5. Table below shows number of oranges sold in eight district in Sarawak. Write a C code to find
total of oranges sold.
The Lowest oranges sold from district
(Give ORANGES as your array name)
6. Write a C code to ask user to enter the temperature of his pet for each day in a week.
(Give TEMPET as your array name).
Then find the average temperature of his pet.
7. Shown below are two tables named ; FIRSTBL and SECONDTBL. Write a C code to copy all data
38
SECOND -TABLE
8. Shown below are names of students who refuse to come to Computing Xtra Class.
Write a C code to display their name.
(Give GENIUS as your array name).
A L I C I A
M E I A N N
S O N N I E
B E R T H A
C L A A R A
Di atas menunjukkan satu urutan nombor, isytiharkan satu pembolehubah untuk mengumpukan
nilai urutan nombor di atas secara tatasusun.
int b=0;
b +=a[i];
printf(”%d\n”,b);
39
a) Paparkan nombor 67.
b) Paparkan nombor 25.
c) Paparkan nombor 45,67,89 dan 12 secara urutan dengan menggunakan kenyataan “for”.
char b[7];
char jantina[10];
Buat satu aturcara yang boleh mengira jumlah lelaki dan jumlah perempuan berdasarkan output
berikut:
Jumlah Pelajar = 10
40
Jumlah Pelajar Perempuan=4
Structures
1. A C code segment for calculating the average mark of each student is given below. (2003)
2. The Basri Motor Company Pte Ltd sells used cars which comprise different makes, models, year
of manufacture and prices. Write a structured declaration in C language that can store
information for 100 used cars of the company. Assume that the makes and models of the cars do
not exceed 25 characters (2004)
41
3. A bookshop manager updates information about the books in his shop. The information stored
consists of the titles, quantities and prices of books. The books are arranged according to titles
to facilitate the calculation of the value of the assets. The value. The value of the assets is
calculated by summing the products of the quantities and the prices of books. (2006)
A constant value of LENGTH is the field size to store the title of a book and STOCK is the number
of different titles in the shop. The title, quantity and price of a book are stored in a data
structure named struct bookStock.
a. Write define statements to declare LENGTH which has a value of 20 and STOCK which has a
value of 100
b. Write a struct bookStock statement which contains the title, quantity and price of a book
c. Write a statement to declare book of the type struct bookStock
d. Write a code segment in C which reads the title, quantity and price of a book. Calculate and
print the total asset for all the book titles in the shop.
4. a. Write declarations statements in C for each of the variables according to the descriptions
given in the table below (2011)
Variable Description
Format Description
yy year of birth
mm month of birth
dd date of birth
sb state of birth
g gender
42
Write a struct myKad statement in C using the information given in the above table.
7. Dibawah ini menunjukkan beberapa pengisytiharan pembolehubah yang berkaitan dengan data
pelajar.
char nama[20];
char kelas[15];
int umur;
char noTelefon[12];
b) Tulis satu pernyataan untuk mengisytiharkan ”pelajar” dalam jenis ”struct profil”.
8. Guru Pusat Sumber ingin mengemaskini maklumat ahli-ahlinya. Maklumat yang perlu disimpan
adalah terdiri daripada no ID, nama ahli, umur, no kad Pengenalan dan kelas.Maklumat-
maklumat tersebut akan disimpan dalam struktur data yang bernama ”struct dataAhli”.
a) Tuliskan satu pernyataan ”struct dataAhli” yang mengandungi no ID, nama ahli, umur, no kad
Pengenalan dan kelas.
c) Berdasarkan ”struct dataAhli ”yang anda buat di (a). Umpukan nilai awalan berikut kepada
strrucktur tersebut.
struct markah
int BM;
int BI;
43
int ICT
};
10. Berikut merupakan data input yang akan dimasukkan kepada struktur data structdataPeribadi.
c) Isytiharan satu pembolehubah pemandudalam jenis struct dataPeribadi yang boleh menerima
tiga data Peribadi pemandu.
d) Andaikan semua nilai awalan data peribadi pemandu telah diumpukan ke dalam struktur data
dengan menggunakan struct dataPeribadi pemandu2[3].
44
NoKP : 700912156477
Umur : 23
No Kereta : SAA4578
Model Kereta : Kancil
………………………………..
…………………………………
………………………………..
Nama : Ah Lim
NoKP : 700131127856
Umur : 23
No Kereta : SAA6902
Model Kereta : Kancil
45