Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

,, 4 []] w Pus0 rn Pus0 w w~ Pus0 ~ I Pus0 Push § EE OJ ~[]] q p ~ w ; p ~rn ; p ~ w § p ~ w EE p OJ COP3503 Programming Fundamentals 2 University of Florida Lab 3 – Array-Based...

1 answer below »
,,
4 []] w Pus0 rn Pus0
w w~ Pus0
~ I Pus0 Push § EE OJ
~[]]
q
p ~ w ; p ~rn ; p ~ w § p ~ w EE p OJ
COP3503
Programming Fundamentals 2
University of Florida
Lab 3 – A
ay-Based Stack and Queue
Overview
In this assignment, you will be implementing your own A
ay-Based Stack (ABS) and A
ay-Based Queue
(ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means
that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of
papers on a desk—the first one to be removed is the last one placed on top.)
A queue is another linear data structure that follows the First-In, First-Out (FIFO) property. FIFO means
that the data added first is the first to be removed (like a line in a grocery store—the first person in line
is the first to checkout).
Both of these are data structures—some sort of class to store information. What makes them different is
how they store and access the information. Adding the same values (say, the numbers 1-10) to either
data structure would result in different ordering/output of the information. Why you might choose one
or the other depends on the specific needs of a program.
New keywords/language concepts
 Templates – write code to work with multiple data types
 The Big Three – Copy Constructor, Copy Assignment Operator, and Destructor – necessary when
working with dynamic memory
 Exceptions – Used to indicate something went wrong in an application. Unhandled exceptions
will cause a program to terminate
 Deep copy – Creating a copy of dynamically allocated memory requires new memory to be
allocated before copying values
Stack Behavior
Stacks have two basic operations (with many other functions to accomplish these tasks):
Push – Add something to the top of the stack.
Pop – Remove something from the top of the stack and return it.
Example of LIFO operations-the data most recently added is the first to be removed




Bacik I IFrolnt ~ue
Enqueue I
COP3503
Programming Fundamentals 2
University of Florida
Queue Behavior
Like a stack, a queue has two basic operations:
Enqueue – Add something to end of the queue. If this were a line, a new person getting into the line
would start at the back.
Dequeue – Remove something from the front of the queue. If this were a line, the person at the start of
the line is next—for whatever the people are lining up for.
Example of FIFO operations-the newest data is last to be removed
Description
Your ABS and ABQ will be template classes, and thus will be able to hold any data type. (Many data
structures follow this convention—reuse code whenever you can!) Because these classes will be using
dynamic memory, you must be sure to define The Big Three:
 Copy Constructor
 Copy Assignment Operator
 Destructor
Data will be stored using a dynamically allocated a
ay (hence the a
ay-based stack and queue). You
may use any other variables/function in your class to make implementation easier.
The nature of containers like these is that they are always changing size. You have 3 elements in a stack,
and push() another… now you need space for 4 elements. Use push() to add another, now you need
space for 5, etc… If your container is full, you can increase the size by an amount other than one, if you
want.
Why increase (or decrease) the size by any amount other than one?
Short answer: performance!
If you are increasing or decreasing the size of a container, it’s reasonable to assume that you will want to
increase or decrease the size again at some point, requiring another round of allocate, copy, delete, etc.
Increasing the capacity by more than you might need (right now) or waiting to reduce the total capacity
allows you to avoid costly dynamic allocations, which can improve performance—especially in situations
in which this resizing happens frequently. This tradeoff to this approach is that it will use more memory,
ut this speed-versus-memory conflict is something that programmers have been dealing with for a long
time.
COP3503
Programming Fundamentals 2
University of Florida
By default, your ABS and ABQ will have a scale factor 2.0f—store this as a class variable.
1. Attempting to push() or enqueue() an item onto an ABS/ABQ that is full will resize the
cu
ent capacity to cu
ent_capacity*scale_factor.
2. When calling pop() or dequeue(), if the “percent full” (e.g. cu
ent size / max capacity)
ecomes strictly less than 1/scale_factor, resize the storage a
ay to
cu
ent_capacity/scale_factor.
An example of the resizing scheme to be implement on a stack.
Resizing a
ays
What’s easy to say isn’t usually easy to do in programming. You can’t “just” change the size of an a
ay.
You have to:





. .
Push () res121ng
The stack is ful l
when push ()
is called.
The stack is
doubled in size
The new item
is added to the
top of the stack
Pop () res121ng
. .
The stack is
exactly halffull
pop ( ) is called
and the top item
is removed

The stack is under
half fu ll, and the
stack is resized.
1. Create a new a
ay based on the desired size
2. Copy elements from the old a
ay to the new a
ay (up to the size of the old a
ay, or the capacity of
the new a
ay, WHICHEVER IS SMALLER).
3. If you were adding something to the a
ay, copy that as well
4. Delete the old a
ay
5. Redirect the pointer to the old a
ay to the new a
ay
Exceptions
Some of your functions will throw exceptions. There are many types of exceptions that can be thrown,
ut in this case you will simply throw e
ors of type runtime_e
or. This is a general purpose e
or to
indicate that something went wrong. The basic syntax for throwing an e
or is simply:
throw type_of_exception("Message describing the e
or.");
If you wanted to throw a runtime_e
or exception that said “An e
or has occu
ed.” you would write:
throw runtime_e
or("An e
or has occu
ed.");
There is also the concept of using try/catch blocks, but for this assignment you will only have to throw
the exceptions. Checking for such exceptions will be handled by various unit tests on zyBooks.

Method Description
ABS() Default constructor. Maximum capacity should
be set to 1, and cu
ent size set to 0;
ABS(int capacity) Constructor for an ABS with the specified starting
maximum capacity.
ABS(const ABS &d) Copy Constructor
ABS &operator=(const ABS &d) Assignment operator.
~ABS() Destructo
void push(T data) Add a new item to the top of the stack and resize
if necessary.
T pop() Remove the item at the top of the stack, resizes if
necessary, and return the value removed.
Throws a runtime_e
or if the stack is empty.
T peek() Return the value of the item at the top of the
stack, without removing it.
Throws a runtime_e
or if the stack is empty.
unsigned int getSize() Returns the cu
ent number of items in the ABS.
unsigned int getMaxCapacity XXXXXXXXXXReturns the cu
ent max capacity of the ABS.
T* getData() Returns the a
ay representing the stack.
Stack Functions
COP3503
Programming Fundamentals 2
University of Florida
Your ABS must support the following methods:
Additional methods may be added as deemed necessary.

Method Description
ABQ() Default constructor. Maximum capacity should
be set to 1, and cu
ent size set to 0;
ABQ(int capacity) Constructor for an ABQ with the specified
starting maximum capacity.
ABQ(const ABS &d) Copy Constructor
ABQ &operator=(const ABQ &d) Assignment operator.
~ABQ() Destructo
void enqueue(T data) Add a new item to end of the queue and resizes if
necessary.
T dequeue() Remove the item at front of the queue, resizes if
necessary, and return the value removed.
Throws a runtime_e
or if the queue is empty.
T peek() Return the value of the item at the front of the
queue, without removing it.
Throws a runtime_e
or if the queue is empty.
unsigned int getSize() Returns the cu
ent number of items in the ABQ.
unsigned int getMaxCapacity() Returns the cu
ent max capacity of the ABQ.
T* getData() Returns the a
ay representing the queue.
COP3503
Programming Fundamentals 2
University of Florida
Queue Functions
Your ABQ must support the following functions
Additional methods may be added as deemed necessary.







COP3503
Programming Fundamentals 2
University of Florida
Extra Credit (1%)
You can earn up to 1% extra credit (toward your final course grade) for this lab by doing everything
listed below. DO NOT work on extra credit until your standard lab scored full points. If your standard
lab does not score full points, you cannot earn any extra credit.
Update ABS/ABQ
Update your ABS and ABQ to use a scale factor other than 2.0f, and modify any methods that should
ehave differently as a result:
● ABS(int capacity, float scale_factor) : Constructor for an ABS with the specified starting capacity,
and a
Answered Same Day Oct 09, 2022

Solution

Aditi answered on Oct 10 2022
56 Votes
Lab Analysis
Setup:
In order to print out the type, scale, N, Push time, Pop time, & Resize counts for each operation, I built a print function for this project. This made it simple to copy and paste data for data analysis & straightforward chart generation into a.csv file.
I created a Test function in "main.cpp" with the following structure:
Test( ABQ_or_ABS, N, scale_factor )
In the test file, I could simply cycle over a
ays of a test Ns and scaling factors in this fashion. The test function would then push/enqueue the right number of Ns at the right scale factor, then pop/dequeue them. I created a print function that outputs each operation's type, scale, N, Push time, Pop time, & Resize counts, with a comma between each data point. This made it simple to copy and paste data for data analysis & straightforward chart generation into a.csv file.
My first lab solution involved establishing a new stack or queue with every pop or dequeue, but I later changed it to simply produce new...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here