key_10AfinalF07

key_10AfinalF07 - PIC 10A Fall 2007 FINAL EXAM KEY Prof....

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

FINAL EXAM KEY Prof. Todd Wittman 1.) [8 points] Correct the error(s) in each segment of code below. You will get points off for changing code that was not in error. a.) int x; cin >> x; string words[x]; words[0] = "Gandalf Array size must be a constant. Change to vector: vector<string> words(x); b.) int y; cin >> y; if 0<y<10 cout << "y is between 0 and 10"; Incorrect if statement: if (0<y && y<10) c.) double investment = 2000.0; double return = 1.016 * investment; cout<< setprecision(2) << fixed; cout<< "The return on your investment is \$" << return; Cannot use return as variable, it is a reserved word. Change return to my_return d.) char c; cin >> c; if (c=="f" && c=="F") cout << "You typed an upper or lower-case F."; Will never run because can't be both f and F. Also chars have single quotes. if (c=='f' || c=='F') 2.) [6 points] Briefly comment what each line of code below does. int a[4]={10,20,30,40}; //Create an array a with values 10,20,30,40. int* p; int* q; //Create integer pointers p and q. p = new int; //Allocate new integer memory block to p. *p = 55; //Assign value 55 to block p points to. q = a; //Set q pointing to first element of array a. *q = *p; //Assign value q points to (a[0]) same as p (55). delete p; //Free up the integer block p points to. 3.) [12 points] Write the output of each of the code segments below. a.) string s = "gandalf"; s.insert(3,"SAM"); cout << s << "\n"; s.erase(2,4); cout << s << "\n"; s = s.substr(2,4); cout << s; ganSAMdalf gadalf dalf b.) int a=4; int j=1; do { cout << j << "+"; j = j + 2; if (j>a) j--; } while (j<8); 1+3+4+5+6+7+ c.) cout << setprecision(2); for (int i=3; i>0; i--) { cout << i/3.0 << " "; } 1 0.67 0.33 d.) int a[4] = {3,4,-2,6};

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/27/2010 for the course PIC 157-050-20 taught by Professor Wittman during the Winter '10 term at UCLA.

Page1 / 4

key_10AfinalF07 - PIC 10A Fall 2007 FINAL EXAM KEY Prof....

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

View Full Document
Ask a homework question - tutors are online