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

Similar documents
CPT111/CPM111 Principles of Programming [Prinsip Pengaturcaraan]

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

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

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

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

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

UNIVERSITI SAINS MALAYSIA

UNIVERSITI SAINS MALAYSIA. CST232 Operating Systems [Sistem Pengendalian]

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

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

UNIVERSITI SAINS MALAYSIA. CST131 Computer Organisation [Organisasi Komputer]

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

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

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

MSS 317 Coding Theory [Teori Pengekodan]

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

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

UNIVERSITI SAINS MALAYSIA. CST131 Computer Organisation [Organisasi Komputer]

MAT181 Programming For Scientific Applications [Pengaturcaraan Untuk Penggunaan Sains]

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

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

UNIVERSITI SAINS MALAYSIA. CST131 Computer Organisation [Organisasi Komputer]

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

UNIVERSITI SAINS MALAYSIA. CPT101 Prinsip-Prinsip Pengaturcaraan

MSS 318 Discrete Mathematics [Matematik Diskret]

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]

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

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

UNIVERSITI SAINS MALAYSIA. CPT101 Prinsip-Prinsip Pengaturcaraan

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

EEE 428 SISTEM KOMPUTER

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. CPT103/CPM211 Struktur Data & Paradigma Pengaturcaraan

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

UNIVERSITI SAINS MALAYSIA. CPT103 Struktur Data & Paradigma Pengaturcaraan

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

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

EEE 348 PENGANTAR REKABENTUK LITAR BERSEPADU

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

UNIVERSITI SAINS MALAYSIA. CST234 Network Programming [Pengaturcaraan Rangkaian]

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

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

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

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

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

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

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

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

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

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

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

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

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

UNIVERSITI SAINS MALAYSIA. CIT502 Pengaturcaraan dan Kejuruteraan Perisian Berorientasi Objek

Manual Pengguna. PCN Online Service Fulfillment System

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

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

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

MICROSOFT EXCEL. Membina Hamparan Elektronik Fungsi Hamparan Elektronik

UNIVERSITI SAINS MALAYSIA. CPT341 Software Design & Architecture [Reka Bentuk & Seni Bina Perisian]

CCS592 Advanced Algorithms and Complexity [Algoritma Lanjutan & Kekompleksan]

PANDUAN PENGGUNA (SUPPLIER) e-purchase ORDER FOR SERVICES

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

CST432 Microprocessors & Embedded Systems [Mikropemproses & Sistem Terbenam]

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

Pengenalan Sistem Maklumat Dalam Pendidikan

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

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

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

TEKNOLOGI, GADJET & KEIBUBAPAAN

Lab 4 : Sorting Techniques

UNIVERSITI SAINS MALAYSIA EEE 355 ROBOT DAN PENGAUTOMATAN

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

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

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

CST234 Network Programming [Pengaturcaraan Rangkaian]

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

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

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

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

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

EEM 312 ROBOTIK & PENGLIHATAN MESIN

LAB 5: WORKING WITH DATA

KOLEJ MULTIMEDIA JALAN GURNEY KIRI KUALA LUMPUR

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

UNIVERSITI SAINS MALAYSIA. CST232 Operating Systems [Sistem Pengendalian]

Instructions: Answer all fourteen [14] questions. tannan:. Jawab semua empat belas [14] soalan.l. [Matematik Diskret] MSS Discrete Mathematics

PANDUAN PENGGUNA (PENSYARAH)

UNIVERSITI MALAYSIA SARAWAK KOTA SAMARAHAN SARAWAK

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

EEE 355 ROBOTIC & AUTOMATION [Robotik & Pengautomatan]

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

Panduan Menggunakan Autoresponder FreeAutobot.com

Transcription:

UNIVERSITI SAINS MALAYSIA Second Semester Examination 2014/2015 Academic Session June 2015 CPT111/CPM111 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 FOURTEEN printed pages before you begin the examination. [Sila pastikan bahawa kertas peperiksaan ini mengandungi TIGA soalan di dalam EMPAT 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) What is a logical error? (iv) (v) Why is main function special in C++? What is the role of #include directive in C++? What is the difference between while and do-while loop? What is the difference between get() and getline()? (30/100) (b) Indicate whether the following statement is TRUE or FALSE. (iv) Hello! is a legal C++ identifier. The statement num = num + 3 is equivalent to num += 3 + num. The expression!(x>10) is equivalent to the expression x<10. The output of the following C++ code is 150. (Assume all variables are properly declared.) num = 100; while (num <=148) num = num + 5; cout << num << endl; (v) If the while expression becomes false in the middle of the while loop, the loop terminates immediately. (20/100) (c) What is the output of the following C++ code? Show the trace. count = 1; num = 15; while (count < 15) num = num - 1; count++; cout << count << << num << endl; (10/100)...3/-

- 3 - (d) Rewrite the following C++ code using switch/case. int selection; cin >> selection; if (selection == 1) cout << Your selection is Option 1 ; else if (selection == 2) cout << Your selection is Option 2 ; else if (selection == 3) cout << Your selection is Option 3 ; else cout << Invalid selection ; Rewrite the following C++ code using for loop. int number = 2; bool done = false; while (!done) cout << number << endl; number = number * 2; if (number >= 64) done = true; (e) Write a C++ program for the following flowchart. (30/100) start num=12 num>=0 T F stop num%>==0 F T print num num=num-1 (10/100)...4/-

- 4-2. (a) The following C++ code uses while loops. #include <iomanip> int i, j; for (i = 1; i <= 9; i++) for ( j = 1; j <= (9 - i); j++) cout << " "; for ( j = 1; j <= i; j++) cout << setw(1) << j; for ( j = (i - 1); j >= 1; j--) cout << setw(1) << j; cout << endl; What is the output of this program? What does iostream and iomanip mean? Why we need to use these two header types in this program? (20/100) (b) The following code uses while loops. cout << "Number Square of Number" << endl; counter = 1; while (counter <= 10) cout << setw(4) << counter << setw(18) << counter * counter << endl; counter++; cout << "Upper case letters are: "; chcounter = 'A'; while (chcounter <= 'Z') cout << chcounter << " "; chcounter++; cout << endl;...5/-

- 5 - Rewrite the C++ code using for loops. Rewrite the C++ code using do-while loops. (30/100) (c) When you are using a function in a C++ program, what are the three (3) components which must be present in the program? (10/100) (d) C++ supports three types of function parameter passing, i.e pass by value, by reference and by pointer. The program below shows an example of a function call using passing by value parameter. int calculate (int); int xy; xy = 12; cout << "original value : " << xy << endl; xy = calculate (xy); cout << "new value : " << xy << endl; int calculate (int data) for (int i = 1; i <= 5; i++) data += i; return data; Modify the program so that the function uses call by reference. Modify the program so that the function uses pointer. What is the output of the above program? (40/100)...6/-

- 6-3. (a) Consider the outline of the C++ program below: #include <fstream> const int year = 20, month = 12; int rainfall[year][month]; int m, y, max, maxy; ifstream infile; ofstream outfile; infile.open ("Rain.dat"); outfile.open ("MaxRain.dat"); infile.close(); outfile.close(); Note: For question 3(a), 3(a) and 3(a), you are required to write a suitable C++ code segments and not to re-write the whole program. Add or modify the appropriate lines so that the program test whether the files are successfully opened, and if not print error message, and exit from the program. Write C++ code that reads in data from the formatted file "Rain.dat", store in the array variable rainfall. The fail "Rain.dat" holds up to twenty years of monthly rainfall figures of Peninsular Malaysia as follow: 12 8 14 28 43... 23 10 data from month Jan Dec for year 1 6 10 0 12 67... 6 2 data for year 2................. 8 7 12 7 23... 9 11 data for year 20 Write C++ code that can determine the year that recorded the maximum rainfall for each month from Jan Dec, and save the result in the format below to a file designated by variable outfile: Month Year 1 3 2 7 3 10.. 12 15 The output above implies that the highest January rainfall was in year 3, the highest February rainfall was in year 7, etc. (50/100)...7/-

- 7 - (b) What is the output of each of the following programs? char string1[] = " How are you?"; cout << "string is : " << string1 << endl; for (int i = 0; string1[i]!= '\0'; i++) cout << string1[i] << "_"; int ch = 100; int *ptr = &ch; cout << "Value stored at pointer ptr is : "; cout << *ptr << endl; 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] + i + j << " "; cout << endl; (50/100)...8/-

KERTAS SOALAN DALAM VERSI BAHASA MALAYSIA - 8-1. (a) Apakah maksud kesalahan logik? (iv) (v) Kenapa fungsi main adalah istimewa dalam C++? Apakah tugas arahan #include dalam C++? Apakah perbezaan di antara gegelung while dan do-while? Apakah perbezaan antara get() and getline()? (30/100) (b) Nyatakan sama ada kenyataan berikut BETUL atau SALAH. (iv) Hello! ialah pengecam C++ yang sahih. Kenyataan num = num + 3 adalah bersamaan dengan kenyataan num += 3 + num. Ungkapan!(x>10) adalah bersamaan dengan ungkapan x<10. Output bagi kod C++ berikut ialah 150. (Andaikan semua pemboleh ubah sudah diisytiharkan.) num = 100; while (num <=148) num = num + 5; cout << num << endl; (v) Jika ungkapan while menjadi palsu di pertengahan gegelung while, gegelung itu akan tamat dengan serta merta. (20/100) (c) Apakah output bagi kod C++ berikut? Tunjukkan surihan. count = 1; num = 15; while (count < 15) num = num - 1; count++; cout << count << << num << endl; (10/100)...9/-

- 9 - (d) Tulis kembali kod C++ berikut dengan menggunakan switch/case. int selection; cin >> selection; if (selection == 1) cout << Your selection is Option 1 ; else if (selection == 2) cout << Your selection is Option 2 ; else if (selection == 3) cout << Your selection is Option 3 ; else cout << Invalid selection ; Tulis kembali kod C++ di bawah dengan menggunakan gegelung for. int number = 2; bool done = false; while (!done) cout << number << endl; number = number * 2; if (number >= 64) done = true; (30/100)...10/-

- 10 - (e) Tuliskan satu atur cara C++ untuk carta alir berikut. start num=12 num>=0 T F stop num%>==0 F T print num num=num-1 (10/100) 2. (a) Kod C++ berikut mengguna gegelung while. #include <iomanip> int i, j; for (i = 1; i <= 9; i++) for ( j = 1; j <= (9 - i); j++) cout << " "; for ( j = 1; j <= i; j++) cout << setw(1) << j; for ( j = (i - 1); j >= 1; j--) cout << setw(1) << j; cout << endl;...11/-

- 11 - Apakah output program C++ ini? Apakah maksud iostream dan iomanip? Mengapa kita perlu menggunakan kedua-dua jenis pengepala/pengatas ini dalam program ini? (20/100) (b) Kod berikut menggunakan gegelung while. cout << "Number Square of Number" << endl; counter = 1; while (counter <= 10) cout << setw(4) << counter << setw(18) << counter * counter << endl; counter++; cout << "Upper case letters are: "; chcounter = 'A'; while (chcounter <= 'Z') cout << chcounter << " "; chcounter++; cout << endl; Tulis kembali kod C++ ini menggunakan gegelung for. Tulis kembali kod C++ ini menggunakan gegelung do-while. (30/100) (c) Apabila anda mengguna fungsi di dalam sesuatu atur cara C++, apakah tiga (3) komponen yang perlu wujud di dalam atur cara tersebut? (10/100)...12/-

- 12 - (d) C++ menyokong tiga jenis penghantaran parameter, iaitu penghantaran melalui nilai, rujukan dan penunjuk. Atur cara berikut menunjukkan satu contoh panggilan fungsi yang menggunakan penghantaran parameter melalui nilai. int calculate (int); int xy; xy = 12; cout << "original value : " << xy << endl; xy = calculate (xy); cout << "new value : " << xy << endl; int calculate (int data) for (int i = 1; i <= 5; i++) data += i; return data; Ubahsuai atur cara tersebut supaya fungsi menggunakan panggilan melalui rujukan. Ubahsuai atur cara tersebut supaya fungsi menggunakan penunjuk. Apakah output program tersebut? (40/100) 3. (a) Pertimbangkan garis kasar atur cara C++ di bawah: #include <fstream> const int year = 20, month = 12; int rainfall[year][month]; int m, y, max, maxy; ifstream infile; ofstream outfile; infile.open ("Rain.dat"); outfile.open ("MaxRain.dat"); infile.close(); outfile.close();...13/-

- 13 - Nota: Untuk soalan 3(a), 3(a) and 3(a), anda dikehendaki menulis cebisan kod C++ yang bersesuaian dan tidak menulis keseluruhan atur cara. Tambah atau ubah suai baris-baris berkenaan supaya atur cara berkenaan menyemak sama ada fail-fail dibuka dengan jayanya, dan jika tidak cetak mesej ralat, dan keluar dari atur cara. Tulis kod C++ untuk baca data dari fail Rain.dat yang berformat, dan simpan di dalam pemboleh ubah tatasusunan bernama rainfall. Fail Rain.dat menyimpan nilai-nilai hujan bulanan selama dua puluh tahun untuk Semenanjung Malaysia seperti berikut: 12 8 14 28 43... 23 10 data from month Jan Dec for year 1 6 10 0 12 67... 6 2 data for year 2................. 8 7 12 7 23... 9 11 data for year 20 Tulis kod C++ yang boleh menentukan tahun yang mencatat hujan maksimum untuk setiap bulan dari Jan Dec, dan simpan keputusan tersebut menggunakan format di bawah ke suatu fail yang dirujuk oleh pemboleh ubah outfile. Month Year 1 3 2 7 3 10.. 12 15 Output di atas menunjukkan hujan maksimum untuk bulan Januari ialah pada tahun 3, hujan maksimum untuk bulan Februari ialah pada tahun 7, dan seterusnya. (50/100)...14/-

- 14 - (b) Apakah output untuk setiap atur cara berikut? char string1[] = " How are you?"; cout << "string is : " << string1 << endl; for (int i = 0; string1[i]!= '\0'; i++) cout << string1[i] << "_"; int ch = 100; int * ptr = &ch; cout << "Value stored at pointer ptr is : "; cout << *ptr << endl; 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] + i + j << " "; cout << endl; (50/100) - ooooooo -