I'm new to C++ and am not doing so well in my class right now. I have an assignment due tomorrow that I'm really struggling on. I emailed my teacher and he helped a little,but I'm still lost...any help with this assignment would be greatly appreciated. Heres the assignment...I'm still on part one.
Write a program that simulates flipping a coin. Count how many times the result is "heads" and how many times the result is "tails".
Ask the user how many times to flip the coin. After they answer, start the random number generator (see below). Then flip your simulated coin the requested amount of times. Print "H" or "T" after each "flip". Since the rand() function produces random integers, if the number is even, you can call it "Heads", and if it's odd, you can call it "Tails".
After you repeat your loop the requested number of times, print the total number of "Heads" and the total number of "Tails".
Include cstdliband ctime to make the random number function work:
#include <cstdlib>
#include <ctime>
using namespace std;
Use this statement to start the random number generator AFTER the user enters the number of coin flips desired:
// The delay for the user to enter number of coin flips gives a
// random time interval. Now we can start the random number
// generator using the clock value.
srand(clock());
You can now use rand()%2 to get a random number. Even numbers will give a remainder of 0, and odd numbers will give a remainder of 1.
Sample run:
How many times would you like to flip a coin? 20
HHTTTTHTHHTTTHHTTHHT
There were 9 heads and 11 tails.
Save part 1.
Part 2:
Get part 1 working first to make sure you can flip and count one coin at a time before proceeding.
Now flip two coins at a time. Ask the user how many times to flip the two coins. Pick two random numbers (two coins) each time through the loop. Count how many times (1) both are "heads", (2) both are "tails", (3) one is "heads" and the other is "tails". After the loop finishes, print each of the three totals.
This is what I have: Im not sure what to put in the while part.
Here are some hints to help you solve the assignment.
1. Keep a counter to track the number of iterations in the while loop and exit out of the loop once the counter reaches the user defined maximum.
2. Similarly, keep two more counters to count the number of heads and tails.
You might want to use while to do 'n' coin flips and store the results in variables.
Pseudocode example:
while ( I still need to flip coins )
{
flip the coin and determine whether or not it was heads
update count of how many heads I had
or
update count of how man tails I had
}
now we have a record of how many tails and heads we flipped.
I am keeping this generic so you can fill in the code. Take this example:
I've inserted an integer n and placed what you have into the while loop, but I'm not sure what to put inside the braces to count how many heads/tails there are..