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
STUDYBLUE makes things that make you better at school.
Things like
online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free.
Join us.
“Simply amazing. The flash cards are smooth, there are many different types of studying tools, and there is a great search engine. I praise you on the awesomeness.”
Dennis