Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Parallel arrays counting word occurrence from text file C++. I'm supposed to read in one word at a time then call my countWords function to

Parallel arrays counting word occurrence from text file C++.

I'm supposed to read in one word at a time then call my countWords function to count how many times that word appears in the file, increment, until all words have been accounted for. Currently it is throwing an exception error on the line commented below in my countWords function:

temp_word[counter] = words[counter + 1]; temp_count[counter] = count[counter + 1];

I'm not sure how to initialize count. I tried doing it like int* count = new int[total], tried setting all of count to 0 in a loop, and just tried setting it to 0. I can't figure out why this is happening. The arrays do need to have dynamic length and be parallel. We also can't use maps, vectors, or strings (even if it's more efficient it's a requirement). Any help would be greatly appreciated, thank you!!

#define _CRT_SECURE_NO_WARNINGS #include #include #include #include

using std::cin; using std::cout; using std::endl; using std::ifstream; using std::ofstream; using std::setw; using std::right; using std::left;

const char DATA_IN[] = { "data.txt" }; const char DATA_OUT[] = { "report.txt" };

void readWords(int& total, char**& words, int*& count, char* buffer); //void writeFile(int& total, char** words, int* count); //void outputFile(int& total, char** words, int* count); void countWords(int& total, char** words, int* count, char* buffer);

int main() { ifstream input (DATA_IN); ofstream output (DATA_OUT);

int total = 0; char** words = nullptr; int* count = nullptr; char buffer[256];

if (input.is_open()) { // load the file readWords(total, words, count, buffer);

// write to the file //writeFile(total, words, count);

//// output to the screen //outputFile(total, words, count); } else cout << "Error opening file!";

input.close(); return 0; }

// read words from text file void readWords(int &total, char**& words, int*& count, char* buffer) { ifstream input(DATA_IN);

total = 100; buffer[256];

words = new char* [total]; // copy initial words into dynamic array for (int i = 0; i < total; i++) { input >> buffer; int temp = strlen(buffer) + 1; words[i] = new char[temp]; strcpy_s(words[i], temp, buffer); // copy buffer to words countWords(total, words, count, buffer); // call process function }

// free allocated memory for (int i = 0; i < total; i++) { delete[] words[i]; } delete[] words;

}

// count how many times a word is found in the file, one at a time use stricmp void countWords(int& total, char** words, int* count, char* buffer) { bool found = false; int found_word = 0; // total number of words in input file

// STEP 1: find words to be deleted while (found_word < total && !found) { // comparing words if (_stricmp(words[found_word], buffer) == 0) { found = true; // word has been found } else { ++found_word; } } // if word is already in array if (found) { // allocate new array of temp_word pointers one larger than the current number of words char** temp_word = new char* [total + 1]; int* temp_count = new int[total + 1];

int counter = 0; // STEP 3 : make new array point to any words located before the one to delete while (counter < total) {

temp_word[counter] = words[counter + 1]; temp_count[counter] = count[counter + 1]; // it is throwing an exception here, count has no value, i tried setting all to 0 in a loop ++counter; } // STEP 4 : deallocate desired word delete[] words[found_word]; // STEP 5 : make new array point to any words located after the one deleted for (int i = 0; i < total; i++) { temp_word[i] = words[i]; } // STEP 6 : deallocate stock array and assign stock pointer to new array delete[] words; words = temp_word; } }

//write list of words to file void writeFile(int& total, char** words, int* count) { ofstream output (DATA_OUT);

if (output.is_open()) { // for each entry, write to the screen // currently for each match it is creating a null value? for (int i = 0; i < total; i++) { output << words[i] << endl; } } else { cout << "cannot open file!"; } output.close();

}

// output word frequency occurence to screen void outputFile(int& total, char** words, int* count) { cout << "Word Frequency Analysis" << endl; cout << "Word\t" << right << setw(5) << "Frequency" << endl; // for each entry, print out words and word frequency for (int i = 0; i < total; i++) { cout << left << setw(10) << words[i] << right << setw(5) << count[i] << endl; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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