Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Count how many times a word appears in a text file with dynamic arrays: I'm very confused since we've introduced dynamic arrays. I'm supposed to

Count how many times a word appears in a text file with dynamic arrays:

I'm very confused since we've introduced dynamic arrays. I'm supposed to read in a file (in this case a sentence) and count how many times each word appears in it. I've been trying to go off an older lab where we read in the file using a buffer, copy it into a temporary variable, then copy that value into my words variable. I'm not sure if this is all necessary. I tried creating the countWords function by incrementing count if two words are found equal, otherwise I add a new word and new count. My first problem is that count is uninitialized, and I'm not sure how to go about that. Words is supposed to keep track of the specific word being read in, and count is supposed to keep track of how many times that appears (they are both parallel arrays). In the end, I am going to output each word (with my words variable), and how many times it appears with count. This lab is to help us get used to pointers and dynamic memory so it's important that I utilize those.

I really just need help being pointed in the right direction, I do want to understand how everything is working together, which is seeming to be my issue. If anyone could help me get started fixing my count function or explaining anything else wrong in my code I would greatly appreciate it.

#define _CRT_SECURE_NO_WARNINGS #include #include #include #include

using std::cin; using std::cout; using std::endl; using std::ifstream; using std::ofstream;

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

int readWords(int& entries, char**& words, int*& count); void writeFile(int entries, char**& words, int*& count); void outputFile(int entries, char**& words, int*& count); void countWords(int entries, char**& words, int*& count);

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

int entries = 0; char** words = nullptr; int* count;

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

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

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

input.close(); return 0; }

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

entries = 0; words = nullptr; char** temp = nullptr; char buffer[256];

input >> buffer; // priming read

if (input.is_open()) // if the file is open { while (!input.eof()) // while it is not end of file { temp = new char* [entries + 1]; // allocate memory for temp + null for (int i = 0; i < entries; i++) { temp[i] = words[i]; // copy words to temp }

temp[entries] = new char[strlen(buffer) + 1]; // set buffer length + 1 for null char strcpy(temp[entries], buffer); // copy buffer into temp variable delete[] words; words = temp; // copy value in temp to words

entries++; // move on to next word input >> buffer; // input 1 word

// call process function countWords(entries, words, count);

} } return entries; // return entries } // count how many times a word is found in the file, one at a time use stricmp void countWords(int entries, char**& words, int*& count) { count = new int[entries]; char** temp = nullptr; char buffer[256];

int i, j;

// find word for (int i = 0; i < entries; i++) { for (int j = 0; j < entries; j++) { // if two words are found to be equal if (strcmp(words[i], words[j]) == 0) { // increment count count[entries]++; } else { // add word words = new char* [entries]; // add count of 1 count = new int [entries + 1]; } } } }

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

// making sure the file is open if (output.is_open()) { // for each entry, write to the output for (int i = 0; i < entries; i++) { output << words[i] << endl; }

} output.close(); }

// output word frequency occurence to screen void outputFile(int entries, char**& words, int*& count) { // header cout << "Word Frequency Analysis" << endl; cout << "Word\t" << "Frequency" << endl;

// for each entry, print out words and word frequency for (int i = 0; i < entries; i++) { cout << words[i] << '\t' << count[i] << endl; } }

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

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions