Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I'm writing associate array program that can analyze the course list and I get lost. Please help, and please attach your output screenshot if

Hi, I'm writing associate array program that can analyze the course list and I get lost. Please help, and please attach your output screenshot if you know how to solve the problem.

Thank you SO much in advance!

Below is part of sample output:

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) 

:

#include

#include

#include

using namespace std;

#include

#include "AssociativeArray.h"

#include "DynamicArray.h"

#include // for srand and rand

#include // for clock(), clock_t, time, and CLOCKS_PER_SEC

int main( )

{

srand(time(0)); rand();

clock_t startTime = clock();

char* token; char buf[1000];

const char* const tab = "\t";

int counter = 0;

int duplicate = 0;

bool dup_tester = true;

AssociativeArray> count;

AssociativeArray> alreadySeen;

ifstream infile;

infile.open("schedule.txt");

if (!infile.good()) throw "I/O error";

while (infile.good() && counter < 100)

{

dup_tester = true;

if (counter % 1000 == 0)

cout << '.'; cout.flush();

// read the line

string line;

getline(infile, line);

strcpy(buf, line.c_str());

if (buf[0] == 0) continue; // skip blank lines

//parse the line

const string semster = strtok(buf, " ");

const string term((token = strtok(0, tab)) ? token : "");

const string section((token = strtok(0, tab)) ? token : "");

const string course((token = strtok(0, tab)) ? token : "");

infile.clear();

if (course.find('-') == string::npos) continue; // invalid line: no dash in course name

const string subjectCode(course.begin(), course.begin() + course.find('-')); // subjectCode means: MATH

// duplication check

if (alreadySeen[term][section])

duplicate++;

else

{

alreadySeen[term][section] = true;

count[subjectCode][course]++;

}

cout << semster << endl << term << endl;

counter++;

}

infile.close();

queue subjectKeys = count.Keys();

queue courseKeys; // = subject_code[subjectCode].Keys() //use this queue to sort the course list

string subcode[300];

string section[300];

int s_num, c_num = 0;

while(subjectKeys.size()>0)

{

subcode[s_num++] = subjectKeys.front();

subjectKeys.pop();

}

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

{

for (int j = i+1; j < s_num; ++j)

{

if(subcode[j] < subcode[i])

swap(subcode[j],subcode[i]);

}

}

int SUB,SEC = 0;

while (s_num && s_num >= 0)

{

courseKeys = count[subcode[SUB]].Keys();

while(courseKeys.size() > 0)

{

section[c_num++] = courseKeys.front(); courseKeys.pop();

}

for (int i = 0; i < c_num; ++i) // Sorting section keys for each run

{

for (int j = i+1; j < c_num; ++j)

{

if(section[j] < section[i])

swap(section[j],section[i]);

}

}

cout << subcode[SUB] << ", " << count[subcode[SUB]].size() << " course(s)" << endl;

while (c_num && c_num >= 0)

{

//cout << " "<< section[SEC] << ", " << subject_code [subcode[SUB]] [section[SEC]] << " section(s) ";

SEC++;

c_num--;

}

SUB++; SEC=0;

s_num--;

}

cout << " The test result for duplicate: " << duplicate << endl;

cout << " Read line " << counter << " lines.";

clock_t endTime = clock();

double elapsedSeconds = (double)(endTime - startTime) / CLOCKS_PER_SEC;

cout << " Runtime: " << elapsedSeconds <<" s";

return 0;

}

BELOW IS :

#ifndef AssociativeArray_h

#define AssociativeArray_h

#include

#include

#include

using namespace std;

template

class AssociativeArray

{

struct Node

{

K key;

V value;

Node* next;

bool inUse;

};

Node* valuse;

int cap;

V dummy = V();

Node* firstNode;

Node* lastNode;

int siz;

public:

AssociativeArray(){firstNode = 0; lastNode = 0; siz = 0;}; // default constructor

AssociativeArray(const AssociativeArray&); //copy constructor

AssociativeArray& operator=(const AssociativeArray &);

~AssociativeArray() ; // Destructor

V& operator[](const K&) const; // getter version

V& operator[](const K&) ; // setter version

bool containsKey(const K&) const; //getter

void deleteKey(const K&); // setter

//queue Keys() const;

queue Keys();

//int size() {return siz;}

int size() const {return siz;}

void clear();

void printList() const;

};

/*template

AssociativeArray::AssociativeArray()

{

//this->cap = cap;

firstNode = 0;

//values = new V[cap];

siz = 0;

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

valuse[i] = V();//

}*/

template

AssociativeArray::~AssociativeArray() // Destructor Dyanamic Memory Management

{

while (firstNode)

{

Node* p = firstNode;

firstNode = firstNode->next;

delete p;

}

}

template //copy constructor Dyanamic Memory Management

AssociativeArray::AssociativeArray(const AssociativeArray& original)

{

firstNode = 0;

Node* lastNode = 0; // temporary tail

siz = original.siz;

for (Node* p = original.firstNode; p; p = p->next)

{

Node* temp = new Node;

temp->value = p->value;

temp->key = p->key;

temp->next = 0;

if (lastNode) lastNode->next = temp;

else firstNode = temp;

lastNode = temp;

}

}

// ********************************************************************************

template // getter function

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

{

for (Node* p = firstNode; p; p = p->next)

if (p->key == key)

return p->value;

return dummy;

}

template // setter function

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

{

for (Node* p = firstNode; p; p = p->next)

if (p->key == key)

return p->value;

++siz;

Node* temp = new Node;

temp->key = key;

temp->value = V();

temp->next = firstNode;

firstNode = temp;

return firstNode->value;

}

// ********************************************************************************

template // assignment operator

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

{

if (this != &original)

{

// deallocate existing list

while (firstNode)

{

Node* p = firstNode->next;

delete firstNode;

firstNode = p;

}

// build new queue

Node* lastNode = 0; // temporary tail

for (Node* p = original.firstNode; p; p = p->next)

{

Node* temp = new Node;

temp->value = p->value;

temp->key = p->key;

temp->next = 0;

if (lastNode) lastNode->next = temp;

else firstNode = temp;

lastNode = temp;

}

siz = original.siz;

}

return *this;

}

// ********************************************************************************

template // containKey

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

{

for (Node* p = firstNode; p; p = p->next)

{

if (p->key == key)

return true;

}

return false;

}

template // deleteKey

void AssociativeArray::deleteKey(const K& key)

{

// find the matching node

Node* p, *prev; // declare above loop so that it survives below the loop

for (p = firstNode, prev = 0; p; prev = p, p = p->next)

if (p->key == key)

break;

// if found (that is, p did not run off the end of the list)

if (p)

{

--siz;

if (prev) prev->next = p->next; // skips over the node at p

else firstNode = p->next; // there's a new head, possibly zero

delete p; // we're done with this node

}

}

// ********************************************************************************

// double check the following

template // Keys getter

queue AssociativeArray::Keys()

{

queue key_Queue;

for (Node* p = firstNode; p; p = p->next)

key_Queue.push(p->key);

return key_Queue;

}

template

void AssociativeArray::clear()

{

while (firstNode)

{

Node* p = firstNode;

firstNode = firstNode->next;

delete p;

--siz;

}

}

#endif

BELOW IS :

#ifndef MyDynamicArray_h

#define MyDynamicArray_h

#include

using namespace std;

template

class DynamicArray

{

int cap;

V* values;

V dummy;

public:

DynamicArray(int = 2);

~DynamicArray() {delete[] values;}

DynamicArray(const DynamicArray&);

int capacity() const {return cap;}

V operator[](int) const;

V& operator[](int);

DynamicArray& operator=(const DynamicArray&);

void capacity(int);

};

template

DynamicArray::DynamicArray(int cap)

{

this->cap = cap;

values = new V[cap];

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

values[i] = V();

};

template

DynamicArray::DynamicArray(const DynamicArray& original)

{

cap = original.cap;

values = new V[cap];

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

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

};

template

V DynamicArray::operator[](int index) const

{

if (index < 0) return dummy;

if (index >= cap) return dummy;

return values[index];

};

template

V& DynamicArray::operator[](int index)

{

if (index < 0) return dummy;

if (index >= cap) capacity(2 * index);

return values[index];

};

template

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

{

if (this != &original)

{

delete [] values;

cap = original.cap;

values = new V[cap];

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

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

}

return *this;

}

template

void DynamicArray::capacity(int cap)

{

// allocate a new array with the new capacity

V* temp = new V[cap];

// get the lesser of the new and old capacities

int limit = min(cap, this->cap); // requires the C++ "algorithm" library

// copy the contents

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

temp[i] = values[i];

// set added values to their defaults

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

temp[i] = V();

// deallocate original array

delete [] values;

// switch newly allocated array into the object

values = temp;

//update the capacity

this->cap = cap;

};

#endif

BELOW IS part of the :

Fall 2014 2353 PHILO-120 Abele MW 12:30-1:50pm H-106

Fall 2008 2353 PHILO-120 Abele MW 12:30-1:45pm H-109

Fall 2016 2353 PHILO-120 Abele MW 11:00-12:20pm H-106

Fall 2012 2353 PHILO-120 Abele MW 12:30-1:45pm H-106 8/17-12/17

Fall 2008 2357 PHILO-220 Abele TTH 9:30-10:45am H-107

Fall 2009 2357 PHILO-220 Abele MW 9:30-10:45am H-107

Fall 2010 2357 PHILO-220 Abele MW 9:30-10:45am H-107

Fall 2014 2374 PHILO-122 Abele F 9:00-11:50am H-106

Spring 2015 3021 PHILO-298 Abele 2/23-5/20

Spring 2015 3469 PHILO-299 Abele

Summer 2009 8003 PHILO-120 Abele MW 5:30-9:45pm H-108 6/22-7/29

Fall 2008 8355 PHILO-120 Abele W 4:00-6:50pm H-108

Spring 2007 2116 CHEM-108 Abrash TTHM 8:00-9:15am PS-267

Spring 2006 2116 CHEM-108 Abrash TTHM 8:00-9:15am PS-267

Spring 2005 2116 CHEM-108 Abrash TTHTH 8:00-9:15am PS-275

Spring 2004 2122 CHEM-108 Abrash TTHW 12:30-1:45pm PS-267

Fall 2002 8632 MUSPF-176 Accatino M 4:00-6:50pm M-104

Fall 2002 8663 MUSPF-146 Accatino M 4:00-6:50pm M-104

Fall 2001 8663 MUSPF-146 Accatino M 4:00-6:50pm M-104

Spring 2015 3228 ENGIN-136 Acharya MW 2:00-3:40pm ET-117 1/21-5/20

Fall 2009 5007 SPCH-120 Achten online 8/26-12/02

Summer 2003 8178 ENGL-118 Ackerman-Luo TTH 6:30-9:45pm LA-211 6/10-7/31

Spring 2012 1382 MUSIC-181 Aczon T 12:30-3:20pm M-125

Spring 2015 1382 MUSX-181 Aczon WW 12:30-1:50pm M-104

Spring 2006 1382 MUSIC-181 Aczon T 12:30-2:20pm M-125

Spring 2009 1382 MUSIC-181 Aczon T 2:00-3:50pm M-125

Spring 2016 1382 MUSX-181 Aczon T 12:30-3:20pm M-127

Spring 2010 1382 MUSIC-181 Aczon T 2:00-3:50pm M-125

Spring 2011 1382 MUSIC-181 Aczon T 2:00-3:50pm M-125 1/22-5/27

Spring 2015 1397 SOCSC-120 Alvarado-Strasser TTH 8:00-9:20am LA-208A

Spring 2015 1417 SOCSC-120 Alvarado-Strasser TTH 12:30-1:50pm LA-208A

Fall 2014 1796 SOCSC-220 Alvarado-Strasser TTH 9:30-10:50am LA-218

Spring 2015 1797 SOCSC-220 Alvarado-Strasser TTH 9:30-10:50am LA-215

Spring 2014 1835 SOCSC-220 Alvarado-Strasser MW 2:00-4:50pm H-109 3/24-5/21

Spring 2014 1836 SOCIO-120 Alvarado-Strasser MW 8:00-9:20am MA-101

Summer 2014 3988 SOCIO-120 Alvarado-Strasser MTWTH 10:30-12:35pm LA-120 6/16-7/24

Fall 2014 8165 SOCSC-120 Alvarado-Strasser TTH 12:30-1:50pm MA-103

Fall 2015 8168 SOCIO-120 Alvarado-Strasser M 7:00-9:50pm LA-215

Spring 2014 8710 SOCSC-120 Alvarado-Strasser T 7:00-9:50pm LA-208B

Spring 2014 9073 SOCIO-120 Alvarado-Strasser TH 6:00-8:50pm SRC-W216

Spring 2007 1036 HUMAN-110 Alvarez-Mon MWF 10:00-10:50am H-108

Spring 2006 1036 HUMAN-110 Alvarez-Mon MWF 10:00-10:50am H-108

Spring 2006 1040 HUMAN-110 Alvarez-Mon MWF 8:00-8:50am H-106

Spring 2007 1040 HUMAN-110 Alvarez-Mon MWF 8:00-8:50am H-106

Fall 2006 2295 HUMAN-105 Alvarez-Mon MWF 9:00-9:50am H-106

Fall 2006 2332 HUMAN-110 Alvarez-Mon MWF 11:00-11:50am H-108

Spring 2014 1258 COMSC-110 Amarachinta MW 9:30-12:20pm ATC-109

Spring 2017 1258 COMSC-110 Amarachinta MW 9:30-11:00am L-142

Spring 2016 1275 COMSC-110 Amarachinta TTH 9:30-12:20pm ATC-115

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

Students also viewed these Databases questions

Question

why we face Listening Challenges?

Answered: 1 week ago

Question

what is Listening in Context?

Answered: 1 week ago