0

I had to write a C++ program for school and I got it work, but I cant figure out how to get it to keep the values to display the amount of times heads was tossed and tails was tossed. It keeps saying 0. These are the directions:Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses.

The program should have the following functions as a minimum:

void toss() - called from main() and will randomly toss the coin and set a variable equal to the face of the coin void count() - called from toss to increment counter for heads or tails void displayCount() - is called from main() and will display the values of the counter for heads and the counter for tails.NO GLoBAL VARIABLES!!

Following is the code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//prototypes
void toss(int headCount, int tailCount);
void count(int headCount, int tailCount);
void displayCount(int headCount, int tailCount);

//tosses a coin 100 itmes and tells you how many were heads and how many were tails.

int main()
{
    int headCount = 0;
    int tailCount = 0;
    int count = 0;

    toss(headCount, tailCount);  //toss coin
    displayCount(headCount, tailCount); //displays how many heads and tails

}

//***************function definitions***************

void toss(int headCount, int tailCount)
{
    srand(time(NULL));//makes the coin toss random

    for (int i = 0; i<100; i++)  //while it is less then 100 tosses
    {
        if (rand() % 2 == 0)   //assigns 0 to heads
        {
            cout << "heads";
        }
        else
        {
            cout << "tails";  //assigns 1 to tails
        }
    }
    count(headCount, tailCount);
}
void count(int headCount, int tailCount)
{
    if (rand() % 2 == 0)
    {
        headCount++;    //counts the amount of heads
    }
    else
    {
        tailCount++;   //counts the amount of tails
    }

}
void displayCount(int headCount, int tailCount)   //displays count of head and tails
{
    cout << "Number of heads: " << headCount << "\n";
    cout << "Number of tails: " << tailCount << "\n";
}   
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Lisa K
  • 1
  • 1
  • 3

1 Answers1

2
void toss(int headCount, int tailCount)

is pass by value, not reference, so what happens in toss stays in toss. Give

void toss(int &headCount, int &tailCount)

a try and then apply the same thinking to count.

On a deeper look, count should be called inside toss's for loop otherwise it will run only once. That means the current guts of the for loop need to be rethought. I would discard the count function entirely and use something like:

void toss(int &headCount, int &tailCount)
{
    srand(time(NULL));//makes the coin toss random. 
                      // Well... Sorta. More on that in a second.

    for (int i = 0; i<100; i++)  //while it is less then 100 tosses
    {
        if (rand() % 2 == 0)   //assigns 0 to heads
        {
            cout << "heads\n";
            headCount++;    //counts the amount of heads
        }
        else
        {
            cout << "tails\n";  //assigns 1 to tails
            tailCount++;   //counts the amount of tails
        }
    }
}

And a side-note:

srand is best invoked once in the program near the top of main. Every time you call srand it resets the random number generator. If you call srand(time(NULL)); again too quickly, time will not have had time to change to the next second and you'll generate the same numbers. In the few cases where you do need to call srand more than once per program, rand and srand are probably not the right tools for the job. Look into the <random> library added in C++11 or third party random number tools.

user4581301
  • 33,082
  • 7
  • 33
  • 54