I created this C++ program for school, creating a custom priority queue class using binary min heap to simulate a Mario Kart race
This program demonstrates my understanding of how priority queues and unordered maps work, which are essential data structures in programming
cout << endl << "Interval " << intervalCount << endl << endl; while(!mariokart.isEmpty()){ cin >> checker; //Wiping "END" and updating input with a racer name, as we start the interval while(checker != "DONE"){ string racerName = checker; //Storing the original value read in for(int i = 0; i < checker.size(); i++){ checker[i] = toupper(checker[i]); //Making the racer name capitalized, as it is the key to all map checks } //Checking if we update the racer or not: Using an unordered map, with keys as racername all capitalized, and boolean values auto racerUpdateIterator = racerUpdateCheck2.find(checker); if(racerUpdateIterator->second == true){ // cout << "Already updated " << racerName << " in this interval!" << endl << endl; //Due to formatting, already updated comes first } else if(racerFinished.find(checker)->second != 0){ cout << "Racer has already finished the race!" << endl << endl; //If hasn't been updated, then check if the user finished the race } else{ //Otherwise...update the value cin >> racingUnit; //Read in the next value as a double, the new distance away from finish line totalRacerCheck.find(checker)->second = true; racerUpdateCheck2.find(checker)->second = true; cout << "Updating " << racerName << endl; //Update value mariokart.decrease_priority(racerNames[checker], racingUnit); //Updating my priority queue of racers if(racingUnit <= 0){ racerFinished[checker] = racerPosition; //Assign racerPosition to racer that finished //Erasing from my totalRacerCheck if we go below value 0. This is to prevent continously checking //for values that already finished for(auto i = totalRacerCheck.begin(); i != totalRacerCheck.end(); i++){ if(i->first == checker){ totalRacerCheck.erase(i); break; } } } }
Figuring out loop logic using priority queues and unordered maps was a learning process, and took some time. After tinkering with the algorithm and drafting it on paper though, it all came together.
A slight issue occurred in meeting the exact formatting required for submission, causing several adjustments to my loop logic.