You are on page 1of 15

DATA STRUCTURES & ALGORITHMS

SUMMATIVE

2
[M4_M5_M6-SUMMATIVE2] - ANSWER SHEET
STACKS / QUEUES / RECURSION

Group Name:
Name Role
Members:

Section:

Data Structures & Algorithms (LAB) Page 1 of 15


I. PROGRAM SOLUTION (60 POINTS)

 REQUIREMENTS:
o You are required to use Codio. Teacher will check actual C++ project in Codio. Each member must
ensure that his Codio Project is updated and the same as what the group submits so that the teacher
may check any of the member’s work.
o In snipping images of your answers, make sure that the names of the files are visible. Properly label
the images as follows and should be pasted in the order stated below:
 Header File (.h file)
 Implementation File (.cpp)
 Main Program (.cpp)
o Show sample output for each operation. Show adequate outputs to demonstrate how your program
works.

Snip and paste your source codes and outputs below. Snip it directly from Codio IDE so that colors
of the codes are preserved for readability. Include additional pages if necessary.

PROBLEM 1: IntStack

Header file (.h file) source codes:

#pragma once

template <class T>


class TemplateStack
{
private:
T *stackArray;
int stackSize;
int top;

public:
TemplateStack(int size);
void push(T num);
void pop(T &num);
bool isFull();
bool isEmpty();
};

Data Structures & Algorithms (LAB) Page 2 of 15


Implementation file(.cpp) source codes:

#include <iostream>
#include "data.h"
using namespace std;

template <class T>


TemplateStack<T>::TemplateStack(int size)
{
stackArray = new T[size];
stackSize = size;
top = -1;
}

template <class T>


void TemplateStack<T>::push(T num)
{
if(isFull())
cout<<"\n The stack is full.";
else
{
top++;
stackArray[top] = num;
}
}

template <class T>


void TemplateStack<T>::pop(T &num)
{
if(isEmpty())
cout<<"\n The stack is empty.";
else
{
num = stackArray[top];
top--;
}
}
template <class T>
bool TemplateStack<T>::isFull()
{
bool status;
if(top == stackSize -1)
status = true;
else
status = false;

Data Structures & Algorithms (LAB) Page 3 of 15


return status;
}

template <class T>


bool TemplateStack<T>::isEmpty()
{
bool status;
if(top == -1)
status = true;
else
status = false;
return status;
}

Main program(.cpp) :

#include <iostream>
#include "data.h"
#include "Implementation.cpp"
using namespace std;

int main()
{

TemplateStack <char>stack(5);
char catchVar;
cout<<"\n ********************** Character Version ********************** ";

cout<<"\n Pushing 'T'";


stack.push('T');
cout<<"\n Pushing 'h'";
stack.push('h');
cout<<"\n Pushing 'i'";
stack.push('i');
cout<<"\n Pushing 's'";
stack.push('s');
cout<<"\n Pushing '$'";
stack.push('$');

cout<<"\n\n Popping....... ";


stack.pop(catchVar);
cout<<catchVar<<endl;

Data Structures & Algorithms (LAB) Page 4 of 15


stack.pop(catchVar);
cout<<"\t\t"<<catchVar<<endl;
stack.pop(catchVar);
cout<<"\t\t"<<catchVar<<endl;
stack.pop(catchVar);
cout<<"\t\t"<<catchVar<<endl;
stack.pop(catchVar);
cout<<"\t\t"<<catchVar<<endl;

TemplateStack <double>stackFloat(3);
double catchVarFloat;

cout<<"\n\n ********************** Float Version ********************** ";

cout<<"\n Pushing 2.5";


stackFloat.push(2.5);
cout<<"\n Pushing 1.9";
stackFloat.push(1.9);
cout<<"\n Pushing 45.7";
stackFloat.push(45.7);

cout<<"\n\n Popping....... ";


stackFloat.pop(catchVarFloat);
cout<<catchVarFloat<<endl;
stackFloat.pop(catchVarFloat);
cout<<"\t\t"<<catchVarFloat<<endl;
stackFloat.pop(catchVarFloat);
cout<<"\t\t"<<catchVarFloat<<endl;
return 0;
}

Data Structures & Algorithms (LAB) Page 5 of 15


PROBLEM 2: Queue Exceptions

Header file (.h file) source codes:

#pragma once

class Q_underflow{};
class Q_overflow{};

class IntQueue
{

private:

int *queueArray;
int queueSize;
int front;
int rear;

Data Structures & Algorithms (LAB) Page 6 of 15


int numItems;
bool isEmpty();
bool isFull();

public:

IntQueue(int);
~IntQueue();
void enqueue(int);
void dequeue(int &);
void clear();
};

Implementation file(.cpp) source codes:

#include <iostream>
#include "data.h"
using namespace std;

IntQueue::IntQueue(int s)

queueArray = new int[s];


queueSize = s;
front = -1;
rear = -1;
numItems = 0;

IntQueue::~IntQueue()
{
delete [] queueArray;
}

void IntQueue::enqueue(int num)


{

if (isFull()){

cout << "The queue is full.\n";


throw Q_overflow();
}

Data Structures & Algorithms (LAB) Page 7 of 15


else

{
rear = (rear + 1) % queueSize;
queueArray[rear] = num;
numItems++;
}
}

void IntQueue::dequeue(int &num)

if (isEmpty())
{
cout << "The queue is empty.\n";
throw Q_underflow();
}

else

front = (front + 1) % queueSize;

num = queueArray[front];

numItems--;

bool IntQueue::isEmpty()

bool status;
if (numItems)
status = false;
else
status = true;
return status;
}

Data Structures & Algorithms (LAB) Page 8 of 15


bool IntQueue::isFull()

bool status;
if (numItems < queueSize)
status = false;
else
status = true;
return status;
}

void IntQueue::clear()

{
front = queueSize - 1;
rear = queueSize - 1;
numItems = 0;
}

Main program(.cpp) :

#include <iostream>
#include "data.h"
using namespace std;

int main()

{
IntQueue iQueue(5);
try{
cout << "Enqueuing 5 items...\n";

for (int x = 0; x < 5; x++)


iQueue.enqueue(x);
cout << "Now attempting to enqueue again...\n";
iQueue.enqueue(5);
cout << "The values in the queue were:\n";
while (true)
{
int value;

Data Structures & Algorithms (LAB) Page 9 of 15


iQueue.dequeue(value);
cout << value << endl;
}
}
catch(Q_overflow ex){
cout<<"Caught exception of Queue Overflow.";
}
catch(Q_underflow ex){
cout<<"Caught exception of Queue Underflow.";
}

Data Structures & Algorithms (LAB) Page 10 of 15


Queue Overflow:

If the line “iQueue.enqueue(5) is on the code.

Data Structures & Algorithms (LAB) Page 11 of 15


Queue Underflow:

If the line “iQueue.enqueue(5) is not on the code.

Data Structures & Algorithms (LAB) Page 12 of 15


PROBLEM 3: Recursive Member Set

Source code:

#include <iostream>

using namespace std;

bool isMember(int *arr, int size, int number) {


if (size == 0) {
return false;
} else {
return arr[size - 1] == number || isMember(arr, size - 1, number);
}
}

int main() {
int size, *arr, num;
cout << "How many numbers are in the array? ";
cin >> size;
arr = new int[size];
cout << "Enter the array numbers separated by spaces:" << endl;
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}
cout << "Enter a value to test membership for: ";
cin >> num;
if (isMember(arr, size, num)) {
cout << "The number " << num << " is in the array." << endl;
} else {
cout << "The number " << num << " is not in the array." << endl;
}
delete[] arr;
return 0;
}

Data Structures & Algorithms (LAB) Page 13 of 15


If value is found:

If value is not found:

Data Structures & Algorithms (LAB) Page 14 of 15


II. QUESTION AND ANSWER (10 POINTS)

Briefly answer the questions below. Avoid erasures. Do not forget to include the sources for all NON-
ORIGINAL IDEAS. Indicate the name of the member that contributed the answer.

a. What is the difference between static and dynamic stacks? What advantages do dynamic stacks
have over static stacks?

A static stack has a fixed size and is implemented as an array. A dynamic stack grows in size as needed
and is implemented as a linked list.

Advantages of a dynamic stack:

There is no need to specify the starting size of the stack.

b. What two queue-like containers does the STL offer?

Stacks and queues are two containers in STL which are very basic in nature. They are the simplest
containers which have wide applications in software programming.

c. When should you choose a recursive algorithm over an iterative algorithm?

In situations where the recursive algorithm is easier to design. Specifically, situations where a
problem can be broken down into small repetitions of very similar problems.

Data Structures & Algorithms (LAB) Page 15 of 15

You might also like