Professional Documents
Culture Documents
SUMMATIVE
2
[M4_M5_M6-SUMMATIVE2] - ANSWER SHEET
STACKS / QUEUES / RECURSION
Group Name:
Name Role
Members:
Section:
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
#pragma once
public:
TemplateStack(int size);
void push(T num);
void pop(T &num);
bool isFull();
bool isEmpty();
};
#include <iostream>
#include "data.h"
using namespace std;
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 ********************** ";
TemplateStack <double>stackFloat(3);
double catchVarFloat;
#pragma once
class Q_underflow{};
class Q_overflow{};
class IntQueue
{
private:
int *queueArray;
int queueSize;
int front;
int rear;
public:
IntQueue(int);
~IntQueue();
void enqueue(int);
void dequeue(int &);
void clear();
};
#include <iostream>
#include "data.h"
using namespace std;
IntQueue::IntQueue(int s)
IntQueue::~IntQueue()
{
delete [] queueArray;
}
if (isFull()){
{
rear = (rear + 1) % queueSize;
queueArray[rear] = num;
numItems++;
}
}
if (isEmpty())
{
cout << "The queue is empty.\n";
throw Q_underflow();
}
else
num = queueArray[front];
numItems--;
bool IntQueue::isEmpty()
bool status;
if (numItems)
status = false;
else
status = true;
return status;
}
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";
Source code:
#include <iostream>
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;
}
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.
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.
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.