diff --git a/pretext/LinearBasic/SimulationHotPotato.ptx b/pretext/LinearBasic/SimulationHotPotato.ptx index ff4b6670..aab0d7e1 100755 --- a/pretext/LinearBasic/SimulationHotPotato.ptx +++ b/pretext/LinearBasic/SimulationHotPotato.ptx @@ -37,12 +37,12 @@ versions of this story. Some count every third man and some allow the last man to escape on a horse. In any case, the idea is the same.

We will implement a general simulation of Hot Potato. Our program - will input an array of names and a constant, call it num, to be used for + will input a vector of names and a constant, call it num, to be used for counting. It will return the name of the last person remaining after repetitive counting by num. What happens at that point is up to you.

To simulate the circle, we will use a queue (see ). The names are first loaded into the queue in the - same order as they appear in the input array, so the first name in the array + same order as they appear in the input vector, so the first name in the vector becomes the front of the queue. Assume that the child holding the potato will be at the front of the queue. Upon passing the potato, the simulation will simply dequeue and then immediately enqueue that child, putting her @@ -79,18 +79,22 @@ #include <iostream> #include <queue> #include <string> +#include <vector> using namespace std; -string hotPotato(string nameArray[], int num) { +string hotPotato(const vector<string>& names, int num) { queue<string> simqueue; - int namelsLenght = nameArray->length(); - for (int i = 0; i < namelsLenght; i++) { - //puts the entire array into a queue. - simqueue.push(nameArray[i]); + + //puts all the players in the queue + for (size_t i = 0; i < names.size(); i++) { + simqueue.push(names[i]); } -while (simqueue.size() > 1) { //loop continues until there is one remaining item. + //loop continues until there is one remaining item. + while (simqueue.size() > 1) { + + //simulate one round for (int i = 0; i < num; i++) { simqueue.push(simqueue.front()); simqueue.pop(); @@ -102,9 +106,9 @@ while (simqueue.size() > 1) { //loop continues until there is one remaining i } int main() { - string s[] = {"Bill", "David", "Susan", "Jane", "Kent", "Brad"}; + vector<string> players = {"Bill", "David", "Susan", "Jane", "Kent", "Brad"}; - cout << hotPotato(s, 7) << endl; + cout << hotPotato(players, 7) << endl; return 0; } @@ -138,11 +142,11 @@ main()

Note that in this example the value of the counting constant is greater - than the number of names in the array. This is not a problem since the + than the number of names in the vector. This is not a problem since the queue acts like a circle and counting continues back at the beginning - until the value is reached. Also, notice that the array is loaded into - the queue such that the first name on the array will be at the front of - the queue. Bill in this case is the first item in the array and + until the value is reached. Also, notice that the vector is loaded into + the queue such that the first name in the vector will be at the front of + the queue. Bill in this case is the first item in the vector and therefore moves to the front of the queue. A variation of this implementation, described in the exercises, allows for a random counter.