You are on page 1of 21
SULIT UNIVERSITI MALAYSIA PERLIS Peperiksaan Semester Kedua Sidang Akademik 2008/2009 20 April 2009 DKT 121 - Fundamental of Computer Programming [Asas Pengaturearaan Komputer] Masa: 3 jam Please make sure that this paper has TWENTY ONE (21) printed pages including this front page before you start the examination. [sua pestilan keris soalan ini mengandungi DUA PULUH SATU (21) muka surat yang bereetci termasuk mua hadapan sebelum anda memulakan peperiksaan int] ‘This question paper has SEVEN (7) questions. Answer FIVE (5) questions. [Kents soalan ini mengandungi TUJUH (7) soalan. Jawab LIMA (5) soatan.] Section A : This section has THREE (3) questions. Answer ALL questions. (60 marks) [Bohagian A: Bohagian ini ada TIGA (3) soalan, Jawab SEMUA soalan. (60 markah)} Section B : Answer any ONE (1) question. (20 marks) (Bahagian B+ Jawab mana- mana SATU (1) soaian. (20 markah)| Section C : Answer any ONE (1) question, (20 marks) TBakagian C Jawab mana mana SATU (1) soalan. (20 markah)] SULIT (KT 121) Section A(60 marks) [Bahagian A(60 markah)| This section has THREE (3) questions. Answer ALL questions. (Bohagian ini ada TIGA (3) soalan, Jawab SEMUA soalan.) Question 1 Soalen 1 a) State FOUR (4) data types available in C language. [Nyatakan EMPAT (4) jenis data yang terdapat di dalam bahasa C.] [4 Marks/Markat] b) _ Identify and correct the errors in each of the following statements {Kenalpasti dan betulkan kesalahan untuk setiap pernyataan berikut:) i. printf(The sum is d\n”, &sum); ii, —_scanf{“f ", &eresistance, current); iii, : Osi < 10h); printf(“a is less more than 8\n"); (a>8) printf(“a is equal to or more than 8\n”); [5 Marks/Merkak}] ©) Write a C statement to declare the following variables with the given data type. (Tuliskan satu penyataan C untuk mengisytihar pembolehubak-pembolehubah berikut dengan Jenis data yang diberi.| i, sumas double ii, velocity as float iii, number as integer iv. choice as char vy. volume as long integer {5 Marks/Markat] Figure 1a shows a simple programming to compare a value of an integer variable a with value of 10. The program displays an appropriate message whether the value assigned to a is greater than or less than 10. [Rajah di bowah menunjukhan satu aturcara ringkas yang bertjuan untuk membandingkon satu nlal pembolehubah integer @ dengan nila 10. Aturcara ini memaparkan mese) bersesuaian sama ‘ada nilai yang ditumpukan kepada a lebih Besar atau Kurang daripada 10.] (KT 121) Hinclude int mainQ { | int a; printf{"Please enter any number: "); scani("%d",&a); printf("%d is %s than10",a,a>=10?"greater":"less"); return 0; Figure la (Rajah 1a} Draw and complete the contents of a given flowchart in Figure 1b based on information given in question 1d and the program in Figure 1a. {Lukis dan Lengkapkan kandungen carta alir yang telah diberikan dalam Rajah 1b berdasarkan ‘maklumat yang dibert di dalam soalan Id dan aturcara Rajah 1a} [6 Marks/Atarkah] Figure 1b [Rajah 16] ANA SULIT SULIT (KT 121) Question 2 Soatan 2 a) _ List THREE (3) types of while loops. [Senaratkan TIGA (3) jenis gelung while} {3 Marks/Markah] b) State the definition of loop. (Nyatakan definisi gelung:] [2 Marks/Aferkah] ¢) Find the output values of the program segment in Figure 2a if input value of x=5 [Cari hasit keluaran segmen aturcara dalam Rajah 2a jika nilai masukan x printf (“Enter an integer: ” scanf (“%d”, &x); product = x; count = 0; while (count <3) { printf (“%d\n”, product); product = product * x; ‘count = count + 1; } Figure 2a (Rajah 2a] [4 Marks/Markah] 4) Find the output of the program segment in Figure 2b. (Cari hasil kelwaran segmen aturcara dalam Rajah 26.) inti for (x= 10; x >=25 i) { printi(“%dle i); } Figure 2b (Rajah 26] {3 Marks/Markak] onS/21 SULIT (DKT 121) The function prototype and function call is shown in the program segment in Figure 2c. Write an appropriate TWO (2) function definitions by using a function name add and subtract. {Prototaip fangs! dan panggilan fungst ditunjutkan dalam seymen aturcara di dalam Rajah 2c Tuliskan DUA (2) definisi fungsi yang bersesuaian dengan menggunakan nama furgsi add dan suberact.} include int add(int,int); Jifanction prototype int subtract(int,int); //fanction prototype void print_result(int); /Hfunction prototype int main( ) { int numl,num2, answer; char op; printf{“Enter two numbers and operator:"); scani(“%d %d %s”, &num, &num2, &op); ewiteh(op) ‘function call { case ‘+':answer=add(numl num2);break; case ‘-"answer=substract(num 1,num2);break; default: printf(“Invalid operator”);exit(0); } print_result(answer); return 0; } /Munction definition for function add return (sum); } //fanction definition for function subtract { return (sub); } | a igure 2c (Rajah 2c] [8 Marks/Markah] 26/21 SULIT SULIT (OKT 121) Question 3 Soatan 3 a) Given the program in Figure 3a. Answer the following questions. wl, w2 and v are the variables and pv is the pointer variable [Diberi aturcara di dalam Rajah 3a. Jawab soalan-soalan beriku. ul, u2 and v adalah ‘pemboleubah dan pv adalah pembolelubah pensnjuk.] | #include int main() { int ul, u2; int v= 7; int *pv: ul =2* (+5); py=&v; u2=2* (“py +5); print" ul=%d u2= %d", ul, u2); xpv =9; /*reset the value of v indirectly*/ printi("\n * return 0; Figure 3a (Rajah 30) Find the value of wl and u2 in Line a. [Cari nilai ui dan u2 di baris a.) (2 Marks/a@arkak] (OKT 121) ii, Will the value of v change in Line b? [Adakah nitai v berubah di baris 6?) [2 Marks/Markak] iii, Display the value of *py and v in Line c. [Papar nilai *pv dan v selepas nilaiv di baris | (2 Marks/Aarkan] b) Define the following file modes: [Definisikan mod-mod fail berikut:] ior iii, wt [3 Marks/Markoh] ©) Write a simple sequential access file program that reads all data from input file ‘meteorologicaldata.txt’ below. The text file line contains an integer ID site number followed by wind speed in knots, day and temperature in degree Celsius. Display all the input on monitor screen. Figure 3b is the data that is inside the file and it is displayed using command printf [Tutis sate program mudah untuk mengakses fail yang membaca semua data dari fait input ‘meteorologicaldata.txt’. Baris fail teks mengandungi integer bagi nombor ID tapak diikuriofeh elajuan angin dalam knot, hari dan cuaca dalam darjak Celsius. Paparkan semua input di skrin ‘monitor. Rajah 3b adalah data yang berada di dalam fail dan ia dipaparkan dengan ‘menggunakan perinich prinaf 1001 10 3 25 sure 3b [Rajah 36] You are given a flowchart in Figure 3c and guideline to construct the program in Figure 3d, {Anda diberi carta alr di Rajah 3¢ dan panduan untuk membuat program di dalam Rajah 3d.) ++ 8/21 SULIT Sf (DKT 121) if [Open file | Read site id, , wind specd, day and temperature from file Print site id, , wind speed, day and temperature to monitor sereen Figure 3c Rajah 31 9/21 SULIT SULIT (OKT 121) se #include int main) { int site_id, wind_speed, day, temp; //declare site id, wind speed, Jiday and temperature Misytihar site id, wind speed, Wday dan temperature Héeclare a file pointer ‘Msytihar pencing fail Hopen a file bute fail /Iread from file : site id, wind speed, day /and temperature (foaca dar fal : site id, wind spe Memperature day dan print to screen | printf(“%d\t%d\t Yed\t %dit %d”,site_id,wind_speed,day,temp); Heetak ke skrin /iclose file oacup fait retum 0; SY Figure 3d (Rajah 34) [5 Marks/Markat] ©) Write a program using array to store integers ranging from 0 to 10 and calculate the cubes of those integers. The output of the program should be as in Figure 3¢, Use Figure 3f as part of the program segment. [Tuliskan satu aturcara yang mengginakan tarasusunan untuk menyimpan integer daripada O ‘hingga 10 dan kirakan kuasa tiga integer-integer tersebut. Hasil keluaran aturcara mestilak seperti dalam Rajah 3e. Gunakan Rajah 3f sebagai sebahagian segmen aturcara.) ++10/21 SULIT (KT 121) -10- "The cubes of integers from 0 to 10 are: 0 1 2 | 7 64 Figure 3e [Rajak 3e] Hinclude int main( ) { int cube(11}, is declare variable cube ‘isytihar pembolehubah cube for i=0;i< 11; i++) Mop 11 times : ulang 1 kali . Htwaite the mathematical expression to calculate cubes lis pernyataan matematik untuk Mmengira kuasa tiga printf (“The cubes of integers from 0 to 10 are:\n”);, for (i=0; i<11; i++) IMprint the cube values of the integers from array cube Heetak nas kuasa tiga integer daripada ‘Matasusunan cube return 0; Figure 3f [Rajah 3/1 [6 Marks/Markah] 11/21 SULIT ae Section B (20 marks) [Bahagian B (20 markah)| This section has TWO (2) questions. Choose any ONE (1) question. [Bahagian ini ada DUA (2) soalan. Pith mana-mana SATU (1) soalan.| Question 4 Soalan 4 Consider a car that accelerates to a velocity of +20nv/s from an initial rest for a time period of t1 seconds and moves with a constant velocity of +20n/s over the next (2 seconds. Finally, the car decelerates to Om/s over (3 seconds. This case can be represented by a velocity versus time graph in Figure 4a. The slope of velocity versus time represents the acceleration and the area bound by the line and the axes represents the displacement [Pertimbangkan sebuah Kereta yang memecut sehingga halaju sebanyak +20m/s daripada Keadaan rohat untuk fempok masa sebanyak 1! saat dan bergerak dengan halaju fetap sebanyak *20m/ unnek tempok masa t2 saat yang seterusnya. Akhirnya kereta itu menyahpecut sehingga Om/s untube 13 saat. Kes ini boleh diwakilkan dengan graf halaju lawan masa di dalam Rajah 4a. Kecensnan haigj lawan masa mewakili pecutan dan luas yang dibatasi oleh garis dan juga paksi ‘mewakilijarak perjalanan.| Velocity (mv/s) Figure 4a [Rajah 4a] SULIT (KT 121) az ‘The formula to calculate the acceleration, a and displacement, d are given as follows: (Formula untuk mengira pecutan, a dan jarak perjalanan, d adalah diberikan seperti beri] gate rt, 1 d= sth*(b, +6, 3 ( ») where: (di mana:] v= final velocity fataju akhir} Vo= initial velocity halaju awl} t = final time (masa akhir] t= initial time {masa awal) h= height [singed bi=base 1 (eapak 1) byrbase 2 (copak 2] The sample output is given in Figure 4b: (Contoh hasil keluaran diberikan dalam Rajah 4b:] Enter velocity! (mm/s) : 20 Enter velocity2 (m/s) ; 20 Enter velocity3 (m/s) : 0 Enter t1 (sec) 2 Enter t2 (sec) 4 Enter (3 (sec) 8 ‘The acceleration for area A is 10.00 m/s/s The acceleration for area B is 0.00 mi/s/s The acceleration for area Cis -5.00 mis/s Total displacement is 100.00m Figure 4b (Rajah 45] oe 3/21 SULIT (DKT 121) as Draw a flowchart on how to enter input velocity v/, v2, v3 and time interval t1, «2 and £3. Calculate the acceleration for area A, B and C and total displacement of the car. Display the calculated output. [Luliskan satu earta alir yang menunjukkan bageimana memasukkan nilai masukan ‘halapa vi, v2, v3 dan masa tt, 2 dan t3. Kira pecutan bagi luas A, B dan C dan jarak perjalanan bagi kereta itu Paparkan has keluaran yang telah dikira) {5 Marks/Afarkak] Write C statements based on the above problem statements using the following guidelines: Trulis penyataan C berdasarkan pernyatcan masalah di atas menggunakan panduan-panduan ert Declare the variables v1, v2, v3, #1, £2, #3 as integer data type and al, a2, 43, d as float data type. [iotihar pembotch ubah v1, v2, v3, 1,12, sebagai jenis data nombor bulat dan a ‘a2, a3 dan d sebagai jenis data nombor perpulukan | [3 Marks/Markat] Prompt the user to enter values of vl, v2, v3, 11, 12 and 13. Read those values from the keyboard and store the values entered. [Tunjuk mesej kepada pengguna untuk memasukkan nilaivl, v2, v3, tl, £2 dan 13 Baca nilai-nilai ita dari papan kekunci dan simpan nilai yang telak dimasukkan.] [6 Marks/Markah] Calculate the acceleration, a1, a2 and a3 by using the given formula [Kira pecutan al, a2 dan a3 dengan menggunakan formula yang diberikan.| [3 Marks/Markah] iv. Calculate the total displacement, d of the car by using the given formula. [kira jumlah jarak perjalanan kereta, d dengan menggunakan formula yang diberika) [2 Marks/Markad] y. Print the acceleration, a, a2 and a3 and total displacement, d. {Paparkan pecutan at, a2 dan a3 dan jumtah jarak perjalanan,d.] {1 Mark/atarkak] 4/21 SULIT suur (KT 121) Question 5 Soalan 5 The inputs of a C program are a series of integers. The program reads the inputs three times from the user and passes them one at a time to function even which uses the remainder operator to determine if an integer is even. The function should take an integer argument and retum 1 if the integer is even and 0 otherwise. [Nilai masukan bagi satu aturcara adalah satu jujukan integer. Aturcara ini akan membaca nilai masukan sebanyak tiga kalt daripada pengguna dan melepaskan mereka sate persatu pada satu masa kepada fangs! even di mana menggunakan operator baki untuk menentukan samada integer adalah genap. Fungsi Ssepatutnya dapat ambil integer dan memulangkan { jika integer adalah gonap dan 0 sebaliknya.| The sample output is given in Figure 5 below: {Contoh has ketuaran diberikan dalam Rajah 5 di bawah:) Enter an integer: 7 7 is not an even integer Enter an integer: 6 6 is an even integer Enter an integer: 10000 10000 is an even integer Figure 5 [Rajah 5} a) Draw a flowchart on how to input a series of integers, determine whether the input is even or not and then display the result. [Lukis satu carta alir yang menunjukkan bagaimana satu jyjukan integer diinput, penentuan samada input adalah genap atau tidak dan papar jawapar.] [5 Marks/Aferkat] b) Write C statements based on the above problem statements using the following guidelines: [Tulis penyataan C berdasarkan pernyaiaan masiah di atas menggunakan panduan-panduan brik] i. Declare a function prototype, named even. UUsytthar satu prototaip fungsi dengan nama even] (1 Mark/starkai] ii. Declare a current input named x and loop counter named i. {Usytihar input semasa bernama x dan pembilang gelung bernama i} [2 Marks/aarkas} e521 SULIT SULIT (KT 121) AS Write a loop statement to input integers for three times. [Tis penyataan gelung untuk memasukian nitat masukan sebanyak tiga kali] {2 Mark/atarkat] Read an integer from the keyboard and store the value entered in integer variable x. [Baca integer dari papan kelunci dan nitai disimpan dalam pembolehubah x.) [1 Mark/atarkah] ‘Write an even function definition for the above problem. The funetion will return true (1) if the passing value is even. [Tulis definisi fungsi even bagi masalah di atas. Fungsi ini akan kembalikan betal (1) jika a adalah genap] [4 Marks/Merkak] Check the condition of the input whether it is an even number or not by using if...else statement and calling the even function. If number is even, print “Number is an even integer”, else print “Number is not an even integer”. [Periksa syarat bagi nilai masukan itu sama ada ia adalah nombor genap atoupun tidak dengan menggunakan pernyataan ¥f...else dan memanggil fungsi even. Jka nombor ‘adalah genap, paparkan Number is an even integer”, lain papar “Number is not an even integer.) [4 Marks/Markeh] Write a statement to indicate the program ended successfully. [Tutis penyaraan untuk menyatakan oturcara sudah berakhir.] [1 Mark/aarkat| ‘SULIT @KT 121) -16- Section C (20 marks) [Behagian C(20 markah)} This section has TWO (2) questions. Choose any ONE (1) question, [Bahagian ini ada DUA (2) soalan. Piltk mana-mana SATU (1) soalan.| Question 6 Soalan 6 Resistors can be connected either in parallel or in series for any electrical or electronic circuit. The formula to calculate the total resistance for resistors that are connected in series is given by: (Periniang boleh disambungkan samaada dalam bentuk selari atau sesiri untuk mana — mana litar elekirtk ‘atau elektronik. Formula untuk mengira jumlah keselurukan bagi periniang yang disambungkan secara sesiri adalah diberikan seperti berikut:] op HR +R, +R +4 RK, ‘The formula to calculate the total resistance for resistors that are connected in parallel is given by: [Formula untuk mengira jumiah keselurukan bagi perinang yang disambungkan secara selari adalah iberikan seperti beriku:} As an engineer, you are given a task to come up with a program that can calculate the total resistance for the resistors. User should be able to select the type of connection whether series or parallel as shown in Table 1. The program also allows the user to give input the number of resistors. [Sebagai seorang jurutera, anda diberikan tugas untuk menyediakan program bagi mengira jumlahk eseluruhan rintangan bagi perintang. Pengguna boleh memilih jeris sambungan sama ada sesiri atow selari seperti yang ditunjukkan pada Jadual 1. Program tersebut juga membenarkan pengguna ‘memasukkan jumlah perincang yang dikehendaki ] Table 1 (Vaduat 1} Type | Connection 1_| Parallel 2 | Sesiri ew TA SULIT SULIT (DKT 121) “7 The programs reads type of the resistor connection and determine the resistance based on type and calculate subtotal and total resistance. (Program ini membaca jenis zambungan periniang don tentukan jumlah keseluruhan rintangan erdasarkan pada jenis dan mengira jumlah kecil dan jumlah keseluruhan perintang.] The formula to calculate subtotal for parallel connection is [Formula untuk mengira jumtah kecil bogi sambungan selart ialah :| rSub = Sub + /rVal; The formula to calculate subtotal for series connection is : (Formula untuk mengira jumlah kecil bagi sambungan sesiri ialah : | rSub = rSub + rVal; The formula to calculate total for parallel connection is : (Formula untuk mengira jumlah keselurukannya bagi sambungan selari ialah : | Parallel = 1/rSub The formula to calculate total resistance for series connection is: (Formula untuk mengira jumlah keseluruhan bagi sambungan sesiri alah :] rSeries = rSub; ‘The program should first display the type of connection from Table 1 and then ask the user to choose the type of connection. The program will continue asking the user to give the number of resistor and value of the resistor. You are advised to use switch and for concept to solve this problem. The example output is given in Figure 6. [Program int pada permulaanya perlu memaparkan jenis sambungan seperti pada Jadual 1 dan kemudiannya bertanya pada pengguna untuk memilth jenis sambungan. Program ini akan terus meminta ‘penggunamemberikan jumiah bilangan perintang dan nilai perintang. Anda dinasihatkan untuk mmenggunatan konsep switch dan for bagi meyelesatkan masalah ini. Contoh sampel hast! keluaran diberikan di dalam Rajah 6.1 oo 8/21 SULIT SULIT KT 121) ae | Connection type Type Connection 1 Parallel 2 Series Choose connection type: 2 ‘Number of resistor: 2 Enter Ri value: 0.47 fer R2 value: 470 Value of 2 series resistor is: 470.47 ohm Lo Figure 6 (Rajah 6) a) Drawa flowchart on how to solve the problem. [Lukiskan satu carta alir untuk menyelesatkan masalah.| [5 Marks/farkah] b) Write a complete C program to solve the problem. [Tuis satu aturcara C lengkap untuk menyelesaikan masalch:} [1S Marks/Aferkan] 219/24 SULIT SULIT (DKT 121) -19- Question 7 Soalan 7 Write a program to compute the Mean and Standard Deviation of an array of data. The program should prompt the uscr to enter inputs and store the inputs in the array. The outputs which are mean and standard deviation should be displayed on the screen. [Tultshen satu cturcara untuk mengira purara dan sisthan piawai bag! satu data tatasusunan. Aturcara berkenaart hendaklah ‘memaparkan mese} kepada pengguna untuk memasukkan nilai masukan dan ‘menyinpan nilai masukan ke dalam tatasuswnan. Hasil keluaran iaitu purata dan sisihan piawai hendaklak dlipaparkan ke atas skrin.] Mean is computed as: [Purata dikira menggunakan formula berikut:| For example: [Sebagai contoh.} For three numbers, add them and divide them by 3: {Bagi tiga nombor, tambahkan nombor-nombor isu dan bakagikan dengan 3:] 7 Gat) 3 Standard Deviation is computed as: [Sisihan piawai dikira menggunakan formula berikut:] Wao ay o= LS G-9 ya ) where x is mean, [di mana x adalah purata.] For example: [Sebogai cone} Suppose we want to find the standard deviation of the data set consisting of the values 3, 7,7 and 19. The followings are the steps on how to calculate the standard deviations [Unda iia ingin mencari sisshan pawal bagt set data yang mengandungi nilai-ilai 3, 7. 7, dan 19 Berikut adalah langhal-langhak untuk mengira sisihan piawat:) ++-20/21 SULIT SULIT (OKT 121) 20- Step 1 : Find the arithmetic mean (average) of 3, 7, 7, and 19 [Langkah I: Cari purata aritmettk bagi 3, 7, 7 dan 19} 747419) _ 4 9 Step 2 : Find the deviation of each number from the mean {Langkak 2: Carl sisihan piawai untuk setiap nombor daripada purata] Step 3 : Square cach of the deviations [Langkah 3: Kuase duakan setap sisihan} (6)? =36 @y = (ay =4 (10)? =100 Step 4 : Find the mean of those squared deviations (Langkah 4: Cari purata bagi nila kuasa dua sisihan) 6+4+4+100) _ 4 36 Step 5 : Square root the mean [Langkah § Punca kuasa duakan purata} 36 =6 So, the standard deviation of the set is 6 [adi sisihan plawai bogi set ini adalah 6.) 2) Drawa flowchart from the above problem statements. {Lukis sone carta alir daripada pernyataan masalah di atas.] [5 Marks/Markat] SULIT (KT 121) 21 b) Write a complete C program to solve the problem [Tulis saru aturcara C lengkap untuk menyelesaikan masalak } [15 Marks/afarkah]

You might also like