8---------10AfinalW07key[1](2)

8---------10AfinalW07key[1](2) - Final Exam, 3/18/07 PIC...

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

View Full Document Right Arrow Icon
Final Exam, 3/18/07 PIC 10A, Winter 2007 KEY ID NUMBER:______________________________________________ Check your TA’s name: _____ Chris _____ Jukka _____ Stephen _____ Joseph _____ Kree _____ Kwai Tsz (Alvin) You have 3 hours to complete this exam. You are not allowed to use any books, notes, calculators, or electronic devices. Write your answers carefully. Incomplete, unintelligible, or illegible answers will receive little or no credit. When you are asked to write a program, it is not necessary to comment your code but you are expected to indent appropriately to make your code easier to follow. There are a total of 200 points on this exam. PAGE SCORE POSSIBLE 1 20 2 23 3 12 4 18 5 20 6 30 7 22 8 25 9 30 TOTAL 200
Background image of page 1

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

View Full DocumentRight Arrow Icon
1.) [20 points] The following segment of code counts the number of words input by the user. string word; vector<string> list; while ( cin >> word ) list.push_back(word); cout << "The # of words is " << list.size( ); a.) What C++ libraries does this code require? <string> <vector> <iostream> b.) Explain the run-time error in this code. (Hint: How many iterations will the loop perform?) This code gets caught in an infinite loop, because anything the user types will be read in as a string. c.) Modify the code to stop when the user enters a word with 5 characters like "frodo". You should only change the while boolean condition. while (cin >> word && word.lenth( ) != 5) d.) Note that if the user entered 1,000,000 words, then the program would store 1,000,000 strings. Modify the code above so that only 1 string needs to be stored. string word; int numWords = 0; while (cin >> word) numWords++;
Background image of page 2
cout << "The # of words is " << numWords;
Background image of page 3

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

View Full DocumentRight Arrow Icon
2.) [6 points] What is the difference between the <iostream> and <fstream> libraries? How are they similar? The <iostream> library is for input/output to the console, <fstream> is for file I/O. With an ifstream fin and ofstream fout, the I/O operations look identical. <iostream> Input: cin >> x; Output: cout << x; <fstream> Input: fin >> x; Output: fout << x; Also both inputs return booleans if the variable x was read successfully. 3.) [5 points] In HW7 and HW8, you wrote a Card class whose constructor created a random card. Explain why it is important not to seed the program with the srand function in the Card constructor function. Seeding in the constructor would start reading the list of random numbers
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

8---------10AfinalW07key[1](2) - Final Exam, 3/18/07 PIC...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online