Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem Description: A vector is an array of elements that can grow and shrink as needed. Instead of needing to reserve the maximum amount of

Problem Description: A vector is an array of elements that can grow and shrink as needed. Instead of needing to reserve the maximum amount of memory when compiled, a vector can dynamically allocate memory during runtime when needed. It can grow when it is full or shrink when it is empty. We will create a vector class that will perform the necessary functions.

Checkpoint 1: Create a class called vector. You should have a *.h and *.cpp file. You will need to include the header file in your driver program. All files must be in your project. Define the private data members of the vector. You will only make a vector of integers, so you need an int * to act as your vector. You will need a current_size (number of elements) and maximum_size (total memory allocated) for your vector also.

Checkpoint 2: Create your initialize function. Start with a vector initially allocated with a maximum of 10 elements. Go ahead and create your display function now also, you will be using it a lot. Place a : that will separate the last element and the rest of the elements. Print the entire vector where the default value is 0. You should output your current and maximum size.

Checkpoint 3: Create your Push_Back() function. This will add an element to the end of the vector. Do not add more elements than the initial 10 allocated yet. Test your push function several times by displaying your vector. As you add elements, your current size should grow and maximum size will remain the same.

Checkpoint 4: Create your Pop_Back() function. This will delete the last element in the vector. Again test your push and pop functions with your display. Try popping an empty vector. This is an underflow error. Place code to prevent this from happening. You want to have similar code in place if you try pushing an element onto a full vector. The next checkpoint deals with these errors, right now just have the error code produced.

Checkpoint 5: Now we want to push more than 10 elements. When you do, you will get an overflow error. When this occurs, recognize this in Push_Back(), and expand the vector. The Expand() function will take the argument for a new maximum size. Since this is expensive timewise, you should always double the maximum size of the vector so it does not happen often. Test this with your Display function to show the expansion works.

Checkpoint 6: Create the Shrink() function that take an argument for the new maximum size. This new size should be large enough to hold all the current data elements. Call this function when your vector has few elements compared to its maximum size. When called, your new maximum size should be more than your current size. Be sure to clean up your memory.

Checkpoint 7: Create a Copy() function. This will be called when you want to copy a vector to another vector. Everything should be the same, the current size, the maximum size, and all the elements.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

1. Design an effective socialization program for employees.

Answered: 1 week ago