{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Final_01_fall_A_sol

# Final_01_fall_A_sol - 19 December 2001 COMP 102 Final Exam...

This preview shows pages 1–5. Sign up to view the full content.

19 December, 2001 COMP 102 -- Final Exam Version A Page 1 of 16 THE HONG KONG UNIVERSITY OF SCIENCE AND TECHNOLOGY Department of Computer Science COMP 102: Computer and Programming Fundamentals I Fall 2001 Final Examination Version A Solution Date: 19 December 2001 Time: 12:30-3:30 pm Venue: Hall This exam contains 10 questions in 16 pages. Please count the pages. You have 3 hours to complete this exam. Problem Your point s Max point s Problem Your point s Max point s 1 6 6 5 2 10 7 5 3 10 8 10 4 15 9 10 5 10 10 19 Subtotal 51 Subtotal 49 Your total points 100 Please identify yourself: Lecture/Lab section Name Student ID Signature

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
19 December, 2001 COMP 102 -- Final Exam Version A Page 2 of 16 1. (6 points) For each of the follow C++ code segments, mark it with “CORRECT” if it is correct and “WRONG” if it is NOT correct. You should consider both syntax and logical correctness. WRONG a)int array[4] ={ 0, 1, 2, 3, 4 }; CORRECT b)int x[5]; x[0] = 0; WRONG c)void fn (const int x[ ] ) { if ( x[0] < 0) x[0] = 0; } CORRECT d)char message[ ] = "Hello"; WRONG e)char message[6]; message = "Hello"; WRONG f)int array[2,3];
19 December, 2001 COMP 102 -- Final Exam Version A Page 3 of 16 2. (10 points) The following program shows the result of a battle between the Taliban and Pashtun tribesmen in Afghanistan. The function call count(taliban) uses enumeration constants as array indices to store the values in the taliban array. What will the report function print on the screen? #include <iostream.h> #include <string.h> enum {totalfighters, fighters, prisoners, dead, totaldead}; const int FIGHTERS = 500; void count(int army[]); // This function produces the values {221, 31, 19, 11, 160} when it is // called with the taliban array. // The values are {452, 146, 35, 39, 121} for the pashtun array. void report(int army[], char name[], char city[]){ cout << "Current " << name << " dead: " << army[dead] << endl; cout << "Current " << name << " prisoners: " << army[prisoners] << endl; cout << "Remaining " << name << " fighters in " << city << ": " << army[fighters] << endl; cout << "The " << name << " has lost " << army[totaldead] << " fighters." << endl; cout << "Total remaining " << name << " fighters: " << army[totalfighters] << endl << endl; } int main () { int taliban[5] = {FIGHTERS, 0, 0, 0, 0}; int pashtun[5] = {FIGHTERS, 0, 0, 0, 0}; count(taliban); report(taliban, "Taliban", "Kandahar"); count(pashtun); pashtun[totalfighters] += taliban[prisoners]; report(pashtun, "Pashtun", "Kandahar"); return 0; } Answer: Complete the following output produced by the report function: Current Taliban dead: 11 Current Taliban prisoners: 19 Remaining Taliban fighters in Kandahar: 31 The Taliban has lost 160 fighters. Total remaining Taliban fighters: 221 Current Pashtun dead: 39 Current Pashtun prisoners: 35 Remaining Pashtun fighters in Kandahar: 146 The Pashtun has lost 121 fighters. Total remaining Pashtun fighters: 471

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
19 December, 2001 COMP 102 -- Final Exam Version A Page 4 of 16 3. (10 points) This is the selection sort algorithm discussed in the lecture for sorting int arrays: void select(int data[], int size){ int temp; int max_index = 0; for (int rightmost=size-1; rightmost>0; rightmost--){ for(int current=1; current<=rightmost; current++){ if (data[current] > data[max_index]) max_index = current; } if(data[max_index] > data[rightmost]){ temp = data[max_index]; data[max_index] = data[rightmost]; data[rightmost] = temp; } } } Using the select
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 16

Final_01_fall_A_sol - 19 December 2001 COMP 102 Final Exam...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online