Question: Part 1 Write and fully test an AssociativeArray template, with all the public interface functions presented in this module. Name its file as AssociativeArray.h .

Part 1

Write and fully test an AssociativeArray template, with all the public interface functions presented in this module. Name its file as AssociativeArray.h. Test but do not submit your test driver CPP. At this point in the semester you should know how to fully test a template, so do it, but don't submit it. Submit the H along with part 2's CPP.

Part 2

Write a new version of the schedule program that (1) makes use of your AssociativeArray template and (2) expands the results to list the number of courses offered per subject code (not the number of sections) and the number of sections of each course. Like this:

ADJUS, 16 course(s) ADJUS-120, 191 section(s) ADJUS-121, 57 section(s) ADJUS-122, 40 section(s) ADJUS-130, 24 section(s) ADJUS-203, 20 section(s) ... ... SPTUT, 1 course(s) SPTUT-020NC, 12 section(s) TAGLG, 2 course(s) TAGLG-155, 5 section(s) TAGLG-156, 3 section(s) 

Base the duplicate checking on the (fast-running) Schedule v.2, and not the (slow-running) Schedule 1. This should run as fast as Schedule v.2.

Use any combination of your own StaticArray, DynamicArray, and AssociativeArray templates, and no STL containers for duplicate checking or subject code and course counting, but be sure to use your AssociativeArray for at least one of your data structures in the solution. If you do use your StaticArray or DynamicArray, include their H files in your file submissions.

Do not use struct-based objects like you did in the previous solutions. Using your AssociativeArray, structs should no longer be necessary. Instead, for counting use an AssociativeArray with course as the "key" and #of sections of that course as "value". Use that as the "value" inside another AssociativeArray with subject code as the "key". For duplicate checking, you can track term-sections with an AssociativeArray of string- AssociativeArray as explained in the module. Or use some other way of your own design, using your own StaticArray, DynamicArray, and AssociativeArray templates.

AssociativeArray.h:

#ifndef ASSOCIATIVEARRAY_H

#define ASSOCIATIVEARRAY_H

#include

#include

using namespace std;

template

class AssociativeArray

{

struct Node

{

K key;

V value;

bool inUse;

};

Node* values;

int siz;

int cap;

void capacity(int);

public:

AssociativeArray(int = 2); //DONE!

AssociativeArray(const AssociativeArray &); //DONE!

AssociativeArray& operator=(const AssociativeArray &); //DONE!

~AssociativeArray() { delete[] values; } //DONE!

V operator[](const K&) const; // or const V& as return type //DONE! //TEST DONE!

V& operator[](const K&); //DONE! //TEST DONE!

bool containsKey(const K&) const; //DONE! //TEST DONE!

void deleteKey(const K&); //DONE! //TEST DONE!

queue keys() const; //DONE!

int size() const { return siz; } //DONE! //TEST DONE!

void clear(); //DONE! //TEST DONE!

};

//==================================================================================

// The Main Constructor

//==================================================================================

template

AssociativeArray::AssociativeArray(int cap)

{

this->cap = cap;

values = new Node[cap];

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

{

values[i].key = K();

values[i].value = V();

values[i].inUse = false;

}

}

//==================================================================================

// The Copy Constructor

//==================================================================================

template

AssociativeArray::AssociativeArray(const AssociativeArray &original)

{

cap = original.cap;

values = new Node[cap];

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

{

values[i].key = original.values[i].key;

values[i].value = original.values[i].value;

values[i].inUse = original.values[i].inUse;

}

siz = original.siz;

}

//==================================================================================

// The Assignment Operator

//==================================================================================

template

AssociativeArray & AssociativeArray::operator=(const AssociativeArray &original)

{

if (this != &original)

{

delete[] values;

cap = original.cap;

values = new Node[cap];

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

{

values[i].key = original.values[i].key;

values[i].value = original.values[i].value;

values[i].inUse = original.values[i].inUse;

}

siz = original.siz;

}

return *this;

}

//==================================================================================

// The Square Bracket Operator Getter

//==================================================================================

template

V AssociativeArray::operator[](const K& index) const

{

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

{

if (values[i].inUse && values[i].key == index)

{

return values[i].value;

}

}

return V();

}

//==================================================================================

// The Square Bracket Operator Setter

//==================================================================================

template

V& AssociativeArray::operator[](const K& key)

{

int indexOfFirstUnusedKey = cap; // =cap indicates no not in-use in found (yet)

for (int index = 0; index < cap; index++) //where "cap" is a private data member

{

if (values[index].inUse)

{

if (values[index].key == key) // K must support ==

return values[index].value;

}

else if (indexOfFirstUnusedKey == cap) // no not in-use found yet

indexOfFirstUnusedKey = index;

}

// If we get this far, no matching key was found

if (indexOfFirstUnusedKey == cap) capacity(2 * cap); // Need more Nodes

values[indexOfFirstUnusedKey].key = key;

values[indexOfFirstUnusedKey].value = V();

values[indexOfFirstUnusedKey].inUse = true;

++siz;

return values[indexOfFirstUnusedKey].value; // This is how the value gets set

}

//==================================================================================

// The containsKey Getter

//==================================================================================

template

bool AssociativeArray::containsKey(const K& key) const

{

for (int index = 0; index < cap; index++) //where "cap" is a private data member

{

if (values[index].inUse)

{

if (values[index].key == key) // K must support ==

return true;

}

}

}

//==================================================================================

// The deleteKey Setter

//==================================================================================

template

void AssociativeArray::deleteKey(const K& key)

{

for (int index = 0; index < cap; index++) //where "cap" is a private data member

{

if (values[index].inUse)

{

if (values[index].key == key) // K must support ==

{

values[index].inUse = false;

--siz;

break;

}

}

}

}

//==================================================================================

// The Keys getter

//==================================================================================

template

queue AssociativeArray::keys() const

{

queue k_queue;

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

if (values[i].inUse)

k_queue.push(values[i].key);

return k_queue;

}

//==================================================================================

// The clear setter

//==================================================================================

template

void AssociativeArray::clear()

{

for (int index = 0; index < cap; index++) //where "cap" is a private data member

{

values[index].inUse = false;

}

siz = 0;

}

//==================================================================================

// The capacity setter

//==================================================================================

template

void AssociativeArray::capacity(int newCap)

{

Node *temp = new Node[newCap];

int limit = min(newCap,cap);

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

{

temp[i] = values[i];

}

for (int i = limit; i < cap; i++)

{

temp[i].value = V();

temp[i].key = K();

}

delete[] values;

values = temp;

cap = newCap;

}

#endif

download link for all files: https://drive.google.com/open?id=15_JNJs_Fw37eo3dqlOlpP68WO2RO1JRu

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!