- StudyBlue
- Michigan
- University of Michigan - Ann Arbor
- Engineering
- Engineering 101
- Ringenberg
- F06 Sample Exam 3 Answers

Peter N.

Advertisement

3.1 What Does the Function Do? [40 points] Examine the function below and determine what it wil output and what it wil return when executed if the vector passed in contains the data: 7 3 2 1 5 int multop( const vector & list) { int i, sz = list.size(); int x, y; if (sz == 1) return list.at(0); if (sz == 2) return list.at(0)+list.at(1); vector a, b; a.resize(sz/2); b.resize(sz-sz/2); for(i=0; i void transpose( vector < vector

> & matrix){ int a, b, N=matrix.size(); int temp; if (N > 1) for (a=1; a < matrix.size(); a++){ for(b=0; b < a; b++){ temp = matrix[b][a]; matrix[a][b] = matrix[b][a]; matrix[b][a] = temp; } } return; } NAME Eror temp should be type Q Problem Caused The procedure as writen only works for integers. It is not truly generic. Solution (also mark your changes on the code to the left) Change second declaration line to read: Q temp; Eror temp is set to matrix[b][a] rather than matrix[a][b] Problem Caused Matrix is not realy transposed and some Information is lost in the process Solution (also mark your changes on the code to the left) Change line to read: temp = matrix[a][b]; 3.3 Write a function. [45 points] Write a function that takes a string that represents a blackjack hand and converts it to a value. Each character in the string represents the face value on the card. ?2?-?9? represent cards of those values. ?J?, ?Q? and ?K? represent jack, quen and king respectively and are worth 10 points. The character ?0? represents a 10 and is worth 10 points. The character ?1? represents an ace and is worth 1 points unless the value of the hand would be greater than 21, in which case it is worth 1 point. For example: Hand Value 5267 5+2+6+7 20 3K2 3+10+2 15 QJ5 10+10+5 25 Q1 10+1 21 15 1+5 16 15J 1+5+10 16 18 1+1+8 20 Assume that the string and cctype libraries are included. Note that the characters used have the folowing integer equivalents So, int c= ?6?; int x, y; x = c ; y = c-1; cout < x < endl; cout < y < endl; Would output 54 53 NAME int blackjack( string hand ){ int n, value=0, aces=0; for(n=0; n < hand.size(); n++){ if(isalpha(hand[n]) or hand[n]==?0?) value += 10; else{ value += hand[n]-?0?; if (hand[n]==?1?) aces++; } } while (aces != 0 and value <= 11){ value = value + 10; aces --; } return value; } char int ?0? 48 ?1? 49 ?2? 50 ?3? 51 ?4? 52 ?5? 53 ?6? 54 ?7? 5 ?8? 56 ?9? 57 ?J? 74 ?Q? 81 ?K? 75 Michael Falk Microsoft Word - SampleExam3a-solutions.doc

Advertisement

"The semester I found StudyBlue, I went from a 2.8 to a 3.8, and graduated with honors!"

Jennifer Colorado School of Mines
StudyBlue is not sponsored or endorsed by any college, university, or instructor.

© 2015 StudyBlue Inc. All rights reserved.

© 2015 StudyBlue Inc. All rights reserved.