[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]

/prog/ - Programming

Programming board

Catalog

8chan Bitcoin address: 1NpQaXqmCBji6gfX8UgaQEmEstvVY7U32C
The next generation of Infinity is here (discussion) (contribute)
A message from @CodeMonkeyZ, 2ch lead developer: "How Hiroyuki Nishimura will sell 4chan data"
Name
Email
Subject
Comment *
File
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Embed
(replaces files and can be used instead)
Options
Password (For file and post deletion.)

Allowed file types:jpg, jpeg, gif, png, webm, mp4, pdf
Max filesize is 8 MB.
Max image dimensions are 10000 x 10000.
You may upload 1 per post.


File: 1435392700544.jpg (26.16 KB, 381x500, 381:500, C primer.jpg)

aecc7c No.2719

So, I'm reading C++ Primer 5th edition, I get to chapter 5, and, I'm not sure how to go about solving one of the exercises.

When I get stuck I typically look at how the problem was solved here https://github.com/Mooophy/Cpp-Primer.

But there's a fatal error in how they solve it.

They use pair, and when I look that up in the back of the book, that type doesn't appear for about 230 more pages, and isn't even mentioned beforehand, as they sometimes do with "there's this we'll discuss later"

The exercise is as follows.

Write a program to read strings from standard input looking for duplicated words. The program should find places in the input where one word is followed immediately by itself. Keep track of the largest number of times a single repetition occurs and which word is repeated. Print the maximum number of duplicates, or else print a message saying that no word was repeated. For example, if the input is

how now now now brown cow cow

the output should indicate that the word now occurred three times.

I'm thinking that I'd start with a vector of strings, and a string variable, use a while loop to get input into the string variable, using .push_back to put the strings into the vector.

Then create a for loop to go through the string vector, comparing the current string to the next one, and upping a counter.

My issue being, what do I do to make it so, if I find say 3 of a word, and then 4 of a different word, make it use the 4, not the 3, and not have a counter just go from 3 to 7?

aecc7c No.2720

Me again, so, so far I have this

// string repetition

#include <iostream>

#include <vector>

#include <string>

using namespace std;

int main()

{

vector<string> words;

vector<int> counter(2, 0);

cout << counter[0] << " " << counter[1] << endl;

string word = " ";

int fuckyall = 0;

string quit = "quit";

while(cin >> word && word != quit)

{

words.push_back(word);

}

string otherWord;

int i = 0;

for(auto beg = words.begin(); beg < words.end(); beg++)

{

word = words[i];

i++;

if(word == words[i])

{

while(word == words[i])

{

counter[1] + 1;

i++;

}

}

else

{

if(counter[1] > counter[0])

{

counter[0] = counter[1];

fuckyall = i;

}

}

}

cout << "The word " << words[fuckyall] << " was repeated " << counter[0] << " times." << endl;

return 0;

}

but when I run it, I do the input, and then it crashes, and I'm not sure what exactly is causing it as someone fairly new to programming.


aecc7c No.2721

also after posting I recognized an error in not resetting counter[1] after putting its value into counter[0]


c7637f No.2724

>>2720

Add debug lines… and consider using .at() insted of [] on vectors


6c0105 No.2726

I wrote a solution for you, OP. I didn't end up using a vector, since you really only need to know two things each time you read a word:

- the previous word

- the number of times it has repeated

Also, instead of waiting for a magical "quit" word, I just used the eof() method to check for when the standard input file ended. At a terminal, you can indicate EOF in standard input by pressing CTRL-D on a new line. If you pipe a file to the command, it will automatically get an EOF at the end of the file.

I tried to put useful comments in place. Here it is:

#include <iostream>
#include <string>

using namespace std;

int main()
{
// The word which occurred most
string mostWord;
// The number of times it occurred (set to 1, since it's the minimum)
int mostCount = 1;

// The last word we read
string lastWord;
// The new word we'll be reading
string currWord;
// The count for the number of times that the current word has repeated
int currCount = 1;

cin >> lastWord;
while (cin >> currWord) {
bool eof = cin.eof();

if (lastWord == currWord && !eof) {
// If the new word is the same as the last one, increase the count
++currCount;
} else {
// Check if we set a new record for most consecutive occurrences. If
// we did, then update mostCount and mostWord accordingly.
if (currCount > mostCount) {
mostCount = currCount;
mostWord = lastWord;
}
// Reset lastWord and currCount
lastWord = currWord;
currCount = 1;
}
// If we've reached the end of the file (e.g. someone pressed CTRL-D at
// the terminal) then exit.
if (eof) break;
}

// Print output
if (mostCount > 1) {
cout << "The string " << mostWord << " occurred " << mostCount <<
" consecutive times.\n";
} else {
cout << "No duplicated words.\n";
}

return 0;
}


6c0105 No.2727

>>2726

Shoot, there are some problems with that code.

Please forgive me; I don't know C++ (only C, I had to look up the libraries to write this). Don't worry, this code doesn't leak any memory, at least (I actually ran it through valgrind because I *really* don't know C++, but it turned out alright).

Here is the revised code. You'll notice that we don't need to check for EOF, because apparently the >> operator does that for us on its own. I also added another test after the while loop; the reason for this will be left as an exercise for the reader.

#include <iostream>
#include <string>

using namespace std;

int main()
{
// The word which occurred most
string mostWord;
// The number of times it occurred (set to 1, since it's the minimum)
int mostCount = 1;

// The last word we read
string lastWord;
// The new word we'll be reading
string currWord;
// The count for the number of times that the current word has repeated
int currCount = 1;

cin >> lastWord;
while (cin >> currWord) {
if (lastWord == currWord) {
// If the new word is the same as the last one, increase the count
++currCount;
} else {
// Check if we set a new record for most consecutive occurrences. If
// we did, then update mostCount and mostWord accordingly.
if (currCount > mostCount) {
mostCount = currCount;
mostWord = lastWord;
}
// Reset lastWord and currCount
lastWord = currWord;
currCount = 1;
}
}

// We need to check once more whether the last word was the most repeated
if (currCount > mostCount) {
mostCount = currCount;
mostWord = lastWord;
}

// Print output
if (mostCount > 1) {
cout << "The string " << mostWord << " occurred " << mostCount <<
" consecutive times.\n";
} else {
cout << "No duplicated words.\n";
}

return 0;
}


aecc7c No.2728

>>2727

Thanks, since I just got off a 12 hour day I'll save it and read over it later, I was probably over thinking the problem when I wrote the code… it was 1am and I hadn't slept in a couple days… fucking 80 degree nights




[Return][Go to top][Catalog][Post a Reply]
Delete Post [ ]
[]
[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]