DNT 126 Basic Computer Programming [Asas Pengaturcaraan...

14
SULIT SULIT UNIVERSITI MALAYSIA PERLIS Peperiksaan Semester Kedua Sidang Akademik 2011/2012 Mac 2012 DNT 126 Basic Computer Programming [Asas Pengaturcaraan Komputer] Masa: 3 jam Please make sure that this question paper has FOURTEEN (14) printing pages including this front page before you start the examination. [Sila pastikan kertas soalan ini mengandungi EMPAT BELAS (14) muka surat yang bercetak termasuk muka hadapan sebelum anda memulakan peperiksaan ini.] This question paper has THREE (3) parts. Answer ALL questions in Part A. Choose ONE (1) question from Part B, and choose ONE (1) question in Part C. [Kertas soalan ini mengandungi Tiga (3) bahagian. Jawab SEMUA soalan dari Bahagian A, SATU (1) soalan dari Bahagian B, dan SATU (1) soalan dari Bahagian C]

Transcript of DNT 126 Basic Computer Programming [Asas Pengaturcaraan...

Page 1: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT

SULIT

UNIVERSITI MALAYSIA PERLIS

Peperiksaan Semester Kedua

Sidang Akademik 2011/2012

Mac 2012

DNT 126 – Basic Computer Programming

[Asas Pengaturcaraan Komputer]

Masa: 3 jam

Please make sure that this question paper has FOURTEEN (14) printing pages including this

front page before you start the examination. [Sila pastikan kertas soalan ini mengandungi EMPAT BELAS (14) muka surat yang bercetak termasuk muka

hadapan sebelum anda memulakan peperiksaan ini.]

This question paper has THREE (3) parts. Answer ALL questions in Part A. Choose ONE (1)

question from Part B, and choose ONE (1) question in Part C. [Kertas soalan ini mengandungi Tiga (3) bahagian. Jawab SEMUA soalan dari Bahagian A, SATU (1)

soalan dari Bahagian B, dan SATU (1) soalan dari Bahagian C]

Page 2: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-2-

SULIT

Part A [Bahagian A]

Question A1 [Soalan A1]

(a) A computer is composed of several parts for it to function. [Komputer terdiri daripada beberapa bahagian untuk membolehkannya beroperasi.]

(i) Draw a block diagram of all parts. [Lukiskan gambarajah blok semua bahagian .]

(3 Marks/Markah)

(ii) Determine the functions of each part. [Beri fungsi-fungsi bagi setiap bahagian.]

(5 Marks/Markah)

(b) Define the terms algorithm and pseudocode. [Takrifkan algoritma dan pseudokod.]

(2 Marks/Markah)

(c) Identify and correct the errors in each of the following statements: [Kenalpasti dan betulkan kesalahan-kesalahan bagi setiap pernyataan berikut:]

(i) (scanf(“%d %d %f”, a,b,c);

(ii) b=a+10;

printf(“The answer is=%d, b);

(iii) if (a >< 5);

printf(“a is more or equal than 5\n”);

(iv) else (a >= 5)

printf(“a is larger or equal than 5\n”);

(4 Marks/Markah)

.../3

Page 3: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-3-

SULIT

Figure 1 [Rajah 1]

(d) Figure 1 shows a simple program to calculate the force based on the input time.

The formulas for the force depend on whether time, t is less or equal to 5

seconds. [Rajah 1 menunjukkan sebuah program ringkas untuk mengira daya berdasarkan input masa.

Formula untuk mengira daya bergantung kepada samaada masa, t kurang atau bersamaan

dengan 5 saat.]

(i) Draw a flowchart based on the program in Figure 1. [Lakar carta alir berdasarkan aturcara Rajah 1]

(6 Marks/Markah)

#include<stdio.h>

int main()

{

double f, t;

printf("Please enter time in seconds:\n");

scanf("%lf",&t);

if (t<=5)

f=20;

else

f=4*(t+2);

printf("Force, f=%lf(N)",f);

return 0;

}

Page 4: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-4-

SULIT

Question A2

[Soalan A2]

(a) The following questions are based on the following programs in Figure 2 and Figure

3: [Soalan berikutnya adalah berdasarkan aturcara di Rajah 2 dan Rajah 3:]

#include <stdio.h> #include <stdio.h>

int main () int main ()

{ {

int temp; int temp;

printf (“Enter temperature : ”); printf (“Enter temperature : ”);

scanf (“%d”, &temp); scanf (“%d”, &temp);

if (temp > 30) if (temp > 30)

printf (“\nHot\n”); printf (“\nHot\n”);

else if (temp > 20) if (temp > 20)

printf (“\nMild\n”); printf (“\nMild\n”);

else if (temp > 10) if (temp > 10)

printf (“\nCold\n”); printf (“\nCold\n”);

else if (temp <= 10)

printf (“\nVery Cold\n”); printf (“\nVery Cold\n”);

return 0; return 0;

} }

Figure 2 Figure 3 [Rajah 2] [Rajah 3]

(i) Given the input value for temperature is 27, write down the output for a

programs. Identify which program produces more logical output. [Diberi nilai masukan untuk suhu ialah 27, tulis hasil keluaran bagi kedua-dua aturcara.

Kenalpasti aturcara yang dapat menghasilkan hasil keluaran yang lebih logik.]

(3 Marks/Markah)

(ii) Based on your answer in Question 2(a)(i), compare and briefly explain the

difference between using a single-entry/single-exit (1 way selection) if

statement or 2 way selection if…else statement. [Berdasarkan jawapan di dalam Soalan 2(a)(i), banding dan terangkan secara ringkas

perbezaan antara penggunaan satu-masukan/satu-keluaran (pilihan 1 hala) penyataan if atau

pilihan 2 hala penyataan if…else.]

(2 Marks/Markah)

.../5

Page 5: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-5-

SULIT

(b) Write a switch statement that display the movement direction of a robot controlled

by pressing specific alphabets on device keyboard. Use the information given in

Figure 4. [Tulis penyataan suis yang memaparkan arah pergerakan bagi sebuah robot kawalan dengan

menekan huruf-huruf tertentu di atas papan kekunci peranti. Gunakan maklumat yang diberikan

dalam Rajah 4.]

Figure 4 [Rajah 4]

(6 Marks/Markah)

(c) Compare and describe the difference between while loops and do-while loops. [Bandingkan dan terangkan perbezaan di antara gelung while dan gelung do-while.]

(2 Marks/Markah)

(d) Write a complete program containing a loop to display the numbers as in Figure 5.

[Tuliskan aturcara lengkap yang mengandungi gegelung untuk memaparkan nombor-nombor seperti

dalam Rajah 5.]

Figure 3b [Rajah 3b]

Figure 5 [Rajah 5]

(7 Marks/Markah)

Instruction Direction

F or f Move Forward

B or b Move Backward

R or r Turn Right

L or l Turn Left

Other alphabets Invalid movement direction

2 4 6 8 10 12 14 16 18

Page 6: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-6-

SULIT

Question A3 [Soalan A3]

(a) Write a function prototype for a sum_n_avg that has three type double input

parameters and two output parameters according to Figure 6. [Tuliskan fungsi prototaip bagi sum_n_avg yang mempunyai tiga jenis double parameter masukan

dan dua parameter keluaran mengikut Rajah 6.]

n1 sump

n2

n3 avgp

Figure 6

[Rajah 6]

(2 Marks/Markah)

(b) The function in (a) computes the sum and the average of its three input arguments

and produces its results through two output parameter. Complete the function call

statement. [Fungsi mengira sum dan average tiga masukan hujah dan menyampaikan keputusannya melalui

dua keluaran parameter. Lengkapkan penyata fungsi panggilan.]

{ double one, two, three, sum, avg;

printf(“ Enter the three numbers> ”);

scanf(“%lf%lf%lf”, &one,&two,&three);

sum_n_avg(____________________________________________);

...........

}

(2 Marks/Markah)

(c) Write a complete C program for your answers in part (a) and (b) to determine the

value of sum and average by using call by reference with pointer argument

input/output parameters. [Tulis satu atucara C yang lengkap kepada jawapan anda di bahagian (a) dan (b) untuk

mendapatkan jumlah bagi sum dan average dengan menggunakan panggilan rujukan dengan hujah

pointer masukan/keluaran parameter. ]

(6 Marks/Markah)

.../7

sum_n_avg

Page 7: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-7-

SULIT

(d) Consider two dimensional arrays given below. Write their functions in C: [Pertimbangkan array dua dimensi telah diberikan di bawah. Tulis fungsi di dalam C:]

A * B = C

(i) to compute the sum of all the elements in each row of matrix A. [untuk mengira semua jumlah elemen dalam setiap baris matrik A]

(3 Marks/Markah)

(ii) the compute the sum of all elements in each column matrix A. [untuk mengira semua jumlah elemen dalam setiap lajur matrik A]

(3 Marks/Markah)

(iii) to calculate the product of matrix C. [untuk mengira hasil darab matrik C]

(4 Marks/Markah)

Page 8: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-8-

SULIT

PART B

[Bahagian B]

Question B4

[Soalan B4]

An autonomous system is used to detect and determine the color of traffic-light

signal, when cars are moving on the road. The system generates 3 signals based on

the color of the traffic-light (Red, Green, Yellow). Assume the color signal is entered

by the user from the keyboard as (R, G, Y). Notify the driver by showing the message

on the screen (Stop, Go, Wait) based on the color captured by the system. [Satu sistem berautonomi digunakan untuk mengesan dan menentukan isyarat warna lampu lalulintas,

ketika kereta sedang bergerak di atas jalan. Sistem ini menghasilkan 3 isyarat berdasarkan warna

lampu lalulintas(Merah, Hijau, Kuning). Andaikan warna isyarat berkenaan di masukkan oleh

pengguna daripada papan kekunci sebagai (R, G, Y). Beritahu pemandu dengan menunjukkan mesej di

atas skrin (Berhenti, Jalan, Tunggu) berdasarkan warna isyarat yang ditangkap oleh sistem.] (a) Formulate a pseudo-code algorithm for the problem.

[Rumuskan algorithma kod-pseudo bagi masalah ini.] (4 Marks/Markah)

(b) Illustrate the flow of control by drawing a suitable flowchart for the above

problem. [Ilustrasikan aliran kawalan dengan melukis carta-alir yang sesuai bagi masalah di atas.]

(4 Marks/Markah)

(c) Write a C program to solve this problem using if…else statement. [Tuliskan aturcara C untuk menyelesaikan masalah ini menggunakan pernyataan if…else]

(5 Marks/Markah)

(d) Write a C program to solve this problem using switch statement.

[Tuliskan aturcara C untuk menyelesaikan masalah ini menggunakan pernyataan switch.] (5 Marks/Markah)

(e) Discuss the suitability between the two statements in part (c) and part (d) based on

this problem. [Bincangkan kesesuaian di antara dua pernyataan berkenaan di bahagian (b) dan bahagian (c)

berdasarkan masalah ini.] (2 Marks/Markah)

Page 9: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-9-

SULIT

Question B5 [Soalan B5]

(a) The value of an array shown in Figure 7. Develop a C programming to show the

relationship between the array and the pointer, that has a same value in the array. [Nilai array ditunjukkan dalam Rajah 7. Bangunkan sebuah pengaturcaraan C bagi menunjukkan

hubungan antara array dan pointer yang mempunyai nilai yang sama di dalam array.]

(5 Marks/Markah)

Array Value Pointer

my_array[0] 12 *(ptr+0)

my_array[1] 3 *(ptr+1)

my_array[2] 41 *(ptr+2)

my_array[3] 2 *(ptr+3)

my_array[4] 6 *(ptr+4)

Figure 7 [Rajah 7]

(b) Write a C programming requesting a ten digit number by using an array and the

programming output will displays the largest number from the array. [Tuliskan pengaturcaraan C untuk memasukan sepuluh digit nombor dengan menggunakan array

dan keluaran atucara akan memaparkan nombor terbesar daripada array.]

(6 Marks/Markah)

.../10

Page 10: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-10-

SULIT

(c) Write a programming to read a list of integers from text file that consists of the

data shown in Figure 8 and name the text file as “Number.txt”. The

programming will write the output data onto the output text file shown in Figure

9, where the output text file is named as “Result.txt”. Perform the following

operation to find and print the sum and the average of the integers. [Tulis pengaturcaraan untuk membaca senarai integer dari fail teks yang mengandungi data yang

ditunjukkan dalam Rajah 8 dan namakan fail teks anda sebagai "Number.txt”. Pengaturcaraan

akan menulis data keluaran ke fail teks keluaran yang ditunjukkan dalam Rajah 9, di mana fail

teks keluaran dinamakan sebagai "Result.txt". Laksanakan maklumat berikut untuk mencari dan

mencetak jumlah dan purata integer.]

(9 Marks/Markah)

3

11

45

Figure 8

[Rajah 8]

*************** Result *****************

The number of integers is : 3

The sum of the integers is : 59

The average of the integers is : 19.67

**************************************

Figure 9

[Rajah 9]

Page 11: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-11-

SULIT

PART C

[Bahagian C]

Question C6

[Soalan C6]

Write a complete program to calculate simple mathematical operations involving two

numbers, that produces an output as shown in Figure 10. You are required to write three

functions named menu, calc_op, and print_result. The descriptions of the three

functions are follows: [Tulis satu program yang lengkap untuk mengira operasi matematik yang mudah yang melibatkan dua

nombor, yang menghasilkan output seperti yang ditunjukkan dalam Rajah 10. Anda dikehendaki menulis

tiga fungsi yang bernama menu, calc_op, dan print_result. Keterangan fungsi tiga adalah seperti berikut:]

menu – this function will display the menu of the variable mathematical

operations. [menu - fungsi ini akan memaparkan menu operasi matematik yang berubah-ubah.]

calc_op- this function will execute the mathematical operation based on

user selection. The resulting of the mathematical operation will be

returned from this function. [calc_op - fungsi ini akan melaksanakan operasi matematik berdasarkan pemilihan

pengguna. Yang menyebabkan operasi matematik akan pulang dari fungsi ini.]

print_result – this function will display the result from the selected

mathematical operation. [print_result - fungsi ini akan memaparkan hasil daripada operasi matematik yang

dipilih.]

Figure 10 (Rajah 10)

(20 Marks/Markah)

Welcome to mini calculator

1- Add

2- Subtract

3- Multiply

Enter two numbers : 10 15

Enter operation : 1

You have selected add operation

Result = 25

Thank you

Page 12: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-12-

SULIT

Question C7 [Soalan C7]

Table 2 [Jadual 2]

Table 1 [Jadual 1]

Name Weight(kg) Height(m)

Firdaus 89 1.85

Gopal 78 1.81

Chong 90 1.64

Table 1 shows list of persons by data collected to measure the BMI and compare which

one status body mass index present. The range of body mass index had shown in Table 2.

Student shall calculate the BMI by using the following formula;

Your task is to develop a C program that can calculate the BMI and the Status of each

person as shown in Figure 11. The program MUST display Name, Weight (kg), Height

(m), BMI (kg/m2) and Status.

[Jadual 1 menunjukkan senarai orang-orang dengan data yang dikumpul untuk mengukur BMI dan

bandingkan mana satu status indeks jisim tubuh yang muncul. Pelbagai indeks jisim tubuh telah

ditunjukkan dalam Jadual 2. Pelajar hendaklah mengira BMI dan formula yang telah diberikan;

Tugas anda adalah untuk membangunkan atucara C yang boleh mengira BMI dan Status setiap orang

yang telah ditunjukkan dalam Rajah 11. Atucara ini MESTI memaparkan Nama, Berat (kg) Tinggi (m),

BMI (kg/m2) dan Status.]

The requirements of this program are: [Keperluan-keperluan yang diperlukan untuk proram ini adalah:]

1. Define the size of array is SIZE = 3. [Takrifkan saiz array SIZE = 3.]

2. Declare an array names char Name[SIZE][20], and float Weight[SIZE],

Height[SIZE]. [Mengisytihar nama array char Nama[SIZE] [20], dan float Berat [SAIZ], Tinggi [SIZE].]

3. Declare an array named BMI to store calculated BMI. [Mengisytiharkan array yang dinamakan BMI untuk menyimpam BMI yang dikira.]

4. Declare an array named Status to store a number of selections. [Mengisytiharkan array yang dinamakan Status untuk menyimpan beberapa pilihan.]

…/13

Body Mass Index

Status BMI

1 - Thin < 20.6

2 - Normal 20.7 - 26.4

3 - Fat 26.5 - 30.9

4 - Obesity 31.0 - 45.2

5 - Danger > 45.3

Page 13: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-13-

SULIT

5. An example of program output can be seen in Figure 11. [Contoh keluaran atucara boleh dilihat dalam gambar Rajah 11.]

6. Create and write into a file name BMI.txt as seen in Figure 12. [Wujudkan dan salin ke dalam nama file BMI.txt seperti ditunjukkan dalam gambar Rajah 12.]

Sample Output: [Contoh Keluaran:]

Enter the name: Firdaus Enter the Weight(kg): 89 Enter the Height(m): 1.85 Enter the name: Gopal Enter the Weight(kg): 78 Enter the Height(m): 1.81 Enter the name: Chong Enter the Weight(kg): 90 Enter the Height(m): 1.64 Name Weight(kg) Height(m) BMI (kg/m2) Status *************************************************** Firdaus 89.00 1.85 26.00 2 Gopal 78.00 1.81 23.81 2 Chong 90.00 1.64 33.46 4 Status: 1 - Thin, 2 - Normal, 3 - Fat, 4 - Obesity, 5 - Danger Thank You

Figure 11 [Rajah 11]

.../14

Page 14: DNT 126 Basic Computer Programming [Asas Pengaturcaraan ...portal.unimap.edu.my/portal/page/portal30/Lecture...hadapan sebelum anda memulakan peperiksaan ini.] This question paper

SULIT (DNT 126)

-14-

SULIT

Name Weight(kg) Height(m) BMI (kg/m2) Status

***************************************************

Firdaus 89.00 1.85 26.00 2

Gopal 78.00 1.81 23.81 2

Chong 90.00 1.64 33.46 4

Status: 1 - Thin, 2 - Normal, 3 - Fat, 4 - Obesity, 5 - Danger

Thank You Figure 12 [Rajah 12]

(a) Show the syntax how to create the BMI.txt file to write the data. [Tunjukkan sintaksis bagaimana mewujudkan file BMI.txt untuk menyalin data.]

(2 Marks/Markah)

(b) Write the program by using if, else if....else nested loop statements to select the

range of body mass index relationship with BMI according to array. [Tulis atucara dengan menggunakan if, else if....else penyataan gelung bersarang untuk memilih

julat indek jisim badan hubungkait dengan BMI mengikut array.]

(6 Marks/Markah)

(c) Write a complete C program to calculate BMI and identify the status for each

person. [Tuliskan aturcara C yang lengkap untuk mengira BMI dan mengenal pasti status bagi setiap

orang.]

(12 Marks/Markah)