UNIVERSITI SAINS MALAYSIA. CPT111 Principles of Programming [Prinsip Pengaturcaraan]

Similar documents
UNIVERSITI SAINS MALAYSIA. CPT111/CPM111 Principles of Programming [Prinsip Pengaturcaraan]

UNIVERSITI SAINS MALAYSIA. CPT113 Programming Methodology & Data Structures [Metodologi Pengaturcaraan & Struktur Data]

CPT111/CPM111 Principles of Programming [Prinsip Pengaturcaraan]

UNIVERSITI SAINS MALAYSIA. CST333 Distributed & Grid Computing [Perkomputeran Teragih & Grid]

INSTRUCTION: This section consists of FOUR (4) questions. Answer ALL questions. ARAHAN: Bahagian ini mengandungi EMPAT (4) soalan. Jawab SEMUA soalan.

UNIVERSITI SAINS MALAYSIA. CMT322/CMM323 Web Engineering & Technologies [Kejuruteraan & Teknologi Web]

UNIVERSITI SAINS MALAYSIA

UNIVERSITI SAINS MALAYSIA. CPT211-CPM313 Programming Language Concepts & Paradigm [Konsep & Paradigma Bahasa Pengaturcaraan]

MAT181 Programming For Scientific Applications [Pengaturcaraan Untuk Penggunaan Sains]

UNIVERSITI SAINS MALAYSIA. CST331 Principles of Parallel & Distributed Programming [Prinsip Pengaturcaraan Selari & Teragih]

UNIVERSITI SAINS MALAYSIA. CST332 Internet Protocols. Architecture & Routing [Protokol, Seni Bina & Penghalaan Internet]

UNIVERSITI SAINS MALAYSIA. Peperiksaan Semester Pertama Sidang Akademik 2003/2004. September/Oktober 2003

UNIVERSITI SAINS MALAYSIA. CST232 Operating Systems [Sistem Pengendalian]

MSS 317 Coding Theory [Teori Pengekodan]

INSTRUCTION: This section consists of TWO (2) structured questions. Answer ALL questions.

MAT 181 Programming For Scientific Applications [Pengaturcaraan Untuk Penggunaan Sains]

MAT 181 Programming For Scientific Applications [Pengaturcaraan Untuk Penggunaan Sains]

UNIVERSITI SAINS MALAYSIA. CPT101 Prinsip-Prinsip Pengaturcaraan

UNIVERSITI SAINS MALAYSIA. CPT344 Computer Vision & Image Processing [Penglihatan Komputer & Pemprosesan Imej]

...a- JEE ELEKTRONIK DIGIT II. UNlVERSlTl SAINS MALAYSIA. Peperiksaan Semester Kedua Sidang Akademik FebruarVMac 2003.

MSS 318 Discrete Mathematics [Matematik Diskret]

UNIVERSITI SAINS MALAYSIA. CPT101 Prinsip-Prinsip Pengaturcaraan

UNIVERSITI SAINS MALAYSIA. CPT103 Struktur Data & Paradigma Pengaturcaraan

UNIVERSITI SAINS MALAYSIA. CST131 Computer Organisation [Organisasi Komputer]

MAT 181 Programming for Scientific Applications [Pengaturcaraan untuk Penggunaan Sains]

UNIVERSITI SAINS MALAYSIA. CCS522 Advanced Data Communication & Computer Networks [Rangkaian Komputer & Komunikasi Data Lanjutan]

UNIVERSITI SAINS MALAYSIA. CST331 Principles of Parallel & Distributed Programming [Prinsip Pengaturcaraan Selari & Teragih]

UNIVERSITI SAINS MALAYSIA. CCS521 Advanced Distributed Systems Concepts and Design [Konsep dan Reka Bentuk Sistem Teragih Maju]

UNIVERSITI SAINS MALAYSIA. CPT101 Prinsip-Prinsip Pengaturcaraan

UNIVERSITI SAINS MALAYSIA. CCS522 Advanced Data Communication and Computer Networks [Rangkaian Komputer dan Komunikasi Data Lanjutan]

INSTRUCTION: This section consists of TWO (2) structured questions. Answer ALL questions.

COMBINING TABLES. Akademi Audit Negara. CAATs ASAS ACL / 1

UNIVERSITI SAINS MALAYSIA. CST131 Computer Organisation [Organisasi Komputer]

UNIVERSITI SAINS MALAYSIA. CST131 Computer Organisation [Organisasi Komputer]

MAT 181 Programming For Scientific Applications [Pengaturcaraan Untuk Penggunaan Sains]

UNIVERSITI SAINS MALAYSIA. CPT103/CPM211 Struktur Data & Paradigma Pengaturcaraan

Pengenalan Sistem Maklumat Dalam Pendidikan

EEE 428 SISTEM KOMPUTER

UNIVERSITI SAINS MALAYSIA. CPT103/ CPM211 Struktur Data & Paradigma Pengaturcaraan

CPT212 Design & Analysis of Algorithms [Reka Bentuk & Analisis Algoritma]

MICROSOFT EXCEL. Membina Hamparan Elektronik Fungsi Hamparan Elektronik

PANDUAN PENGGUNA (SUPPLIER) MAINTAIN CERTIFICATES/SUPPLIER DETAILS SUPPLIER RELATIONSHIP MANAGEMENT SUPPLY CHAIN MANAGEMENT SYSTEM (SCMS)

UNIVERSITI SAINS MALAYSIA. CMT324 Computer Graphics & Visual Computing [Grafik Komputer & Perkomputeran Visual]

ssk 2023 asas komunikasi dan rangkaian TOPIK 4.0 PENGALAMATAN RANGKAIAN Minggu 11

EEE 348 PENGANTAR REKABENTUK LITAR BERSEPADU

PANDUAN PENGGUNA (SUPPLIER) MAINTAIN CERTIFICATES/SUPPLIER DETAILS SUPPLIER RELATIONSHIP MANAGEMENT SUPPLY CHAIN MANAGEMENT SYSTEM (SCMS)

PART A SULIT (EKT 221) BAHAGIAN A. Answer ALL questions. Question 1. a) Briefly explain the concept of Clock Gating.

UNIVERSITI SAINS MALAYSIA. CST432 Microprocessors & Embedded Systems [Mikropemproses & Sistem Terbenam]

UNIVERSITI SAINS MALAYSIA. CMT422 Multimedia Information Systems & Management [Sistem & Pengurusan Maklumat Multimedia]

INSTRUCTION: This section consists of FOUR (4) structured questions. Answer ALL questions.

PANDUAN PENGGUNA (PENTADBIR SYSTEM/SYSTEM ADMINISTRATOR) (INFOTECH, BPPF DAN POLIS

PANDUAN PENGGUNA (SUPPLIER) e-purchase ORDER FOR SERVICES

UNIVERSITI SAINS MALAYSIA. CST234 Network Programming [Pengaturcaraan Rangkaian]

UNIVERSITI SAINS MALAYSIA. CCS513 Computer Vision and Image Analysis [Penglihatan Komputer dan Analisis Imej]

INSTRUCTION: This section consists of FOUR (4) structured questions. Answer ALL questions.

UNIVERSITI SAINS MALAYSIA. CST334 Network Monitoring & Security [Pengawasan & Keselamatan Rangkaian]

FIRST TIME LOGIN & SELF REGISTRATION USER GUIDE LOG MASUK KALI PERTAMA & PENDAFTARAN SENDIRI PANDUAN PENGGUNA

UNIVERSITI SAINS MALAYSIA. CPT113/CPM213 Programming Methodology & Data Structures [Metodologi Pengaturcaraan & Struktur Data]

UNIVERSITI SAINS MALAYSIA. CMT224/CMM221 Multimedia Systems [Sistem Multimedia]

EEE348 INTRODUCTION TO INTEGRATED CIRCUIT DESIGN (PENGANTAR REKABENTUK LITAR BERSEPADU)

Lab 4 : Sorting Techniques

DOKUMEN TIDAK TERKAWAL

PANDUAN PENGGUNA (PENSYARAH)

UNIVERSITI MALAYSIA PERLIS. DNT 126 Basic Computer Programming [Asas Pengaturcaraan Komputer]

CPT211/CPM313 Programming Language Concepts & Paradigms [Konsep & Paradigma Bahasa Pengaturcaraan]

MANAGE COURSE RESOURCES LABEL TEXT PAGE URL BOOK FILE FOLDER IMS CONTENT PACKAGE

UNIVERSITI SAINS MALAYSIA. CST231/CSM331 Data Communications & Networks [Komunikasi Data & Rangkaian]

CST432 Microprocessors & Embedded Systems [Mikropemproses & Sistem Terbenam]

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

INSTRUCTION: This section consists of FOUR (4) structured questions. Answer ALL questions.

CREATING USER ID AND PASSWORD : NOTA PENTING NOTA PENTING SEBELUM MEMULAKAN PROSES CREATE USER ID & PASSWORD

Manual Pengguna. PCN Online Service Fulfillment System

INSTRUCTION: This section consists of TWO (2) structured questions. Answer ALL questions.

INSTRUCTION: This section consists of TWO (2) questions. Answer ALL questions. ARAHAN: Bahagian ini mengandungi DUA (2) soalan. Jawab SEMUA soalan.

Pengguna akan diberikan Username dan Password oleh Administrator untuk login sebagai admin/conference Manager bagi conference yang akan diadakan.

Panduan Pengguna Autodesk Education Community

Panduan Menggunakan Autoresponder FreeAutobot.com

UNIVERSITI SAINS MALAYSIA. CCS523 Computer Security & Cryptography [Keselamatan Komputer & Kriptografi]

CCS592 Advanced Algorithms and Complexity [Algoritma Lanjutan & Kekompleksan]

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CPT113/CPM213 Programming Methodology & Data Structures [Metodologi Pengaturcaraan & Struktur Data]

TEKNOLOGI, GADJET & KEIBUBAPAAN

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

INSTRUCTION: This section consists of TWO (2) structured questions. Answer ALL questions.

Registration of Supplier (ROS) TM Supplier Registration Renewal via SUS Portal (Pembaharuan Pendaftaran Pembekal TM melalui SUS Portal)

KOLEJ MULTIMEDIA JALAN GURNEY KIRI KUALA LUMPUR

INSTRUCTION: This section consists of TWO (2) questions. Answer ALL questions. ARAHAN: Bahagian ini mengandungi DUA (2) soalan. Jawab SEMUA soalan.

UNIVERSITI SAINS MALAYSIA. CST232 Operating Systems [Sistem Pengendalian]

Registration of Supplier (ROS) TM Supplier Registration Renewal via SUS Portal (Pembaharuan Pendaftaran Pembekal TM melalui SUS Portal)

UNIVERSITI SAINS MALAYSIA. CPT212 Design & Analysis of Algorithms [Reka Bentuk & Analisis Algoritma]

UNIVERSITI SAINS MALAYSIA. CIT502 Pengaturcaraan dan Kejuruteraan Perisian Berorientasi Objek

1. Akses untuk koperasi adalah melalui

EEM 312 ROBOTIK & PENGLIHATAN MESIN

LAB 5: WORKING WITH DATA

Semasa buku ini ditulis XAMPP mengandungi empat versi:

CST234 Network Programming [Pengaturcaraan Rangkaian]

INSTRUCTION: This section consists of TEN (10) structured questions. Answer ALL questions.

UNIVERSITI SAINS MALAYSIA. CMT322/CMM323 Web Engineering & Technologies [Kejuruteraan & Teknologi Web]

INSTRUCTION: This section consists of TWO (2) short answer and structured essay questions. Answer ALL questions.

INSTRUCTION: This section consists of TWO (2) structured questions. Answer ALL questions.

Transcription:

UNIVERSITI SAINS MALAYSIA First Semester Examination 2014/2015 Academic Session December 2014/January 2015 CPT111 Principles of Programming [Prinsip Pengaturcaraan] Duration : 2 hours [Masa : 2 jam] INSTRUCTIONS TO CANDIDATE: [ARAHAN KEPADA CALON:] Please ensure that this examination paper contains THREE questions in FIFTEEN printed pages before you begin the examination. [Sila pastikan bahawa kertas peperiksaan ini mengandungi TIGA soalan di dalam LIMA BELAS muka surat yang bercetak sebelum anda memulakan peperiksaan ini.] Answer ALL questions. [Jawab SEMUA soalan.] You may answer the questions either in English or in Bahasa Malaysia. [Anda dibenarkan menjawab soalan sama ada dalam bahasa Inggeris atau bahasa Malaysia.] In the event of any discrepancies, the English version shall be used. [Sekiranya terdapat sebarang percanggahan pada soalan peperiksaan, versi bahasa Inggeris hendaklah diguna pakai.]...2/-

- 2-1. (a) Suppose a, b, and sum are int variables and c is a double variable. What value is assigned to each variable after each statement executes? The statement is executed one after another. Suppose a = 3, b = 5, and c = 14.1. (i) sum = a + b + c; c /= a; (iii) b += c - a; (iv) a *= 2 * b + c; (6/100) (b) Suppose the input is 5. What is the value of alpha after the following C++ code executes? cin >> alpha; switch (alpha) case 1: case 2: alpha = alpha + 2; break; case 4: alpha++; case 5: alpha = 2 * alpha; case 6: alpha = alpha + 5; break; default: alpha--; (2/100) (c) What is the output of the following program segments? (i) int count = 5; while (count-- > 0) cout << count << " "; cout << endl; int x = 19683; int i; int y = 0; for (i = x; i >= 1; i = i / 3) y++; cout << "x = " << x << ", y = " << y << endl; Is there a relationship between the variables x and y? If yes, state the relationship? (7/100)...3/-

- 3 - (d) Write a C++ program that will prompts the user to input two numbers. If one of the numbers is 0 or less than 0, the program should output a message indicating that both numbers must be nonzero and ask the user to continue input both numbers until both numbers are nonzero. If the first number is greater than the second number, swap the number. The program then will display the sum of the even integers between and including two numbers entered by the user. In other words, if the user enters an even number, that number should be included in the sum. For example if the user enters the integer 2 and 7, the sum is 12 (2+4+6). If the user enters the integers 2 and 8, the sum is 20 (2+4+6+8). (15/100) 2. (a) Identify whether the following function calls are TRUE or FALSE for the following function prototypes: Function Prototype Function Call TRUE/FALSE i int funct1 (int,int,string); int val; val=funct1(); ii void funct2 (void); string val; val=funct2(int); iii double funct3 (void); cout << funct3(); iv int funct4(int,double,char); int val; val=funct4(4,2.5, y ); v void funct5(char,char); cout <<funct( a, b, c ); vi float funct6(float,int); cout << funct6(2.5, 3); (6/100) (b) Given the following functions int myfunction (int a, int b) for (int i=a;i<=b;i++) a=i+a; return a; void myfunction2(int ary[], int s) int i=0; while (i<s-1) ary[i]=ary[i] * 2; i++; What is the output for each of the following function calls: (i) cout << myfunction (2,2); cout << myfunction (4,6); (iii) int mydata[4]=1,2,3,4; myfunction2 (mydata,4); for (int i = 0; i<4;i++) cout << mydata[i] << ; (6/100)...4/-

- 4 - (c) Program is written to estimate the electrical power consumed by each electrical appliance in the month. Examples of electric appliances are: Lights (16 watts) Refrigerator (550 watts) TV (150 watts) Ceiling Fan (50 watts) Note: 1000 watts = 1 kwh = 1unit Your program should be able to compute the electrical power consumption charge based on the average hours consumed by each of electrical appliance every day. Finally, the program should display the electric power consumption charge for a month (Assume 1 month is 30 days). The following information is the electric tariff: 1 units = 0.21 cents/kwh For example, if there are 4 lights with 16 watts each and 8 hours usage every day. Thus, total units = (4 unit x 16 watts x 8 hours x 30 days)/1000 = 15.36 Electric charge = 15.36 x 0.21 cents/kwh = RM 3.22 Your program should accept input such as number of electrical items, watts, total hours used per day. The program should have the following functions: Accept_input function. This function does not receive any parameters. This function will accept input from a user and compute total units consumed by electric item. Finally, it returns a value of the total units consumed by electric appliance. Calculate_bill function. This function will calculate the electric charge based on the electric tariff given above. This function will receive a parameter of total units consumed by electric appliance and finally it returns a value of the electric power consumption charge. Display_info function. This function will display the electric charge which is returned by Calculate_bill function. Based on the above description, answer the following: (i) Write correct C++ statements for the following function prototypes: Accept_input Calculate_bill Display_info (3/100) Write a complete C++ program for the following functions: Accept_input Calculate_bill Display_info (15/100)...5/-

- 5 - (iii) Given the following C++ main() program segment: main() float total_units, electric_charge; total_units= // function call for Accept_input function electric_charge= // function call for Calculate_bill function // function call for Display_info function // end of main Write the correct C++ statements for the above function calls. (5/100) 3. (a) (i) Write a C++ statement to dynamically allocate memory for an array whose size is not known until runtime. Write a statement to reclaim the memory allocated below: int* num = new int; *num = 10; (4/100) (b) What is the content of myarray after the execution of the following code. int ctr = 0; int myarray[3]; for ( int i = 0; i < 3; i++) myarray[i] = ctr; ctr = ctr + i; (3/100)...6/-

- 6 - (c) What is the output of each of the following program segments? (i) int num[3]; for ( int cnt = 1; cnt < 3; cnt++) num[cnt] = cnt + 1; cout << num[cnt]; char string1[] = How are you? ; cout << string is : << string1 << endl; for ( int i = 0; string1[i]!= \0 ; i++) cout << string1[i] << _ ; (iii) int ch = 100; int * ptr = &ch; cout << Value stored at pointer ptr is : cout << *ptr<< endl; (iv) int array [3][4] =1,2,3,4,2,3,4,5,3,4,5,6; cout << Array contains : << endl; for ( int i = 0; i < 3; i++) for ( int j = 0; j < 4; j++) cout << array[i][j] << ; cout << endl; (8/100) (d) State and correct the error(s) for the following questions: (i) The for loop should initialize all array values to -1: int array [10]; for (int i = 0; i < 9; i++) array[i] = -1; The for loop should print all array values: int array [10]; for (int i = 0; i <= 10; i++) cout << array[i];...7/-

- 7 - (iii) The following program displays the reverse of row and column of a 3 x 3 array: int array[3][3] = 1,2,3,4,5,6,7,8,9; for ( int I = 0; I < 3; I ++) for ( int j = 0; j < 3; j++) cout << array [i][j]; cout << endl example output : 1 4 7 2 5 8 3 6 9 (5/100) (e) (i) Write a function named Reverse that takes as its argument an array of floating values and the size of the array. The function must reverse the values in the array. example input array : 5.8 2.3 4.5 7.8 output array : 7.8 4.5 2.3 5.8 (5/100) Write a C++ segment code that will (A) (B) create a 3 by 3 array named Array. initialise the array declared in (A) with the value according to row index: Array : 1 1 1 2 2 2 3 3 3 (C) Update the array in (B) by adding the row index to the original value. Before : 1 1 1 2 2 2 3 3 3 After : 2 2 2 4 4 4 6 6 6...8/-

- 8 - (iii) Sum each column and save in one dimensional array called Total_Col and sum each row and save in a one dimensional array called Total_Row. (10/100)...9/-

KERTAS SOALAN DALAM VERSI BAHASA MALAYSIA - 9-1. (a) Anggapkan a, b, dan sum adalah pemboleh ubah int dan c adalah pemboleh ubah double. Apakah nilai yang diumpukkan kepada setiap pemboleh ubah selepas setiap kenyataan berikut dilaksanakan. Setiap kenyataan ini dilaksanakan secara berturutan. Anggapkan a = 3, b = 5, dan c = 14.1. (i) sum = a + b + c; c /= a; (iii) b += c - a; (iv) a *= 2 * b + c; (6/100) (b) Anggapkan input adalah 5. Apakah nilai alpha selepas kod C++ berikut dilaksanakan? cin >> alpha; switch (alpha) case 1: case 2: alpha = alpha + 2; break; case 4: alpha++; case 5: alpha = 2 * alpha; case 6: alpha = alpha + 5; break; default: alpha--; (2/100) (c) Apakah output bagi segmen atur cara berikut? (i) int count = 5; while (count-- > 0) cout << count << " "; cout << endl;...10/-

- 10 - int x = 19683; int i; int y = 0; for (i = x; i >= 1; i = i / 3) y++; cout << "x = " << x << ", y = " << y << endl; Adakah terdapat perhubungan di antara pemboleh ubah x dan y? Jika ya, nyatakan perhubungan tersebut. (7/100) (d) Tulis satu atur cara C++ yang akan meminta pengguna supaya memasukkan dua nombor. Jika salah satu dari nombor tersebut adalah 0 atau kurang dari 0, atur cara perlu keluarkan mesej memberitahu bahawa kedua-dua nombor mestilah lebih besar dari kosong, dan meminta pengguna memasukkan semula kedua-dua nombor, sehingga kedua-dua nombor adalah bukan sifar. Jika nombor pertama lebih besar daripada nombor kedua, tukar ganti dua nombor tersebut. Atur cara seterusnya akan memaparkan hasil tambah semua integer genap di antara dua nombor tersebut, termasuk kedua-dua nombor yang dimasukkan oleh pengguna. Dengan kata lain, jika pengguna memasukkan nombor genap, nombor tersebut mesti dimasukkan sekali dalam jumlah. Contohnya, jika pengguna memasukkan integer 2 dan 7, jumlah adalah 12 (2+4+6). Jika pengguna masukkan integer 2 dan 8, jumlah adalah 20 (2+4+6+8). (15/100) 2. (a) Tentukan sama ada panggilan-panggilan fungsi berikut adalah BETUL atau SALAH Prototaip Fungsi Panggilan Fungsi BENAR/SALAH i int funct1 (int,int,string); int val; val=funct1(); ii void funct2 (void); string val; val=funct2(int); iii double funct3 (void); cout << funct3(); iv int funct4(int,double,char); int val; val=funct4(4,2.5, y ); v void funct5(char,char); cout <<funct( a, b, c ); vi float funct6(float,int); cout << funct6(2.5, 3); (6/100)...11/-

- 11 - (b) Diberi fungsi-fungsi berikut: int myfunction (int a, int b) for (int i=a;i<=b;i++) a=i+a; return a; void myfunction2(int ary[], int s) int i=0; while (i<s-1) ary[i]=ary[i] * 2; i++; Apakah paparan untuk setiap panggilan fungsi berikut: (i) cout << myfunction (2,2); cout << myfunction (4,6); (iii) int mydata[4]=1,2,3,4; myfunction2 (mydata, 4); for (int i = 0; I<4;i++) cout << mydata[i] << ; (6/100) (c) Atur cara ditulis untuk menganggar caj elektrik yang diguna oleh setiap peralatan elektrik dalam sebulan. Contoh-contoh peralatan elektrik adalah: Lampu (16 watt) Peti sejuk (550 watt) TV (150 watts) Kipas siling (50 watt) Nota: 1000 watt = 1 kwh = 1 unit Atur cara anda perlu mengira caj penggunaan kuasa elektrik berdasarkan jam purata yang digunakan oleh setiap peralatan elektrik setiap hari. Akhirnya atur cara tersebut perlu memaparkan caj penggunaan kuasa elektrik pada bulan tersebut (Anggapkan 1 bulan adalah 30 hari). Maklumat berikut adalah tarif elektrik: 1 units = 0.21 sen/kwh Contohnya, jika terdapat 4 lampu dengan 16 watt setiap satu dan 8 jam kegunaan setiap hari. Maka jumlah unit = (4 unit x 16 watt x 8 jam x 30 hari)/1000 = 15.36 Caj elektrik = 15.36 x 0.21 sen/kwh = RM 3.22 Atur cara anda perlu menerima input seperti bilangan peralatan elektrik, watt dan jumlah jam yang digunakan setiap hari. Atur cara tersebut perlu mempunyai fungsi-fungsi berikut:...12/-

- 12 - Fungsi Accept_input. Fungsi ini tidak menerima apa-apa parameter. Fungsi ini akan menerima input dari pengguna dan mengira jumlah unit yang digunakan oleh peralatan elektrik. Akhirnya fungsi ini akan kembalikan nilai jumlah unit yang digunakan oleh peralatan elektrik. Fungsi Calculate_bill. Fungsi ini akan mengira caj elektrik berdasarkan kepada tariff elektrik yang diberi di atas. Fungsi ini akan menerima suatu parameter iaitu jumlah unit yang digunakan oleh peralatan elektrik dan akhirnya fungsi ini kembalikan nilai caj penggunaan kuasa elektrik Fungsi Display_info. Fungsi ini akan memaparkan caj elektrik yang dikembalikan oleh fungsi Calculate_bill. Berdasarkan kepada penerangan di atas, jawap berikutnya: (i) Tulis kenyataan-kenyataan C++ yang betul untuk prototaip fungsi-fungsi berikut: Accept_input Calculate_bill Display_info (3/100) Tulis atur cara C++ yang lengkap untuk fungsi-fungsi berikut: Accept_input Calculate_bill Display_info (15/100) (iii) Diberi keratan atur cara main() C++ berikut: main() float total_units, electric_charge; total_units= // panggilan fungsi untuk fungsi Accept_input electric_charge= // panggilan fungsi untuk fungsi Calculate_bill // panggilan fungsi untuk fungsi Display_info // tamat main Tulis kenyataan-kenyataan C++ yang betul untuk panggilan fungsi-fungsi di atas. (5/100)...13/-

- 13-3. (a) (i) Tulis satu kenyataan C++ untuk menyediakan memori secara dinamik kepada satu tatasusunan yang tidak diketahui saiznya sehingga masa larian. Tulis satu kenyataan untuk mengambil semula memori yang telah disediakan di bawah: int* num = new int; *num = 10; (4/100) (b) Apakah kandungan myarray selepas perlaksanaan kod berikut. int ctr = 0; int myarray[3]; for ( int i = 0; i < 3; i++) myarray[i] = ctr; ctr = ctr + i; (3/100) (c) Apakah output bagi setiap segmen atur cara berikut? (i) int num[3]; for ( int cnt = 1; cnt < 3; cnt++) num[cnt] = cnt + 1; cout << num[cnt]; char string1[] = How are you? ; cout << string is : << string1 << endl; for ( int i = 0; string1[i]!= \0 ; i++) cout << string1[i] << _ ; (iii) int ch = 100; int * ptr = &ch; cout << Value stored at pointer ptr is : cout << *ptr<< endl;...14/-

- 14 - (iv) int array [3][4] =1,2,3,4,2,3,4,5,3,4,5,6; cout << Array contains : << endl; for ( int i = 0; i < 3; i++) for ( int j = 0; j < 4; j++) cout << array[i][j] << ; cout << endl; (8/100) (d) Nyatakan dan betulkan kesalahan(-kesalahan) bagi setiap soalan berikut: (i) Gelung for perlu nilai awalkan semua nilai tatasusunan kepada -1: int array [10]; for (int i = 0; i < 9; i++) array[i] = -1; Gelung for perlu cetak semua nilai tatasusunan: int array [10]; for (int i = 0; i <= 10; i++) cout << array[i]; (iii) Atur cara berikut memaparkan nilai terbalik bagi baris dan lajur tatasusunan 3 x 3: int array[3][3] = 1,2,3,4,5,6,7,8,9; for ( int I = 0; I < 3; I ++) for ( int j = 0; j < 3; j++) cout << array [i][j]; cout << endl contoh output : 1 4 7 2 5 8 3 6 9 (5/100)...15/-

- 15 - (e) (i) Tulis satu fungsi bernama Reverse yang menerima sebagai argumen satu tatasusunan yang bernilai apung dan saiz tatasusunan tersebut. Fungsi perlu mengterbalikkan nilai dalam tatasusunan. contoh tatasusunan input : 5.8 2.3 4.5 7.8 tatasusunan output : 7.8 4.5 2.3 5.8 (5/100) Tulis satu kod segmen C++ yang akan (A) (B) mencipta tatasusunan 3 x 3 bernama Array. menilai awalkan tatasusunan yang diisytiharkan dalam (A) dengan nilai mengikut indeks baris: Tatasusunan : 1 1 1 2 2 2 3 3 3 (C) Kemaskini tatasusunan dalam (B) dengan menambahkan nilai indeks baris kepada nilai asal. Sebelum : 1 1 1 2 2 2 3 3 3 Selepas : 2 2 2 4 4 4 6 6 6 (iii) Jumlahkan setiap lajur dan simpan ke dalam tatasusunan satu dimensi bernama Total_Col dan jumlahkan setiap baris dan simpan ke dalam tatasusunan satu dimensi bernama Total_Row. (10/100) - ooooooo -