Question: Write An Array Application [MyDynamicArray.cpp] Write MyDynamicArray.cpp using your DynamicArray template . Use your already-tested and verified H file from part 1. Exactly as in

Write An Array Application [MyDynamicArray.cpp] Write MyDynamicArray.cpp using your DynamicArray template. Use your already-tested and verified H file from part 1.

Exactly as in Lab Assignment 2a's MyStaticArray.cpp, this app lets its user enter as many values as they like, and when that process is completed, lets the user look up values by matching index.

In a loop, the app should prompting the user to enter a pair of numbers on the same line: a whole number index and its corresponding floating point value. Do not validate index input in the app. Quit the loop when an uppercase or lowercase Q is entered for either the index or the value. Indexes can be entered in any order -- they don't have to start with zero and go up by one thereafter. It's whatever the user enters.

Your app should keep track of which indexes got entered. Use a bool DynamicArray for that. After all data entry is complete, the app should:

output how many (unique) indexes got entered,

output the list of all used indexes and their values, per the example below, and

implement an event-controlled loop that prompts for an index value and outputs whether the index is in use or not, and if in use, what is the

value stored for that index. Loop until the user elects to stop by entering uppercase or lowercase Q.

Here's a sample of how this should work (user input in blue):

Write An Array Application [MyDynamicArray.cpp] Write MyDynamicArray.cpp using your DynamicArray template. Use

Here is the code for DynamicArray.h:

#ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include using namespace std; template class DynamicArray { private: int size; dataT dummy; dataT *data; bool *in_use; static const int SIZE = 100; unsigned int capacity; public: DynamicArray(int = SIZE); DynamicArray(const DynamicArray &); virtual ~DynamicArray(); DynamicArray & operator=(const DynamicArray &); dataT operator[](unsigned int )const; dataT& operator[](unsigned int); unsigned int Size()const; unsigned int Capacity()const; bool ContainsKey(unsigned int)const; void DeleteKey(unsigned int); vector Keys()const; void Clear(); private: void Copy(const DynamicArray &); void Delete(); void SetCapacity(unsigned int SIZE = 10); }; template DynamicArray::DynamicArray(int SIZE) { capacity = SIZE; data = new dataT[capacity]; in_use = new bool[capacity]; size = 0; for (int i = 0; i DynamicArray::DynamicArray(const DynamicArray & d_arr) :data(NULL), in_use(NULL) { Copy(d_arr); } template DynamicArray:: ~DynamicArray() { Delete(); } template DynamicArray & DynamicArray::operator=(const DynamicArray & d_arr) { if (this != &d_arr) { Delete(); Copy(d_arr); } return *this; } template dataT DynamicArray::operator[](unsigned int index)const { if (index >= capacity) return dummy; return data[index]; } template dataT& DynamicArray::operator[](unsigned int idx) { if (idx + 1 > capacity) { SetCapacity(idx + 1); } if (!in_use[idx]) size++; in_use[idx] = true; return data[idx]; } template unsigned int DynamicArray::Size()const { return size; } template unsigned int DynamicArray::Capacity()const { return capacity; } template bool DynamicArray::ContainsKey(unsigned int idx)const { if (idx void DynamicArray::DeleteKey(unsigned int idx) { if (idx vector DynamicArray::Keys()const { vector a; for (unsigned int i = 0; i void DynamicArray::Clear() { for (unsigned int i = 0; i void DynamicArray::Copy(const DynamicArray & D_array) { size = D_array.size; capacity = D_array.capacity; data = new dataT[capacity]; in_use = new bool[capacity]; for (unsigned int i = 0; i void DynamicArray::Delete() { if (data) { delete[] data; data = NULL; } if (in_use) { delete in_use; in_use = NULL; } size = capacity = 0; } template void DynamicArray::SetCapacity(unsigned int new_size) { if (new_size == 0) { Delete(); return; } dataT * newvalues = new dataT[new_size]; bool * newInUse = new bool[new_size]; size = 0; unsigned int upperLimit; if (capacity ostream & operator & D_array) { vector a = D_array.Keys(); for (unsigned int i = 0; i

And the code for DynamicArrayDriver.cpp:

#include #include #include #include "DynamicArray.h" using namespace std; void MyDynamicDriver() { DynamicArray string_array; DynamicArray int_array; cout s_arr(string_array); const DynamicArrayint_arr(int_array); cout s_arr2; DynamicArray int_arr2; cout s_indices; s_indices = s_arr2.Keys(); cout i_indices; i_indices = int_arr2.Keys(); cout

Heres the code for MyStaticArray:

#include

#include

using namespace std;

#include

#include "StaticArray.h"

const bool PLACED = true;

int main()

{

//Delclarations

StaticArray storeValue;

StaticArray storeStatus;

int storeTotal = 0;

string index;

string value;

do

{

cout

cin >> index;

if(index == "Q" || index == "q")

{

break;

}

cin >> value; // Get value from user

cin.ignore(1000,10);

storeValue[atof(index.c_str())] = atof(value.c_str()); // Store value

storeStatus[atof(index.c_str())] = PLACED;

}

while(index != "Q" || index != "q"); // Q to quit

for(int i = 0; i

{

if(storeStatus[i] == PLACED)

{

storeTotal++;

}

}

// Output

cout

cout

for(int i = 0; i

{

if(storeStatus[i] != 0)

{

cout "

}

}

do

{

cout

cin >> index;

if(index == "Q" || index == "q")

{

break;

}

if(storeStatus[atof(index.c_str())] == PLACED && atof(index.c_str()) >=0 && atof(index.c_str())

{

double valFind = storeValue[atoi(index.c_str())]; // Find value for user

cout

}

else

{

cout

}

}

while(index != "Q" || index != "q");

} // Close Main

// End of program

Input an index and a value [Q to quit]: 33 1.2 Input an index and a value [Q to quit]: 4 100 Input an index and a value [Q to quit]: 5 300 Input an index and a value [Q to quit]: x 1.7 Input an index and a value [Q to quit]: 33 120 Input an index and a value [Q to quit]: -1 23.4 Input an index and a value [Q to quit]: 2000 -999.9 Input an index and a value [Q to quit]: q You stored this many values: 4 The index-value pairs are: 4 100 5 300 33 => 120 2000 => -999.9 Input an index for me to look up [Q to quit]: 33 Input an index for me to look up [Q to quit]: -10 Input an index for me to look up [Q to quit]: 38 Input an index for me to look up [Q to quit]: 10000 Input an index for me to look up [Q to quit]: 2000 Input an index for me to look up [Q to quit]: q Found it the value stored at index 33 is 120 Sorry, but there is no value stored at index -10 Sorry, but there is no value stored at index 38 Sorry, but there is no value stored at index 10000 Found it -- the value stored at index 2000 is -999.9

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!