Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Dynamic Array container with this user interface: // Gets the current number of entries in container int getCurrentSize() // Returns the current capacity

Create a "Dynamic Array" container with this user interface:

// Gets the current number of entries in container int getCurrentSize() // Returns the current capacity of the container int capacity() // Checks whether the container is empty. boolean isEmpty() // Adds a new entry to the container boolean insert(newEntry) // Removes an entry from the container and moves all entries above anEntry down one boolean remove(anEntry) // Get index value int getValue(index) // Removes all entries from the container void clear() // Resize a container by doubling current capacity int resize()

* Implement dynamic resizing using this algorithm:

1. Starting with a dynamic size of 10, if the number of elements exceed this number:

a. Reallocate the container size to double the current size

b. Move the contents of the current container to the newly sized container

c. Delete the previously sized container.

Resize C++ Data* ptemp = new Data[capacity*2 ]; for (int i=0; i

2. Repeat from step 1a. as necessary.

3. Note the data file called "Words.CSV" is in the documents folder for this week.

4. Read the data file and store the words in the dynamic array.

This is what I've so far

#include

#include

#include

#include

#include

#include

#include

using namespace std;

template

class DynamicArray

{

/*

Container class DynamicArray

*/

private:

ArrayType *arr;

int entry,size;

public:

DynamicArray();

~DynamicArray();

int getCurrentSize() { return entry; };/*this function returns total number of entries in the container*/

int capacity() { return size; };/*this function returns capacity of the container*/

bool isEmpty();

bool insert(ArrayType);

bool remove(ArrayType);

int getValue(ArrayType);

void clear() { entry = 0; };/*this function clears the whole container*/

int resize();

};

template

int DynamicArray::resize()

{

/*this function resizes the container by doubling its previous size*/

ArrayType *temp = NULL;

try

{

temp = new ArrayType [2 * size];

}catch(bad_alloc xa){

cout<<" Array allocation failed ";

exit(1);

}

size *= 2;

for(int i = 0 , j = 0 ; i < entry ; i++)

{

temp[i] = arr[i];

j++;

}

delete [] arr;

arr = NULL;

arr = temp , temp = NULL;

return size;

}

template

int DynamicArray::getValue(ArrayType index)

{

/*this function return index of an element specified as index in the container if it presents

in the container, else it returns -1*/

int loc = -1;

for(int i = 0 ; i < entry ; i++)

{

if(arr[i] == index)

{

loc = i;

break;

}

}

return loc;

}

template

bool DynamicArray::remove(ArrayType anEntry)

{

/*this function removes an element specified as anEntry from the container and returns true and

decrements total number of entries by one else returns false*/

bool flag = false;

int index = -1;

for(int i = 0 ; i < entry ; i++)

{

if(arr[i] == anEntry)

{

index = i , flag = true;

break;

}

}

for(int i = index ; i < (entry - 1) && flag == true ; i++)

{

arr[i] = arr[i+1];

}

if(flag == true)

entry -= 1;

return flag;

}

template

bool DynamicArray::insert(ArrayType newEntry)

{

/*this function inserts an element specified as newEntry in the container,

if it successfully inserts an element then it returns true and increments total

number of entries by one, else it returns false*/

entry += 1;

if(entry <= size)

{

arr[entry - 1] = newEntry;

return true;

}

else

{

entry -= 1;

return false;

}

}

template

bool DynamicArray::isEmpty()

{

/*this function checks whether the container is empty or not,

if the container is empty then it returns true else returns false*/

if(entry == 0)

return true;

else

return false;

}

template

DynamicArray::DynamicArray()

{

/*constructer*/

arr = NULL;

try

{

arr = new ArrayType [10];

}catch(bad_alloc xa)

{

cout<<" Array allocation failed ";

exit(0);

}

entry = 0, size = 10;

}

template

DynamicArray::~DynamicArray()

{

/*destructer*/

delete [] arr;

arr = NULL;

}

int main()

{

DynamicArray arr;

ifstream inputFile;

string fileName;

string entry;

//Asks user for file name

//cout << "Please enter the file name: " << endl;

//cin >> fileName;

inputFile.open("Words.csv");

/*

while (inputFile.fail())

{

inputFile.clear();

cout << "Invalid file name" << endl;

cout << "Please enter the valid file name: " << endl;

cin >> fileName;

inputFile.open("Words.csv");

}*/

while (inputFile)

{

getline(inputFile,entry);

arr.insert(entry);

}

return 0;

}

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 Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions