c++ - Search through arrays to find seperate instances of a word -
this assignment in advanced c++ class, we're going on pointers , dynamically allocated arrays, though doesn't seem trouble lies...
function header, quote, words, , count important arrays, numwords holds count of how many elements in quote array.
void countwords ( char ** quote, char **& words, int *& count, int numwords ) {
variables , such, of arrays being passed pointer main.
char ** temp = nullptr; int * itemp = nullptr; int wordcount = 0; int quotecount = 0;
what follows priming read for loop coming up. i'm creating 2 dynamically allocated arrays, 1 store new found instances of word in quote array, , store count of how many times word appeared in quote array. seems working fine, don't how large (code bloat) advice started differently excellent.
first dynamic array char's.
temp = new char * [wordcount + 1]; (int z = 0; z < wordcount; z++) { temp[z] = words[z]; } temp[wordcount] = new char[ strlen( quote[wordcount]) +1 ]; strcpy( temp[wordcount], quote[wordcount] ); delete [] words; words = temp;
second dynamic array int's.
itemp = new int [ wordcount + 1 ]; (int z = 0; z < wordcount; z++) { itemp[z] = count[z]; } itemp[wordcount] = 0; delete [] count; count = itemp;
this loop supposed use new value in **char array, iterate through quote find other instances of value, , increments count @ same index number element. seems work...
for (int j = 0; j < numwords; j++) { if (_stricmp( words[ wordcount ], quote[j]) == 0 ) { count[ wordcount ]++; } }
counters how many words have been stored in words array, , in quote array function is.
wordcount++; quotecount++;
this things start go wonky...
for (int = 0; < numwords; i++) { //right here program breaks, after second iteration of //for-loop. happens loop counter (i) increments 2 shortly //before words[2] gets created. i've tried decrementing inside if, //where quotecount gets incremented, causes out-of-bounds error //on quote array on last iteration. //check see if quote value in words if (_stricmp( quote[ quotecount ], words[ ]) == 0 ) { //if true, move on next element in quote array. quotecount++; } //if false, write quote value words else { temp = new char * [wordcount + 1]; (int z = 0; z < wordcount; z++) { temp[z] = words[z]; } temp[wordcount] = new char[ strlen( quote[wordcount]) +1 ]; strcpy( temp[wordcount], quote[wordcount] ); delete [] words; words = temp; //create new array element in count track new value in words itemp = new int [ wordcount + 1 ]; (int z = 0; z < wordcount; z++) { itemp[z] = count[z]; } itemp[wordcount] = 0; delete [] count; count = itemp; //check quote other instances of word (int j = 0; j < numwords; j++) { if (_stricmp( words[ wordcount ], quote[j]) == 0 ) { count[ wordcount ]++; }//if }//for //increment word counter, indicating new word stored. wordcount++; }//else }//for }//function
i feel has become far more complicated needs be. tried nesting for-loops begin with, couldn't seem working either. thing is, once copies word quote words, shouldn't copy word again.
also, general input on code quality , such appricated. i'm striving become software engineer later in life, , best @ do, need learn as can time.
your main issue seem want step through quote using i
, @ same time using index words
. more logical way things check each word of quote against existing words have copied words
, , either incrementing count or inserting word accordingly. can take advantage of fact there @ numwords
in words
, allocate enough memory @ start of function:
void countwords(char const** quote, char**& words, int*& count, int numwords) { words = new char*[numwords]; count = new int[numwords]; int words = 0; (int = 0; < numwords; ++i) { int j = 0; (; j < words; ++j) { if (!strcmp(quote[i], words[j])) { // duplicate word ++count[j]; break; } } if (j == words) { // new word found words[words] = new char[strlen(quote[i]) + 1]{}; strcpy(words[words], quote[i]); count[words] = 1; words++; } } count[words] = 0; } int main() { char const* quote[] = {"hello", "world", "hello", "world"}; char** words; int* count; countwords(quote, words, count, 4); (int = 0; count[i]; ++i) { std::cout << words[i] << ' ' << count[i] << '\n'; delete[] words[i]; } delete[] words; delete[] count; }
in case array / pointer shenanigans hard read , error-prone, there hardly code here (or original code) called "c++" aside new
, it's c dash of c++. entire app can more written in modern c++ this:
#include <iostream> #include <unordered_map> int main() { std::string word; std::unordered_map<std::string, int> map; while (std::cin >> word) ++map[word]; (auto const& w : map) std::cout << w.first << " : " << w.second << '\n'; }
and used this: app < textfile.txt
Comments
Post a Comment