r/CodingHelp • u/OperationVisual6523 • 3d ago
[C++] I need help with my code (Cpp Markov Chain, PickRandomWord part)
Its in c++ and the code im trying to make is for a markov chain. Its an assignment my teacher has given for extra credit and im trying to do it. Im kinda lost, i sent my professor an email and so far she hasn't responded at all.
So i figured to come here on reddit and hopefully figure out where im going wrong. So far i only have one of the three required components for the markov chain and i don't really want to use chat or gem to see whats wrong. So yeah if anyone's up for it, please help? That'd greatly be appreciated.
Heres my code on the bottom:
Here, for anyone who wants to see their code in code blocks:
map<string, int> ticket;
map<string, int>::iterator itr;
map<string, int> winning_tickt;
int totalTickets = 0;
for (const auto& word : wordsAndCounts) {
totalTickets += word.second;
}
if (totalTickets == 0) {
throw runtime_error("Couldn't select a random word");
}
winning_tickt["Winning_ticket"] = rand() % totalTickets;
for (int i = 0; i < totalTickets; i++) {
ticket["Ticket" + i] = i;
}
// First idea:
// while (ticket != winning_tickt) {
// for (itr = ticket.begin(); itr != ticket.end(); itr++) {
// if (ticket == winning_tickt) {
// cout << "Found it!";
// }
// }
// }
// Second idea:
while (ticket != winning_tickt) {
for (itr = ticket.begin(); itr != ticket.end(); itr++) {
if (ticket == winning_tickt) {
return ticket["winner"];
}
}
}Alright to put it short, im trying to make the pickrandomword part, made two maps, one for all the tickets and one for the winning ticket. I have an iterator so it traverse each ticket and find the winning ticket or is equal to the winning ticket. I have totaltickets set to zero so i can use the for loop to add counts of all the tickets to my totaltickets since the perimeter is a const variable. I was hoping for the 2nd for loop to be the way i traverse each ticket while the winning ticket hasnt been for yet! And im trying to think of what to do once the winn I ng ticket has been found but im getting stumped. So yeah. Please..help.
1
u/gofuckadick 3d ago
Here, for anyone who wants to see their code in code blocks:
map<string, int> ticket;
map<string, int>::iterator itr;
map<string, int> winning_tickt;
int totalTickets = 0;
for (const auto& word : wordsAndCounts) {
totalTickets += word.second;
}
if (totalTickets == 0) {
throw runtime_error("Couldn't select a random word");
}
winning_tickt["Winning_ticket"] = rand() % totalTickets;
for (int i = 0; i < totalTickets; i++) {
ticket["Ticket" + i] = i;
}
// First idea:
// while (ticket != winning_tickt) {
// for (itr = ticket.begin(); itr != ticket.end(); itr++) {
// if (ticket == winning_tickt) {
// cout << "Found it!";
// }
// }
// }
// Second idea:
while (ticket != winning_tickt) {
for (itr = ticket.begin(); itr != ticket.end(); itr++) {
if (ticket == winning_tickt) {
return ticket["winner"];
}
}
}I think you're overcomplicating the ticket system a bit. You don't actually need to create a separate map containing every ticket. You've already got the right idea with totalTickets and rand() % totalTickets.
Think of each word as owning a range of tickets equal to its count. For example, if "cat" appears 3 times, "dog" twice, and "bird" 5 times, you have 10 tickets total. Cat owns 0–2, dog owns 3–4, and bird owns 5–9. Once you've generated the winning ticket, you can iterate through wordsAndCounts and use the counts to determine which word owns it.
Also, ticket == winning_tickt compares the entire maps, not individual entries. Since neither map changes inside your while loop, that loop will never terminate if the maps start out unequal.
You're actually pretty close with the first part of your approach. I'd focus on figuring out how to select a word using the counts you already have, rather than building another map of tickets.
Oh and watch out for "Ticket" + i - that's pointer arithmetic on a string literal, not string concatenation.
•
u/AutoModerator 3d ago
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.