Sie sind auf Seite 1von 6

MTS 3013 : STRUCTURED PROGRAMMING

SEMESTER 1 SESI 2013/2014


SET A (1 HOURS)

Instruction: Answer all questions in your own answer paper.


Arahan : Jawab semua soalan di dalam buku jawapan anda sendiri.

1. State whether the following list of identifiers are valid or invalid. If it is not valid, state the reason why.
Nyatakan samada senarai pencam berikut sah atau tidak sah. Jika ia tidak sah, nyatakan sebabnya.
1time :
Invalid, identifiers cannot begin with number

two fold :
Invalid, identifiers cannot have space in between

c3p0:
Valid

[5 marks]

2. Identify and correct the errors in each of the following programming segment. (Note : There may be more
than one error in each piece of code).
Kenalpasti dan betulkan ralat dalam setiap segmen atur cara berikut. (Nota: Kemungkinan terdapat lebih
daripada satu ralat dalam setiap kod).

The following code should output the odd integers from 19 to 1


Kod berikut mengeluarkan output integer genap daripada 19 ke 1
for (x == 19; x >= 1; x+=2)
cout << x << endl
for (x = 19; x >= 1; x-=2)
cout << x << endl;

The following statement should determine if count is outside the range of 0 through 100.
Pernyataan berikut mengenalpasti samada count berada di luar julat 0 hingga 100.
if (count < 0 && count > 100)
if (count < 0 || count > 100)

The following statement should assign 0 to z if a is less than 10, otherwise it should assign 7 to z.
Segmen berikut mengumpukkan 0 kepada z sekiranya a kurang daripada 10, jika tidak ia
mengumpukkan 7 kepada z.
if (a < 10);
z == 0;
else
z == 7;
if (a < 10)
z = 0;
else
z = 7;
[6 marks]

3. Rewrite the program segment below using for loop.


Tulis semula segmen atur cara berikut menggunakan gelung for.
count = 0;
i = 0;
while (i < 12)
{
cin >> x;
if (x == i)
count++;
i++;
}
count = 0;
for (i = 0; i < 12; i++)
{
cin >> x;
if (x == i)
count++;
}
[4 marks]

4. Write a program to ask the user to input two numbers, compare the numbers, and then prints the larger
number followed by the words "is larger". If the numbers are equal, print the message "These numbers
are equal."
Tuliskan atur cara yang menyuruh pengguna memasukkan dua nombor, membandingkan nombor
tersebut, dan mencetak nombor yang lebih besar. Jika nombor tersebut sama, cetak mesej Nombor
tersebut adalah sama.
#include <iostream.h>
void main()
{ int num1, num2;
cout <<"Enter two numbers : ";
cin >> num1 >> num2;
if (num1 > num2)
cout << num1 << " is larger. ";
else if (num2 > num1)
cout << num2 << " is larger. ";
else if (num1 = = num2)
cout << " These numbers are equal. ";
[10 marks]

5. Write C++ statements to print the integers from 1 to 20 using while loop and the counter variable x.
Assume that the variable x has been declared. Print only 5 integers per line. (Hint : Use the calculation x
% 5. When the value of this is 0, print \n (a new line character), otherwise print \t (a tab character)).
Tulis pernyataan C++ untuk mencetak integer daripada 1 hingga 20 menggunakan gelung while dan
pembolehubah pembilang x. Andaikan pembolehubah x telah diisytihar. Cetak hanya 5 integer pada satu
baris. (Kunci: Gunakan pengiraan x % 5. Apabila nilai didapati 0, cetak \n, jika tidak cetak \t).
x = 1;
while ( x <= 20)
{ cout << x << "\t";
if (x % 5 = = 0)
cout << "\n";
x++;
}
[10 marks]

MTS 3013 : STRUCTURED PROGRAMMING


SEMESTER 1 SESI 2013/2014
SET B (1 HOURS)

Instruction: Answer all questions in your own answer paper.


Arahan : Jawab semua soalan di dalam buku jawapan anda sendiri.

1. State whether the following list of identifiers are valid or invalid. If it is not valid, state the reason why.
Nyatakan samada senarai pencam berikut sah atau tidak sah. Jika ia tidak sah, nyatakan sebabnya.

income#1

Invalid, identifiers cannot include special characters

case

Invalid, reserved word cannot be used as identifiers

_staff_num

Valid
[5 marks]

2. Identify and correct the errors in each of the following. (Note : There may be more than one error in each
piece of code).
Kenalpasti dan betulkan ralat dalam setiap segmen atur cara berikut. (Nota: Kemungkinan terdapat lebih
daripada satu ralat dalam setiap kod).

The following code should output the even integers from 2 to 100
Kod berikut mengeluarkan output integer ganjil daripada 2 ke 100
char counter = 2;
do
{
cout << counter << endl;
counter = counter + 2;
} While (counter < 100);
int counter = 2;
do
{
cout << counter << endl;
counter = counter + 2;
} while (counter < 100);

The following code should determine if x is not greater than 20.


Kod berikut mengenalpasti jika x bukan lebih besar daripada 20.
if (!x > 20)
if (x < 20)

The following statement should determine if count is within the range of 0 through 100.
Pernyataan berikut mengenalpasti jika count berada dalam julat 0 hingga 100.
if (count >=0 || count <= 100)
if (count >=0 && count <= 100)
[6 marks]

3. Rewrite the program segment below using for loop.


Tulis semula segmen atur cara berikut menggunakan gelung for.
count = 0;
i = 0;
while (i < 12)
{
cin >> x;
if (x == i)
count++;
i++;
}
count = 0;
for (i = 0; i < 12; i++)
{
cin >> x;
if (x == i)
count++;
}
[4 marks]
4. Write a program to ask the user to input two numbers, compare the numbers, and then prints the larger
number followed by the words "is larger". If the numbers are equal, print the message "These numbers
are equal."
Tuliskan atur cara yang menyuruh pengguna memasukkan dua nombor, membandingkan nombor
tersebut, dan mencetak nombor yang lebih besar. Jika nombor tersebut sama, cetak mesej Nombor
tersebut adalah sama.
#include <iostream.h>
void main()
{ int num1, num2;
cout <<"Enter two numbers : ";
cin >> num1 >> num2;
if (num1 > num2)
cout << num1 << " is larger. ";
else if (num2 > num1)
cout << num2 << " is larger. ";
else if (num1 = = num2)
cout << " These numbers are equal. ";
[10 marks]

5. Write C++ statements to print the integers from 1 to 20 using while loop and the counter variable x.
Assume that the variable x has been declared. Print only 5 integers per line. (Hint : Use the calculation x
% 5. When the value of this is 0, print \n (a new line character), otherwise print \t (a tab character)).
Tulis pernyataan C++ untuk mencetak integer daripada 1 hingga 20 menggunakan gelung while dan
pembolehubah pembilang x. Andaikan pembolehubah x telah diisytihar. Cetak hanya 5 integer pada satu
baris. (Kunci: Gunakan pengiraan x % 5. Apabila nilai didapati 0, cetak \n, jika tidak cetak \t).
x = 1;
while ( x <= 20)
{ cout << x << "\t";
if (x % 5 = = 0)
cout << "\n";
x++;
}
[10 marks]

Das könnte Ihnen auch gefallen