You are on page 1of 11

2

MTS3013: Structured Programming


SECTION A: (70 Marks)
Instruction: Answer all questions in the answer booklet provided.
Arahan : Jawab semua soalan di dalam buku jawapan yang disediakan.

1.

State whether the following statements are TRUE (T) or FALSE (F).
Nyatakan samada setiap penyataan berikut BENAR(B) atau PALSU(P).
a. Statement for(x=1;x>0;x--) will execute statement in the loop at least once.
(Penyataan for(x=1;x>0;x--) akan melaksanakan penyataan dalam gelung
sekurang-kurangnya sekali).
b. The dowhile is also called as post-test loop.
(Penyataan dowhile dipanggil juga sebagai gelung pos-semakan).
c. The condition customer in a statement while (customer == 1), must be declared
as int customer before it can be used in a program.
(Syarat customer dalam pernyataan while (customer == 1), mesti diisytiharkan
sebagai int pilihan sebelum boleh digunakan di dalam atur cara).
d. The declaration of char Department[20]={COMPUTING}; is a VALID declaration.
(Pengisytiharan tatasusunan char Department[20]={COMPUTING}; merupakan
pengisytiharan yang SAH).
e. Logic errors occur because users do not follow the syntax in C++ language.
(Ralat logik berlaku disebabkan pengguna tidak mengikut sintaks dalam bahasa C++).

[5 marks]

2.

Write C++ statements for the following statements.


a. Display an output as shown below:
Paparkan output seperti di bawah:
SELAMAT DATANG
MALAYSIA
MALAYSIA
MALAYSIA

[4 marks]

[see next page

MTS3013: Structured Programming


b. Write the following arithmetic equation in C++ statement.
(Tulis penyataan ungkapan aritmatik berikut dalam penyataan C++).
Z= 3x+t + xy2
ab

[1 mark]

3. State the output of the following program segments.


Nyatakan output bagi segmen aturcara berikut.

a.

void main()
{
int i = 0;
switch (i + 2)
{
case -1:
cout
case 0:
cout
case 1:
cout
case 2:
cout
default:
cout
}
}

<< "\n Art Department "; break;


<< "\n IT Department"; break;
<< "\n MM Department"; break;
<< \n FSKIK; break;
<< "\n Error";

[1 mark]
b. int number;
for (number = 10; number < 15; number++)
{
if ((number % 2) == 0)
cout << 2 * number << " ";
else
cout << number << " ";
}

[5 marks]

4. Program A is written using while loop. Rewrite the program using for loop.
(Program A ditulis menggunakan gelung while. Tulis semula program menggunakan gelung for).
int x = 10;
[see next page

MTS3013: Structured Programming


while (x
{
b =
c =
if

< 100)

45;
10;
( x > 50 )
if ( b < c
result
else
result
cout << x << "
x += 50;

)
= b/3;
= c%3;
& "<<result<<endl;

Program A
[4 marks]
5. Program B has errors. Find the errors and rewrite the correct program. Underline your
answers.
(Program B mempunyai ralat. Cari ralat tersebut dan tulis semula program yang betul.
Gariskan jawapan anda).
// This program divides a user-supplied number by another user-supplied number.
// It checks for division by zero.
#include <iostream>
int main( )
{
cout >> "Enter a number : ";
cin << num1;
cout >> "Enter another number : ";
cin << num2;
float num1, num2, quotient;
if (num = 0)
cout << "Divison by zero is not possible. \n";
cout << "Please run the program again ";
cout << "and enter a number besides zero. \n";
else
quotient = num1 / num2;
cout << "The quotient of " <<num1 <<
cout << " divided by " <<num2 << " is ";
cout << quotient << endl;
return 0;
}

Program B
[10 marks]
[see next page

MTS3013: Structured Programming

6. University KSI has practiced a drive thru students registration to avoid the traffic jammed
on a new students registration day. Every vehicle that passes through the security guard
building is required to show the students registration offer letter. A green card will be given
to male students and they are instructed to go to either College A or B. On the other hand, a
red card will be given to female students and they are instructed to go either to College C or
D. At the College Registration Office, each student will be allocated to their college
accordingly.
Program C will assist the college management administration to: (i) determine the number
of students that have been registered at every college and (ii) the total number of male
and female students that have been registered. However, the program has a few logic
errors and you are required to analyze the program to answer question a, b and c.
(Universiti KSI telah mempraktikkan pendaftaran pelajar secara pandu lalu bagi
mengelakkan kesesakan lalu lintas di hari pendaftaran pelajar baru. Setiap kenderaan yang
melalui pondok pos pengawal dikehendakki menunjukkan surat tawaran pendaftaran
pelajar. Kad berwarna hijau akan diberikan kepada pelajar lelaki dan mereka diarahkan
pergi sama ada ke kolej A atau kolej B. Sebaliknya, kad berwarna merah akan diberikan
kepada pelajar perempuan dan mereka akan diarahkan pergi sama ada ke kolej C atau
kolej D. Di pejabat pendaftaran kolej, pelajar akan ditempatkan di kolej yang sepatutnya).
Atur cara C akan membantu pihak pengurusan kolej untuk: (i) menentukan bilangan
pelajar yang telah mendaftar di setiap kolej dan (ii) jumlah bilangan pelajar lelaki dan
pelajar perempuan yang telah mendaftar. Walau bagaimanapun, atur cara ini mempunyai
kesilapan logik dan anda dikehendaki mengkaji atur cara ini untuk menjawab soalan a, b
dan c.

#include <iostream.h>
void main()
{
char card, college;
int countA = 1, countB = 1, countC=1, countD= 1;
int male, female;
char respon = 'Y';
while (respon == 'Y')
{
cout << "Colour of the card";
cin >> card;
if (card == 'G')
{ cout << "Students College";
cin>> college;
if (college =='A')
countA;
[see next page

MTS3013: Structured Programming


else
countB;
}
else if (card =='R')
{ cout << "Students College";
cin >> college;
if (college == 'C')
countC;
else
countD;
}
male = countA;
female = countC;
cout << "Is there any student to key in?" << endl;
cin >> respon;
}
cout
cout
cout
cout
cout
cout

<<
<<
<<
<<
<<
<<

"Total
"Total
"Total
"Total
"Total
"Total

of
of
of
of
of
of

student at A:" << countA << endl;


student at B:" << countB << endl;
student at C:" << countC << endl;
student at D:" << countD << endl;
male student:" << male << endl;
female student" << female << endl;

}
Program C

a. You are required to identify logic errors and state the effects of these logic errors to the
program output.
(Anda dikehendaki mengenal pasti ralat logik tersebut dan kesan ralat logik ini pada
output atur cara).

[5 marks]
b. Correct the errors by rewriting the correct statements.
(Betulkan ralat tersebut dengan menulis semula penyataan yang betul).
[4 marks]
c. State the reason why for loop is not suitable to be used in the above programming.
(Nyatakan sebab kenapa gelung for tidak sesuai digunakan dalam atur cara di atas).
[1 mark]

[see next page

MTS3013: Structured Programming

7. A particular talent competition has five judges, each of whom awards a score between 0 and
10 to each performer. Fractional scores, such as 8.3 are allowed. A performer's final score is
determined by finding the average of all five scores. Write a program that includes the
following functions:
(Satu pertandingan bakat dihakimi oleh lima juri, setiap daripada mereka memberi skor
antara 0 dan 10 kepada setiap peserta. Skor perpuluhan seperti 8.3 adalah dibenarkan.
Skor akhir bagi peserta ditentukan dengan mencari purata bagi kesemua lima markah. Tulis
satu program yang merangkumi fungsi-fungsi berikut):
a. The main( ) function that accepts the marks from all five judges as an input, then call
function calcScore( ).
(Fungsi main( ) yang menerima markah dari kelima-lima juri sebagai input, kemudian
memanggil fungsi calcScore( )).
[6 marks]
b. calcScore( ) function will calculate the average of all the five scores the performer
received. This function should be called just once by main( ) function, should be
passed the five scores and return the average value to the main( ) function to be
displayed.
(Fungsi calcScore( ) akan mengira purata bagi kesemua lima skor yang diterima
oleh peserta. Fungsi ini akan dipanggil hanya sekali oleh fungsi main( ), menerima
nilai kelima-lima markah dan mengembalikan nilai purata kepada fungsi main( ) untuk
dipaparkan).
[4 marks]

8. By assuming addresses of a and z are 2000 and 2010 respectively, state the output of the
following programs.
Dengan mengandaikan alamat bagi a ialah 2000 dan alamat bagi z ialah 2010, nyatakan
output bagi atur cara berikut.
a.

void main()
{
int a = 25;
int *z;
z = &a;
cout << z << endl;
cout << *z << endl;
*z = 55;
cout << *z << endl;
cout << &a;
}

[4 marks]

[see next page

MTS3013: Structured Programming


b.

void main()
{
int a = 89, k = a;
int *z;
z = &a;
cout << *z << k;
}
[2 marks]

c. What is the output of the following program segment?


(Apakah output bagi keratan atur cara berikut?)
int myScore = 92;
int myNewScore;
int *ptr;
ptr = &myScore;
cout << "myScore = " << *ptr << endl;
*ptr = *ptr + 2;
myNewScore=*ptr;
cout <<"myScore is"<<myScore<<endl;
cout << "myNewScore = " <<myNewScore << endl;

[4 marks]
9. The structure Camera is defined as follows:
(Struktur Camera ditakrifkan seperti berikut):
struct Camera
{
char cameraMake[20];
char cameraModel[20];
char zoomLevel[5];
float megaPixels;
float price;
};
a. Declare an array variable of the Camera structure to store data for 25 types of camera.
(Isytiharkan satu pembolehubah tatasusunan berjenis struktur Camera untuk
menyimpan data bagi 25 jenis kamera).
[2 marks]
b. Write a program segment to input data into the variable from question (a) above.
(Tulis satu segmen program untuk input data ke dalam pembolehubah dari soalan (a) di
atas).
[3 marks]
[see next page

MTS3013: Structured Programming

c. Write a program segment to display all the camera models with price of more than RM
500.
(Tulis satu segmen program untuk memaparkan semua model kamera dengan harga
lebih daripada RM 500).
[5 marks]

[see next page

10

MTS3013: Structured Programming


SECTION B: (30 marks)
Instruction: Answer only 1 (ONE) question in the answer booklet provided.
Arahan : Jawab hanya 1 (SATU) soalan di dalam buku jawapan yang disediakan.

1. Universiti Pendidikan Sultan Idris has 150 staffs which consist of an academic and a non
academic group. Each staff is given an option to select either a KWSP scheme or a
PENSION scheme. Display the details of each staff information and the total number of
academic staffs and non academic staffs that have selected a KWSP scheme and a
PENSION scheme. You are required to get an output as shown below. Use an array to
solve this problem. An example of output is shown below.

(Universiti Pendidikan Sultan Idris mempunyai 150 kakitangan yang terdiri dari golongan
akademik dan bukan akademik. Setiap kakitangan diberi pilihan untuk memilih sama ada
skim KWSP atau pun skim pencen. Paparkan maklumat setiap kakitangan dan bilangan
kakitangan akademik dan bukan akademik yang memilih skim pencen dan juga skim
KWSP. Anda dikehendaki mendapatkan output seperti yang ditunjukkan bawah. Gunakan
tatasusunan untuk menyelesaikan masalah ini. Contoh output adalah seperti yang
ditunjukkan di bawah).

OUTPUT:
Staffs Information:
Name

IC no.

Age

Category

Scheme

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

Selected Scheme:
Academic Category
KWSP

____

PENSION

____

[see next page

11

MTS3013: Structured Programming

Non-Academic Category
KWSP

____

PENSION

____
[30 marks]

2. Write a program that computes and displays the charges for a patient's hospital stay. First,
the program should ask if the patient was admitted as an in-patient or an out-patient. If the
patient was an in-patient, the following data should be entered:
a.
b.
c.
d.

The number of days spent in the hospital


The daily rate
Hospital medication charges
Charges for hospital services (lab test, etc.)

The program should ask for the following data if the patient was an out-patient:
a. Charges for the hospital services (lab test, etc.)
b. Hospital medication charges
The program should use two functions to calculate the total charges. One of the functions
should accept arguments for the in-patient data, while the other function accepts arguments
for out-patient information. Both functions should return the total charges.
Note: The program should check for input validation which does not accept negative
numbers for any data.
(Tulis satu program yang mengira dan memaparkan caj bagi penginapan pesakit hospital.
Pertama, program ini harus bertanya jika pesakit telah dimasukkan sebagai pesakit dalam
atau pesakit luar. Jika pesakit adalah pesakit dalam, data berikut perlu dimasukkan:
a.
b.
c.
d.

Bilangan hari menginap di hospital


Kadar caj harian
Caj ubat hospital
Bayaran perkhidmatan hospital (ujian makmal, dan lain-lain)

Program ini perlu meminta data berikut jika pesakit adalah seorang pesakit luar:
a. Caj untuk perkhidmatan hospital (ujian makmal, dan lain-lain)
b. Caj ubat hospital

[see next page

12

MTS3013: Structured Programming

Program ini perlu menggunakan dua fungsi untuk mengira jumlah bayaran. Fungsi pertama
perlu menerima parameter untuk data pesakit dalam, manakala fungsi kedua perlu
menerima parameter untuk data pesakit luar. Kedua-dua fungsi perlu memulangkan nilai
jumlah bayaran.
Nota: Program ini perlu menyemak untuk pengesahan input di mana program tersebut tidak
boleh menerima nombor negatif bagi kemasukan data).
[30 marks]

[END OF QUESTIONS]

[see next page

You might also like