STL Trace Code Worksheet #3, Exercises of Compiler Design

A series of code segments that need to be traced and output shown. The code segments involve the use of various STL containers such as list, deque, stack, set, and priority_queue. The purpose of the exercise is to test the student's understanding of the STL and their ability to trace code. The document also contains instructions not to use the computer, C++ compiler, or the internet for help with the exercises and to show scratchwork on the paper.

Typology: Exercises

2022/2023

Uploaded on 05/11/2023

wilbur
wilbur 🇺🇸

212 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Software Design Name –
STL Trace Code worksheet #3
designed by Matt V 2010
DO NOT USE THE COMPUTER, C++ COMPILER, OR THE INTERNET FOR HELP WITH THESE EXERCISES.
SHOW SCRATCHWORK ON THIS PAPER.
Trace the following code segments and show the output.
1)
list<int> numbers;
int k = 6;
for (int j = 0; j < 6; j++)
{
numbers.insert(numbers.end(), k);
k--;
}
list<int>::iterator i;
for (i = numbers.begin(); i != numbers.end(); i++)
{
cout << *i << endl;
}
2)
deque<int> deqExample;
int k = 7;
for (int i = 1; i < 6; i++)
{
deqExample.push_front(k + 1);
k++;
}
deqExample.pop_front();
deqExample.pop_front();
for (int i = 0; i < deqExample.size(); ++i)
{
cout << deqExample[i] << ' ';
}
3)
stack <int> stackExample;
stackExample.push(60);
stackExample.push(6);
stackExample.push(222);
stackExample.push(8);
stackExample.pop();
while (stackExample.size() != 0)
{
cout << stackExample.top() << " ";
stackExample.pop();
pf3

Partial preview of the text

Download STL Trace Code Worksheet #3 and more Exercises Compiler Design in PDF only on Docsity!

Software Design Name –

STL Trace Code worksheet

designed by Matt V 2010

DO NOT USE THE COMPUTER, C++ COMPILER, OR THE INTERNET FOR HELP WITH THESE EXERCISES.

SHOW SCRATCHWORK ON THIS PAPER.

Trace the following code segments and show the output.

list numbers; int k = 6;

for (int j = 0; j < 6; j++) { numbers.insert(numbers.end(), k); k--; }

list::iterator i;

for (i = numbers.begin(); i != numbers.end(); i++) { cout << *i << endl; }

deque deqExample; int k = 7;

for (int i = 1; i < 6; i++) { deqExample.push_front(k + 1); k++; }

deqExample.pop_front(); deqExample.pop_front();

for (int i = 0; i < deqExample.size(); ++i) { cout << deqExample[i] << ' '; }

stack stackExample; stackExample.push(60); stackExample.push(6); stackExample.push(222); stackExample.push(8); stackExample.pop();

while (stackExample.size() != 0) { cout << stackExample.top() << " "; stackExample.pop();

set setExample; setExample.insert(2); setExample.insert(34); setExample.insert(54); setExample.insert(99); set::iterator i; setExample.erase(34);

for (i = setExample.begin(); i != setExample.end(); i++) cout << *i << " ";

list numbers; int num = 1;

for (int j = 0; j < 5; j++) { numbers.insert(numbers.end(), num); num++; }

numbers.pop_front(); numbers.reverse(); list::iterator i;

for (i = numbers.begin(); i != numbers.end(); i++) cout << *i << endl;

priority_queue pq; pq.push(16); pq.push(50); pq.push(99); pq.pop(); pq.push(22); pq.push(66); pq.push(33); pq.pop();

while (!pq.empty()) { cout << pq.top() << " "; pq.pop(); }

deque deqExample; int num = 1;

for (int i = 1; i <= 6; i++)