Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help! Why isn\'t my code working properly? It only finds 185 duplicates in the l

ID: 3870670 • Letter: H

Question

Help! Why isn't my code working properly? It only finds 185 duplicates in the list, but it should find 199.

How the program should work:

Write DvcSchedule4.cpp to read and parse the 70,000 line dvc-schedule.txt (Links to an external site.)Links to an external site.  text file, and find each subject code in the file. Output each code to the console screen, in alphabetical order, with the number of classes offered under that code. Use your own DynamicArray template from Lab Assignment 3. Do NOT use any STL containers, and do NOT modify your H except to make corrections. Submit the H file, even if there are no corrections since lab 3. Canvas will add it's version tracking to the file's name -- that's okay.

Parse. Start with the simple parsing code and see if you can read and parse the TXT file.

Count. Then add your code to count how many subject codes and how many sections of each.

Duplicate Check. Then add your code to track and exclude duplicates from counting.

Link to .txt file: https://drive.google.com/file/d/0B14YTZL55whCSGE0VjcyajZxSmc/view

Code DvcSchedule4.cpp:

#include <array>

#include <ctime>

#include <fstream>

#include <iostream>

#include "StaticArray.h"

#include <string>

using namespace std;

struct SubjectCode {

string name;

int count;

inline bool operator<(SubjectCode arg) {

if (name.compare(arg.name) < 0) {

return true;

}

return false;

}

};

struct Node {

SubjectCode data;

Node* next;

};

struct termAndSection {

string semester;

string sectionNumber;

};

bool foundData(SubjectCode);

int main() {

  

cout << " Casey Bender" << endl;

cout << "Lab 4, The "DvcSchedule4" Program " << endl;

cout << endl;

cout << endl;

  

  

char* token;

char buf[1000];

Node* start = 0;

Node* p;

Node *prev;

Node* q;

array<termAndSection, 1000> data1;

string line;

int dupliCount =0;

bool dupliCourse;

bool found;

clock_t startTime = clock();

double elapsedSeconds;

ifstream fin;

fin.open("dvc-schedule.txt");

if (!fin.good()) {

cout << "Unable to open file" << endl;

}

while (!fin.eof()) {

dupliCourse = false;

found = false;

  

getline(fin, line);

strcpy(buf, line.c_str());

if (buf[0] == 0) continue;

  

const string term(token = strtok(buf, " "));

const string section(token = strtok(NULL, " "));

const string course(token = strtok(NULL, " , -"));

for (int i = 1; i < 1000; i++) {

if (term.compare(data1[i].semester) == 0 &&

section.compare(data1[i].sectionNumber) == 0) {

dupliCourse = true;

}

}

if (dupliCourse == true) {

dupliCount++;

} else {

for (int i = 1; i < 1000; i++) {

data1[i].semester = term;

data1[i].sectionNumber = section;

}

p = start;

while (p != NULL && !found) {

if (p->data.name.compare(course) == 0) {

found = true;

}

if (!found) {

p = p->next;

}

}

if (found) {

p->data.count++;

} else {

Node* node = new Node;

node->data.name = course;

node->data.count = 1;

node->next = start;

start = node;

}

}

}

fin.close();

  

for (p = start; p; p = p->next) {

for (q = p->next; q; q = q->next) {

if (q->data < p->data) {

SubjectCode temp = p->data;

p->data = q->data;

q->data = temp;

}

}

}

  

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

  

for (p = start;p ;p = p->next) {

if (p -> data.name != "course")

cout << p->data.name << ", " << p->data.count << " Classes" << endl;

}

  

cout << " The duplicates entries are " << dupliCount << endl;

cout << "The processing time is: " << elapsedSeconds << " seconds." << endl;

cout << endl;

cout << endl;

return 0;

}

Header: StaticArray.h

#ifndef StaticArray_H

#define StaticArray_H

template<class DataType, int CAPACITY>

class Array{

public:

Array();

inline DataType& operator[](int);

int getCapacity() const;

int lsearch(const DataType&) const;

private:

DataType data[CAPACITY];

int index;

DataType dummy;

};

template<class DataType, int CAPACITY>

Array<DataType, CAPACITY>::Array() {

}

template<class DataType, int CAPACITY>

inline DataType& Array<DataType, CAPACITY>::operator[](int index) {

if (index < 0)

      return dummy;

  

if (index >= CAPACITY)

return dummy;

  

return data[index];

}

template<class DataType, int CAPACITY>

int Array<DataType, CAPACITY>::getCapacity() const {

return CAPACITY;

}

template<class DataType, int CAPACITY>

int Array<DataType, CAPACITY>::lsearch(const DataType& arg) const {

for (int i = 0; i < CAPACITY; i++) {

if(arg == data[i]) {

return i;

}

}

return -1;

}

#endif

Explanation / Answer

DvcSchedule4.cpp
-------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstring>
#include "StaticArray.h"
using namespace std;

struct SubjectCode {
    string name;
    int count;

    inline bool operator<(SubjectCode arg) {
        if (name.compare(arg.name) < 0) {
            return true;
        }
        return false;
    }
};

struct Node {
    SubjectCode data;
    Node* next;

};

struct termAndSection {
    string semester;
    string sectionNumber;
};

bool foundData(SubjectCode);

int main() {
    cout << "Lab 4, The "DvcSchedule4" Program " << endl
         << "File: " << __FILE__ << endl
         << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;

    char* token;
    char buf[1000];
    Node* start = 0;
    Node* p;
    Node *prev;
    Node* q;
    Array<termAndSection, 1000> data1;
    string line;
    int dupliCount =0;
    bool dupliCourse;
    bool found;
    clock_t startTime = clock();
    double elapsedSeconds;
    ifstream fin;

    fin.open("dvc-schedule.txt");
    if (!fin.good()) {
        cout << "Unable to open file" << endl;
    }

    while (!fin.eof()) {
        dupliCourse = false;
        found = false;

        getline(fin, line);
        strcpy(buf, line.c_str());
        if (buf[0] == 0) continue;

        const string term(token = strtok(buf, " "));
        const string section(token = strtok(NULL, " "));
        const string course(token = strtok(NULL, " , -"));

        for (int i = 1; i < 1000; i++) {
            if (term.compare(data1[i].semester) == 0 &&
                section.compare(data1[i].sectionNumber) == 0) {
                dupliCourse = true;
            }
        }

        if (dupliCourse == true) {
            dupliCount++;
        } else {
            for (int i = 1; i < 1000; i++) {
                data1[i].semester = term;
                data1[i].sectionNumber = section;
            }
            p = start;
            while (p != NULL && !found) {
                if (p->data.name.compare(course) == 0) {
                    found = true;
                }
                if (!found) {
                    p = p->next;
                }
            }
            if (found) {
                p->data.count++;
            } else {
                Node* node = new Node;
                node->data.name = course;
                node->data.count = 1;
                node->next = start;
                start = node;
            }
        }
    }
    fin.close();

    for (p = start; p; p = p->next) {
        for (q = p->next; q; q = q->next) {
            if (q->data < p->data) {
                SubjectCode temp = p->data;
                p->data = q->data;
                q->data = temp;
            }
        }
    }

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

    for (p = start;p ;p = p->next) {
        if (p -> data.name != "course")
            cout << p->data.name << ", " << p->data.count << " Classes" << endl;
    }

    cout << " The duplicates entries are " << dupliCount << endl;
    cout << "The processing time is: " << elapsedSeconds << " seconds." << endl;
    return 0;
}
----------------------------------------------------------------------------------------------------------
StaticArray.h
----------------------------------------------------------
#ifndef StaticArray_H
#define StaticArray_H

template<class DataType, int CAPACITY>
class Array{
public:
    Array();
    inline DataType& operator[](int);
    int getCapacity() const;
    int lsearch(const DataType&) const;

private:
    DataType data[CAPACITY];
    int index;
    DataType dummy;
};

template<class DataType, int CAPACITY>
Array<DataType, CAPACITY>::Array() {
}

template<class DataType, int CAPACITY>
inline DataType& Array<DataType, CAPACITY>::operator[](int index) {
    if (index < 0)
        return dummy;

    if (index >= CAPACITY)
        return dummy;

    return data[index];
}

template<class DataType, int CAPACITY>
int Array<DataType, CAPACITY>::getCapacity() const {
    return CAPACITY;
}

template<class DataType, int CAPACITY>
int Array<DataType, CAPACITY>::lsearch(const DataType& arg) const {
    for (int i = 0; i < CAPACITY; i++) {
        if(arg == data[i]) {
            return i;
        }
    }
    return -1;
}

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote