Sei sulla pagina 1di 31

KOLEJ UNIVERSITI TEKNIKAL KEBANGSAAN MALAYSIA PEPERIKSAAN SEMESTER I

SESI 2003/2004

FAKULTI TEKNOLOGI MAKLUMAT & KOMUNIKASI

KOD MATAPELAJARAN

BITG 1113

PENGATURCARAAN KOMPUTER

MATAPELAJARAN

PENYELARAS

PN ZARIT A MOHD. KOSNIN

KURSUS

BEKCI BEKEI BEKP/BENC/BENEI

BENT/BMCAI BMCD/BMCSI BMCT

SEKSYEN

1&2

MASA

2 %JAM

TARIKH

13 Oktober 2003

Arahan kepada calon

1. Kertas soalan ini mengandungi TIGA bahagian iaitu

• Bahagian A (30 saalan abjektif).

• Bahagian B (4 saalan struktur)

• Bahagian C (1 soalan esei)

2. Sila jawab SEMUA saalan. Jawapan bahagian A, hendaklah ditanda pada barang jawapan yang disediakan. Jawapan bahagian B dan C, hendaklah ditulis di buku jawapan yang disediakan.

KERTAS SOALAN INI TERDIRI DARIPADA (31) MUKA SURAT SAHAJA (TERMASUK MUKA SURAT HADAPAN)

23'86·· IJ

I

(BITG 1113)

PART A: (45 Marks)

Instruction : Please write your answers in the answer form provided

..

1. Which of the following are looping structure?

A. do ... while and if

B. for and repeat

C. do ... while and if .. else

D. for and do .. while

Questions number 2 & 3 are based on the program code below.

cout « "GELUNG"i

int I I J I M=6i

for ( I = 0 i I < 10 i I += 4 )

for ( J = 10 i J >0 i J - = 4 )

M=M+li

2.

How many time will the print command be executed?

A. 16

B. 100 C.40 D.9

3.

What is the final value for M ?

A.7

B. 15

C. 16

D. 9

- 2 -

I

sum = sum + numi

(BITG 1113)

4.

Which of the following statements about the do ... while loop is true?

A. The limit test in a do ... while loop is made before each iteration .

. B. The do ... while loop is the same as the while loop, except that the relational condition is placed at the, top of the loop. ..

C. The update in a do ... while statement is contained in the while statement expression itself.

D. The do ... while statement is a posttest loop.

5. Suppose that the input is 38 45 71 4 1. What is the output of the following code? Assume all variables are properly declared.

sum = numi

cin»num;

wh~le(num <= 40)

cin»num;

,,-"_J

}

i-·,I

cout«"Sum = "«sum«endl;

A. Sum = 38

B. Sum=43

C. Sum = 121

D. Sum = 116

~ ..

1# .::

7.

(BITG 1113) Choose a CORRECT statement about function.

A. A function that receives parameters that are passed by value actually receives a copy of the actual parameter values, and any changes made to the copied value in the function will affect the original value in the program segment that caned the function.

B. To call a function that receives parameters that are passed by reference using pointers, each actual parameter for pointer variable starts with the symbol *.

C. Parameters that are sent to a cal1edfunction by.using pass by reference are variable address.

D. Function definition is known as the function prototype.



8.

Which of the following statements about main is FALSE?

A. One and only one function in a program may be called main.

B. The execution of a C program always begins with main.

C. The function main is caned by the computer's operating system.

D. Every function, other than main, must be caned by main and must return to main.

9.

The operator used to specify pass-by-reference is:

A. Dereference operator (#)

B. Pointer operator (A)

C. Address operator (&)

D. Array operator ([ ])

10.

Which is NOT a file opening mode? A. ios::in

B. ios::out
C. ios::add
D. ios::ate i ¢'a"Q;9

' ~ •• ~. < -t, ','

,J ~ '.

w· ._", ." .,."

I

11 Given the declaration

(BITG 1113)

·IL c_ha_r s_t_r_in_g_l_S_[_1_6_]_i ~

to

Which of the following statements is INVALID?

A. strcpy(stringl5, "Hello there");

B. strlen(string1S);

C. string1S == "Jacksonville";

D. cin»stringlS;

12. The C++ library that contains the prototype statements for file operations is:

A. fproto.h

B. fstream.h

C. stdio.h

D. stdlib.h

13.

Which statement correctly opens an input stream named in_file and attaches it to a file name project.txt?

A. in_file=project.txt

B. infile=rproject.txt"

C. in _ file.opent'tproject, txt");

D. in _ file.open{project. txt);

14.

The function that makes a stream ready for reading or writing.

A. file

B. close

C. open

D. stream

(BITG 1113)

15. The character input function that reads one character at a time from a text stream is:

A. put ()

B. read ()

C. char ()

D. get ()

..

16. Please choose a CORRECT statement for the following declaration:

in t komi syen [2] [3] = { {9 5 I 80 I 78 } I {7 0 , 90 I 100 } } i

1. Array komisyen consists of 6 elements.

n. The value for line size and column size can be ignored, if the initial

.

value is not given.

iii. Sub braces can be ignored.

iv. The suitable size for the array above is komisyen[3][2]

A. i and ii
B. i and iii
C. i.ii and iii
D. i, iii and iv 17.

For the following array declaration,

double marks [2] [3);

the last element can be referred as:

A. marks [1] [2] i

B. marks [2] [3] ;

C. marks [3] [2] i

D. marks [2] [1] i

.~

239'1

double jumlah, bayar[3] [2]i jumlah= Kira (bayar [2] [0] ) t,

..

(BITG 1113)

18. Given the following statements:

Choose a suitable statement for a function head that suits the function call.

A. double Kira(doublebil[][] double bayar[][])

B. double Kira(double bi1[])

C. double Kira( double bil)

D. double Kira(double bayar[][])

.

19. Which of the following statements initializes an array of four elements to the

first four odd numbers?

A. int oddsAry[] = {I, 3. 5, 7};

B. int oddsAry[ ] = [1.3.5. 7];

C. int oddsAry[4] = [I. 3.5. 7];

D. int oddsAry[5] = [1,3, 5, 7];

20. Assuming that the variables are properly declared, which of the following . statements reads five integers from the keyboard into an array?

A. for (I = 1; 1 <= 5; 1++) cin > ary[I++];

B. for (I = 0; 1 < 4; 1++) cin > ary[I++];

C. for (I = 0; 1 < 5; 1++) cin > ary[I++];

D. for (I = 1; 1 < 5; 1++) cin > ary[I++];

-7-

I, ~

(BITG 1113)

21. Based on the following program segment, what is the value of pl , p2 and p3?

..

int pl,p2,p3;

int *ptl, *pt2, *pt3j

pl=100i p2=200i p3=300j ptl=&P1i pt2=&p2j pt3=pt2j

*pt3=400j

p2=*pt3i

pt2=&p3i

A. 100,200,300

B. 100, 400, 300

C. 100,200,200

D. 100, 400, 400

22.

What is the value of *ip3 in the following program segment?

A. 60

B. 80 C.90 D. 100

int nombor[]= {100,90,80,70,60}i int *ipl, *ip2, *ip3i ipl=nomborj

ip2=ipl+1i

ip3=ipl+2j

ip2::ip3+1i

ip3=ip2+1j

ip3=ip2-3j

2'~, ,: '9':3-' '

-'U'" .: ':',r

I

II)

o

~ ...

(BITG 1113)

23. Given the following definitions, what is the value of a after the code has been

executed?
int a = 5, bi
int *pl = &ai
int *p2i
b *pl * 2;
*p2 = &bi
a = a * b + *p2 - *p1i A.75

B. 45

C. 55 D.70

,.

24. Given the definitions shown below, which answer is a compiler error?

int ii

float fj

int *pdj

float *pfi

A. pd ::: pf;

B. f ::: 5;

C. pd ::: &i;

D. pf = &f;

'0

'.'

-9-

;.~ .. : :

(BITG 1113)
25. What is printed from the following block of code?
int x = 3;
int y = 2;
int z = 1; ..
int *p = &x;
int *q = &Yi
int *r = &Zi 26.

*r = *q * *Pi *p *q * *ri

cout « *p « I I « *q « I I « *r « endl;

A. 3 2 1

B. 2 2 1

-

C. 12 2 6

D. 11 91 41

Consider the following declaration. Choose a statement that sends a value of a struct member to the called function; which actually sends an address.

struct pekerja

char nama [20] ;

float jualan bulanan[12]; float komisyen;

float bonus;

}p1i

A. Kira jualan(p l.jualan_ bulanan);

B. Isi_maklumat(pl);

C. Kira_bonus(&pl.bonus);

D. Kira_komisyen(pl.komisyen);

- 10-

I

, ,

(BITG 1113)

27. Given a structure variable named stu containing an array named exams, which

of the following answers refers to the third exam for a student?

A. stu.exams[2]

B. stu [2].exams[2]

..

C. stu[2]->exams

D. stu->exams[2]

28. Which of the following answers about structures (struct) is FALSE?

A. Structure definitions may include initialization values.

B. The fields in a structure must be of the same type.

C. A structure can be viewed as atemplate for the individual parts.

D. All fields in a structure must be related.

29.

Given a pointer named ptr, to a structure STU containing a field name, which of the following answers correctly references name?

A. ptr.name

B. ptr->name

C. ptr.STU.name

D. ptr->STU->name

30. Given the structure shown below, which answer is not valid?

struct STU

{

char name [26] ; int midterm [3] i

int final;

}; ~.-

STU class[40]; STU *p = class;

A. class[iJ.name[ 1]

B. class[i].midterm[2]

C. *p.name

D. p->name

(BITG 1113)

PART B: (40 Marks)

Question 1 (1 0 marks)

..

a) What is printed from the following block of code? for (x = 1; x < 10; x++)

(4 marks)

if x % 5 != 0)

cout « X « I I;

else

break;

b) The for loop below has been changed to do .•. while loop. Fill in the blanks in the

-

output to complete the do ••. while loop.

for (x 10; x>= 1; x- -)
{
for (y = x-1; y>=l; y--)
cout « " II •
I
cout « x « endl; (6 marks)

- 12-

(BITG 1113)

Output:

x = __

do

..

y = ----

do

cout « " "j

} while

_______ );

cout « X « endl;

} while ( ) i

Question 2 ( 10 marks)

a) Define input and output files named "fslfataln" and "fsDataOut" . Code the statement to open both files. The name of the external input and output files are

"data.txt," and 11 data. out. 11

(4 marks)

b) Code the statement to check the fpDataln file above successful open. (6 Marks)

- 13 -

I

~'

.

.

~~ I

(BITG 1113)

Question 3 (1 0 marks)

By using tables/diagrams, write the value change of each variable if the program

segment is executed, and state what are the printed values.

..

#include <iostream.h>

void tukar_l(int *, int *)i void tukar_2(int x, int *};

void main ()

{

int a=3, b=8, c=7j tukar_l (&a, &b) j cout«a«b«c«endli tukar_2 (b, &c); cout«?«b«Ci

}

void tukar_l(int *x, int *y)

{

int tempi

(2 marks)

(BITG 1113)

.,

;

Question 4 (10 marks)

a) Given the following array, show what the following code fragment prints.

.. (6 marks)

int aryl ] = {1. 2, 4, 5, 8, 9, 16, 17, 32, 33};

for (I = 1; I < 10; 1++ ) if (ary[1] % I 1= 0)

cout « ary[I] « " ";

b) Given the following type definition and variable, code a reference to each element in the variable.

char

{

struct ADDRESS

struct NAME

{

first [16] ;

char

last [31] i

} name; char street [31] i char city[26];

} i

ADDRESS addri

(c) Code a reference to each element in (a) if the last line of code, ADDRESS addri

is changed to

ADDRESS *Pi

(2 marks)

- 16-

(BITG 1113)

PART C: (15 Marks)

Question ( 15 marks)

You are required to create a program to calculate the total cost of ordering pizza. Below are the requirements you have to follow:

(a) Use the variable total Cost, pizzaNum, PricePerTopping and PlainPrice in your program.

(b) Use the main function to:

• Get the number of pizza

• Call the costOfPizzaO and get the return value from it

• Print the respond of number of pizza, total cost, and "Thank you!",

(c) Use the costOfpizza function to:

• Get the number of toppings

• Count the total cost.

• Return the total cost to maim)

(d) Set the variable PricePerTopping and PlainPrice as constant value in costOfPizzaO. RM2 will be charged to every topping while plain price per pizza costs RM 1 O.

(e) Create elements NumberOffoppings and Cost in structure "PIZZA" and use the reference myPizza to store the values in these variables.

(f) Use for loop to get the total cost of all pizzas ordered.

Output example is as follow:

-K4>i.ci{ *<ll'del'ing P i Z 2: a *"***"*-",*,"*,*,** .. M\
Ho\-! f'lany pizza do you Hant to Ol'4Iet'?
J
[10\'/ nany topping;; do you uant for pizza n umbe i- i ?
1
HO\-l nany topping;; do you \'Ji'lnt fa!' pizza number- 2 ")
2
flo\-! nan!) topping" do 'JOlt Hant Fo r- pizza !'tunbel' 3 ?
3
':lOll have o r-de r-e d 3 pizza(;;).
The total cost is: R1'142. Thank you!
Pi-e s s any IWij to continae Write the complete program for the above problem

I I'~

(BITG 1113)

BAHAGIAN A: (45 Markah)

Araban: Sila jawab pada borang jawapan yang disediakan

1. Yang manakah di antara berikut merupakan struktur ularigan ?

A. do ... while and if

B. for and repeat

C. do ... while and if .. else

D. for and do .. while

Soalan 2 & 3 adalah berdasarkan keratan aturcara di bawah

int I I J I M=6i
for ( I = 0 i I < 10 i I += 4 )
for ( J = 10 i J >0 i J - ::: 4 )
cout « "GELUNG"; M:::M+1i

2. Berapa kalikah arahan cetakan akan dilaksanakan ?

A. 16

B. 100 C.40 D. 9

3.

Apakah nilai akhir bagi pembolehubah M ?

A. 7

B. 15

C. 16

D. 9

- 17-

24'0'2,

.'11

.;

(BITG 1113)

4. Yang manakah antara penyataan-penyataan berikut BENAR mengenai gelung do ... while?

A. Ujian batasan di dalam gelung do ... while dilaksanakan sebelum bermula

setiap kitaran. ..

B. Gelung do ... while adalah sarna dengan gelung while, melainkan syarat hubungan diletakkan di atas gelung.

C. Kemaskini penyataan do ... while terkandung di dalarn penyataan ekspresi dirinya sendiri.

D. Penyataan do ... while adalah satu gelung ujian pasca.

5. Anggapkan input adalah 38 45 71 4 1. Apakah output yang dihasilkan oleh kod berikut? Anggapkan semua pembolehubah telah diisytiharkan dengan betu1.

sum = sum + numj

cin>:>numi

sum = numi

while (num <= 40)

{

cin:>:>numi

}

cout«"Sum = "«sum«endli

A. Sum = 38

B. Sum=43

C. Sum = 121

D. Sum = 116

- 18-

--

(BITG 1113)

6. Fungsi bemama PURATA akan menerima 2 input berjenis double dan akan memulangkan satu nilai berjenis char. Apakah prototaip bagi fungsi tersebut? A. double PURATA (char A);

B. char PURATA (double A, B);

C. void PURATA (char A, double B, double C);

D. char PURA TA (double, double);

..

7. Pilih kenyataan yang BENAR mengenai fungsi.

A. Fungsi yang menerima parameter secara pass by value sebenamya menerima salinan nilai parameter sebenar, dan sebarang perubahan terhadap salinan nilai di dalam fungsi itu akan mempengaruhi nilai asal pada bahagian aturcara yang memanggil fungsi tersebut.

B. Untuk memanggil fungsi yang menerima parameter secara pass by reference yang menggunakan penuding, setiap parameter sebenar bagi pembolehubah penuding dimulai dengan simbol *.

C. Parameter yang dihantar kepada fungsi yang dipanggil menggunakan cara pass by reference merupakan alamat pembolehubah.

D. Definisi fungsi dikenali sebagai prototaip fungsi,

'.'

"

8. Manakah di antara penyataan berikut adalah PALSU mengenai main?

A. Satu dan hanya satu fungsi di dalam program akan dipanggi sebagai main.

B. Perlaksanaan aturcara C lazimnya bermula dengan main.

C. Fungsi main adalah dipanggil oleh sistem pengoperasian komputer.

D. Setiap fungsi, selain main. mesti dipanggil oleh main dan mesti dipulangkan ke main.

9.

Operator yang digunakan untuk menspesifikasi pass-by-reference adalah:

A. Dereference operator (#)

B. Pointer operator (")

C. Address operator (&)

D. Array operator ([ ])

- 19"

I

tl

,. 1

,

10.

(BITG 1113) Yang manakah BUKAN mod pembukaan fail:

A. ios::in

B. ios::out

C. ios::add

D. ios::ate

..

11. Diberi pengisytiharan berikut:

char string15 [16] i

Manakah penyataan di bawah adalah TIDAK sah?

A. strcpy(stringI5, "Hello there");

B. strlen( string 15);

.

C. string15 = "Jacksonville";

D. cin»string15;

12.

Perpustakaan C++ yang mengandungikenyataan prototaip bagi operasi fail ialah:

A. fproto.h

B. fstream.h

C. stdio.h

D. stdlib.h

13.

Yang mana antara berikut pemyataan yang benar bagi membuka fail input in_file dan diberikan kepada fail1uaran ya~g bernama project.txt?

A. in_file=project.txt

B. in_file="project.txt"

C. in_ file. open("proj ect. txt");

D. in_file.open(project.txt);

~ 20 ~

I

Fungsi tujuan dibaca dan ditulis

A. file

B. close

C. open

D. stream

..

(BITG 1113)

14.

______ digunakan untuk menyediakan sesuatu file untuk

15. Fungsi yang digunakan untuk membaca satu aksara dalam satu mas a daripada text stream adalah :

A. put 0

B. read ()

C. char ()

D. get t )

16.

Sila pilih pernyataan yang BENAR bagi pengisytiharan berikut:

int komisyen[2] [3]={{95,80,78},{70,90,lOO}}i

1. tatasusunan komisyen di atas mengandungi 6 unsur.

n. Nilai bagi saiz baris dan lajur boleh diabaikan, jika nilai awalan tidak diberikan.

111. Sub braces boleh diabaikan.

IV. Saiz yang sesuai bagi tatasusunan di atas ialah komisyen[3][2]

A. i dan ii

B. :i dan iii

C. i,ii dan iii

D. i, iii dan iv

- 21 -

(BITG 1113)

17. Dalam pengisytiharan tatasusunan seperti di bawah,

double markah [2] [3] i elemen terakhir yang boleh dirujuk ialah :

A. markah[l] [2];

B. markah[2J[3];

C. markah[3] [2];

D. markah[2] [1];

..

18. Diberi pernyataan seperti berikut:

double jumlah, bayar[3] [2] i jumlah= Kira (bayar [2] [0] ) i

Pilih pemyataan yang sesuai bagi kepala fungsi tersebut supaya bersesuaian dengan fungsi yang memanggil.

A. double Kira(double bil[][] double bayar[][])

B. double Kira( double bil[])

C. double Kira(double bil)

D. double Kira(double bayar[][])

19. Yang mana antara kenyataan berikut menunjukkan tatasusunan yang mempunyai empat element bagi empat nombor ganjil pertama?

A int oddsAry[] = {l, 3, 5. 7};

B. int oddsAry[] = [1. 3. 5. 7];

C. int oddsAry[4] = [1, 3,5, 7];

D. int oddsAry[5] = [1,3,5. 7J;

~22 ~

I

\1

..

(BITG 1113)

20. Andaikan pembolebubah telah diisytibarkan, yang manakab antara kenyataan berikut yang boleb membaca lima integer daripada papan kekunci ke dalam tatasususnan?

A. for (I = 1; 1 <= 5; 1++) cin > ary[I++];

B. for (I = 0; 1 < 4; 1++) cin > ary[I++];

C. for (I = 0; 1 < 5; 1++) cin > ary[I++];

D. for (I = 1; 1 < 5; 1++) cin > ary[I++];

21. Berdasarkan keratan aturcara berikut, apakab nilai p l, p2, p3?

int pl,p2,p3i

int *ptl, *pt2, *pt3i pl=lOOi p2=200; p3=300i ptl=&p1i pt2=&P2i pt3=pt2;

*pt3=400;

p2=*pt3;

pt2=&p3;

A. 100,200,300
B. 100,400,300
C. 100.200,200
D. 100,400,400 - 23-

(BITG 1113)

22. Apakah nilai *ip3 pada keratan aturcara berikut?

A.60 B. 80 C.90 D. 100

int nombor[]= {lOO,90,80,70,60}i int *ipl, *ip2,*ip3i ipl=nombori

ip2=ipl+li

ip3=ipl+2i

ip2=ip3+1i

ip3=ip2+1i

ip3=ip2-3;

..

23. Diberi definisi berikut, apakah nilai bagi a selepas keratan aturcara berikut dilarikan?

A. 75

B. 45

C. 55 D.70

int a = S,b;
int *pl = &ai
int *p2;
b == *pl * 2;
*p2 = &bi
a == a * b + *p2 - *pl; ~'

'i: .

(BITG 1113)

24. Diberi pengisytiharan seperti berikut, manakah yang boleh memberikan ralat kompilasi?

..

int i;

float

fi

int *pd;

float *pf i

A. pd =pf;

B. f = 5;

C. pd = &i;

D. pf = &f;

25. Apakah cetakan yang dihasilkan daripada blok aturcara ini ?

int x = 3i
int y 2;
int Z = 1;
int *p = &Xi
int *q = &Yi
int *r = &Zi *r = *q * *Pi *p = *q * *ri

cout « *p « ' , « *q « I I « *r « erid'l ,

A. 3 2 1
B. 2 2 1
C. 12 2 6
D. 11 91 41 - 25-

24 t,O··

I

char nama [20] i

float jualan bulanan[12] i float komisyeni

float bonus;

(BITG 1113)

26 Pertimbangkan pengisytiharan di bawah. Pilih penyataan yang membuat penghantaran nilai satu ahli struktur kepada fungsi yang dipanggil, yang sebenarnya menyebabkan penghantaran alamat.

..

struct pekerja

{

}p1i

A. Kim _jualan(p I.jualan _ bulanan);

B. Isi., maklumat(p 1);

C. Kira _ bonus( &p 1. bonus);

D. Kira_komisyen(p1.komisyen);

;~.;.il.'.

F!'

27. Diberi nama pembolehubah struktur, stu yang mengandungi tatasusunan exams, yang mana antara jawapan berikut merujuk kepada exam ketiga bagi pelajar.

A. stu.exams[2]

B. stu[2].exams[2]

C. stu[2]->exams

D. stu->exams[2]

28.

.Mana antara berikut jawapan rnengenai struktur (s truct) adalah tidak benar?

A. Definasi struktur boleh dimasukkan nilai awalan.

B. Medan di dalam struktur mesti terdiri daripada jenis yang sarna.

C. Struktur boleh dilihat sebagai suatu templet untuk b~hagian individu.

D. Semua medan di dalam sturktur mestilah berkaitan

-26 -

2411

I

f:-- I

(BITG 1113)

I

29. Diberi penuding bernama ptr, bagi satu struktur STU yang mengandungi satu bidang nama, di manakah antara berikut menjawap tepat untuk nama rujukan? A. ptr.name

B. ptr->name

C. ptr.STU.name

D. ptr->STU->name

..

30. Diberi struktur seperti di bawah, manakah merupakan jawapan TIDAK SAH?

struct STU

{

char name [26]; , int midterm [3] ; int final;

} ;

STU class[40]; STU *p = class;

I""', '

=

-_'

~,<~~

L.

t'? ,

<-_ " ,

,

I; ~~:. j-{

A. class[i].name[l]

B. class[i].midterm[2}

C. *p.name

D. p->name

k,

I

I'

- 27 ~

I

I

(BITG 1113)

BAHAGIAN B: (40 Markah)

Soalan 1 (10 markah)

..

a) Apakah yang dicetak oleh kod aturcara di bawah?

(4 markah)

break;

for (x = 1; x < 10i x++)

{

if ( x % 5 != 0)

cout «x« I Ii else

}

b)

Gelung for di bawah telah ditukar kepada gelung do •.• while. Salin semula dan lengkapkan output di bawah untuk gelung do ••• while.

for (x = 10; x>= 1; x--)

{

for (y = x-l; y>=li y--) , cout « " "i

cout « x « endl;

}

(6 markah)

Output:

x: _

do {

y=--do

{

cout«" ";

} while (..__ );

cout « x « end];

} while '-- _l

- 28-

,

;

. ..

I

(BITG 1113)

Soalan 2 (10 markah)

a) Takrifkan satu fail input dan output bemama "fsDataIn" dan "fsfratafnrt" .

Tuliskan kod e++ untuk membuka kedua-dua fail terseblYt. Nama bagi fail-fail input dan output luaran ialah "data.txt" dan "data.out",

(4markah) b) Kodkan kenyataan bagi mengesahkanfail fpDataIn di atas berjaya dibuka.

(6 markah)

Soalan 3 (10 markah)

Dengan bantuan jadual/rajah, tuliskan perubaban nilai setiap pembolebubab apabila segmen aturcara berikut dilaksanakan, dan tuliskan output yang terhasil.

#include <iostream.h>

void tukar_l(int *, int *)i void tukar_2(int x, int *)i

void main ()

{

int a=3, b=8, c=7i tukar_l (&a, &b)i cout«a«b«c«enql; tukar_2 (b. &c) i

cout«a«b«ci

}

void tukar l(int *x. int *y)

{ _

int tempi temp = *Xi *x = *y; *y =tempi

}

void tukar 2(int X, int *y)

{ _

int tempi temp = Xi X = *Yi *y =tempi

}

- 29-

24l'4 il!

I

v.. .~

(BITG 1113)

Soalan 4 (lO markah)

-

a) Di beri tatasusunan seperti berikut, tunjukkan apakah output yang tercetak di

akhir keratan aturcara ini.

..

(6 markah)

int aryl ] = {1, 2, 4,5,8,9,16,17,32, 33};

for (I = 1; I < 10; 1++ ) if (ary [I] % I 1= 0)

cout « ary[1] « U u;

b)

Diberi definisi jenis dan pembolehubah berikut, tuliskan kod aturcara rujukan bagi setiap elemen di dalam pembolehubah.

struct ADDRESS

{

char

struct NAME

{

first [16] i

char last [31] ; } name;

char street [31] ; char city[26] i

} i

ADDRESS addri

(2 markah)

c)

Tuliskan kod rujukan bagi setiap elemen di dalam (a) jika baris terakhir kod,

ADDRESS

addri

ditukar kepada ADDRESS *Pi

(2 markah)

-30 -

- 31 -

(BITG 1113)

BAHAGIAN C: (15 Markah)

Soalan (15 markah)

Anda dikehendaki untuk membangunkan satu program untuk mengirajumlah kos bagi penempahan pizza. Di bawah adalah keperluan yang anda perlu ikuti:

_(a) Menggunakan pembolehubah totaiCost, pizzaNum, PricePerTopping dan PlainPrice di dalam program anda.

(b) Gunakan fungsi main untuk:

• Mendapatkan bilangan pizza

• Memanggil costOfPizzaO dan mendapatkan nilai pulangan daripadanya

• Mencetak maklumbalas bagi bilangan pizza, jumlah kos dan "Thank

you!".

(c) Gunakan fungsi costOfPizza untuk:

-

• Mendapatkan bilangan toppings

• Mengira jumlah koso

• Memulangkan jumlah kos ke maim)

(d) Menetapkan pembolehubah PricePerTopping and PlainPrice sebagai pemalar di dalam costOfi>izzaO. Setiap topping berharga RM2 manakala pizza kosong berharga RM 10.

(e) Menghasilkan elemen NumberOfToppings dan Cost di dalam struktur "PIZZA" dan mengguna rujukan myPizza untuk menyimpan nilai ke dalam pembolehubah-pembolehubah tersebut.

(f) Menggunakan gelung for untuk mendapatkan jumlah kos bagi kesemua pizza yang ditempah.

Hasil akhir adalah seperti berikut:

~"*-l(~****Ol'del'iflg Pizza ***** How many pizza do you want to order? J

BOil filany topping8 do you Hant fop :pizza number- 1 ?
1
Ho u wany toppings do YDU Hant fol' pi2:za numher 2 ?
2
Hou Illany toppings do you H<lnt for piZZa flul1lbel' 3 t
3
'tou have o r-dc r-c d 3 pizza(s) .
The total cost .is RM42. Thank you!
Press any key to continue Tuliskan aturcara lengkap untuk masalah di atas.

2416

Potrebbero piacerti anche