Академический Документы
Профессиональный Документы
Культура Документы
Spring 2016
Midterm (Written)
JenniferDunham
Name: __________________________________
jedunham@csumb.edu
Email: __________________________________
1. What is output of the following code? A brief explanation of how you arrived at the answer is
necessary. (2 points)
int values = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
Stack s;
for (int i = 0; i < 9; i++)
s.push( values[ i ] );
int n = 25;
for (int i = 0; i < 4; i++)
{
n += s.pop( );
}
for (int i = 0; i < 2; i++)
{
n -= s.pop( );
}
cout << n;
Thisprogramwilloutputthenumber65.
Thefirststeppushesthefirst8values(1to17)ofthearraytothestack.
nisthensetto25.Then,thetop4valuesofthestackarepopped,withtheirvaluesaddedtosas
theyarepopped(s+=17+=15+=13+=11)=81
Then,thenext2valuesarepoppedoffthestack,withtheirvaluesremovedfromsastheyarepopped
(s=9=7)=65.65isoutputtothescreen.
2. Consider two stacks each of size 5. When you pop an element from the first stack you
multiply it by 3 and add it to the second stack. When you pop an element from the second stack
you multiply it by 4 and add it to the first stack.
Push numbers 1 to 5 to the first stack in order (i.e., push 1 first, then 2 and so on). Push numbers
6 to 10 to the second stack in order. First pop two numbers from the first stack (remember when
you pop a number it is going to be added to the second stack). Then pop three numbers from the
second stack (remember when you pop a number it is going to be added to the first stack). (2
points)
Notethatthefirsttwopop()sfromthefirststackarenotaddedtothesecondstack,becauseitisfull
with5values.Thethirdpop()fromthesecondstackisnotaddedtothefirststack,becauseitisfull
with5values.
3. What is output of the following code? A brief explanation of how you arrived at the answer is
necessary. (2 points)
int values = [5, 10, 15, 20, 30];
Queue q;
for (int i = 0; i < 5; i++)
q.enqueue( values[ i ] );
int n = 0;
for (int i = 0; i < 2; i++)
{
n += q.dequeue( );
}
cout << n;
cout << q.front(); // the function front prints the element at
the front of the queue.
Theprogramwilloutputthenumber15,twice.(i.e.,1515)
All5numbersfromthearrayareenqueuedintoq.nissetto0.Twovaluesfromthequeueare
dequeued,andtheirvaluesareaddedton(n+=5+=10)=15.cout<<noutputs15.Thefront()
numberinthequeueafterthe2dequeuesis15,socout<<q.front()outputs15.
myQueue.dequeue();
cout << myQueue.front() << endl;
Theprogramwilloutputthenumber100andthenthenumber300.
100and300areaddedtothequeue.Thefront()numberatthispointis100.Then,aftera
dequeuer(),the100isremoved,and300isnowthefront()number.
5. What data structure will you use to convert infix expressions to postfix expressions? Convert
the following infix expressions to postfix expressions using that data structure. Clearly show the
contents within the data structure at each step. You can show the contents of the data structure by
separating them by commas. (3 points)
a) (2+3*7)+(4-6*7)
Iwilluseastacktoconvertinfixexpressionstopostfixexpressions.
Stack
PostfixString
(
(
2
(,+
2
(,+
23
(,+,*
23
(,+,*
237
237*+
+
237*+
+,(
237*+
+,(
237*+4
+,(,
237*+4
+,(,
237*+46
+,(,,*
237*+46
+,(,,*
237*+467
+
237*+467*
237*+467*+
Finalresult:237*+467*+
Whenexecuted,thisgives15,asexpected.
6. Rank the following in order of increasing complexity O(n) O(log n) O(nn), O(1), O(n log log
n). Explain your ranking. (2 points)
O(1)O(logn)O(n)O(nloglogn)O(nn)
ThemostefficientalgorithmisonethatrunsatconstantO(1)complexity.O(logn)isslightlyless
efficientbutstillarelativelyefficientcomplexity.O(n),linearcomplexity,comesnext,witha
complexityofO(nloglogn)beingjustslightlylessefficientthanO(n).O(nn)isaveryinefficient
complexity,withahighrateofincreasingcomplexityasnincreases.
7. What is the complexity of the following code assuming that the cout statement has O(1)
complexity. Explain your answer. (2 points)
for (int i = 0; i < n ; i++)
for (int j = 0; j < n ; j++)
cout << i + j;
O(n2)(quadratic)
=2*2x(n2)
=2*2*2x(n3)
=2kx(nk)
Setk=n1
x(n)=2n1x(1)
x(n)=2n1
a) What is the value of sum after this code executes (provide your answer in terms of n).
n(n+1)/2
b) What is the complexity of the code assuming that adding two numbers always has O(1)
complexity? Explain your answer.
O(n).Althoughthesumofnisrelatedton2duetoisrelationshipwithanascendingsequenceof
integers,thisfunctionwillcompleteexecutionafteronly1loopofntimes,meaningitscomplexityis
O(n).
10. Implement the empty() function that returns true if a linked list is empty. Otherwise return
false. Write C++ code for the empty() function. (2 points)
bool LinkedList::empty()
{
Node * ptr = first;
if (mySize <= 1) //or if
return true;
return false;
}
first pointer == NULL (Can use either MySize or the head pointer)