1 | initial version |
One possible way to create a counting game for coin flipping in C++ using data structures is as follows:
Here's an example implementation:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function to flip a coin and return "heads" or "tails"
string flipCoin() {
if (rand() % 2 == 0)
return "heads";
else
return "tails";
}
int main() {
// Seed the random number generator with the current time
srand(time(NULL));
// Prompt the user for the number of flips
int numFlips;
cout << "Enter the number of times you want to flip the coin: ";
cin >> numFlips;
// Create a vector to store the coin flip results
vector<string> flips(numFlips);
// Flip the coin and store the results in the vector
for (int i = 0; i < numFlips; i++) {
flips[i] = flipCoin();
}
// Count the number of heads and tails
int numHeads = 0;
int numTails = 0;
for (string result : flips) {
if (result == "heads")
numHeads++;
else
numTails++;
}
// Display the results to the user
cout << "Out of " << numFlips << " flips, there were " << numHeads << " heads and " << numTails << " tails." << endl;
return 0;
}
This program uses a vector to store the results of the coin flips, and then counts the number of heads and tails using a loop. The results are then displayed to the user.