Question

In: Computer Science

Write a program that determines the probability of tossing a coin 10 times and getting exactly...

Write a program that determines the probability of tossing a coin 10 times and getting exactly 0, 1, 2, 3, etc. heads. This is the binomial probability distribution. Store the probability in an array. You could get 0 heads or 10 heads or anything inbetween.

Use a for loop. The for loop will go from 0 to 10 inclusive. Use r as the number of successes. So r will go from 0 to 10. The probability of a success is .5, and also the probability of a failure is .5.

Print out in table form, column 1=r; goes 0 to 10, and then column 2; the probability of r.

Use 4 decimal places for the probability. You know if you get the correct answers because The sum of all the probabilities is 1.0, and all probabilities are in the range of 0 to 1 inclusive.

In C++, prefer visual studios but not required.

Solutions

Expert Solution

The code is as follows:

#include <iostream>
#include<cmath>
#include <sstream>
#include <iomanip>

using namespace std;


// Returns factorial of n
int fact (int n)
{
int s = 1;
for (int i = 2; i <= n; i++)
    s = s * i;
return s;
}

int nCr (int n, int r)
{
return fact (n) / (fact (r) * fact (n - r));
}

int main ()
{
double prob[11][2];       //array to store the probability values
double p = 0.5;       //probability of pass
double f = 0.5;       //probability of failure
double x = 0;
int n = 10;           //number of tries

for (int i = 0; i <= 10; i++)
    {
      x = nCr (n, i) * pow (p, i) * pow (f, n - i); // The formula is used to calculate the probability
      //following three lines of code is used for rounding the value to 4 decimal places
      stringstream tmp;
      tmp << setprecision(4) << fixed << x;
      double new_val = stod(tmp.str());
      prob[i][0] = i;
      prob[i][1] = new_val;
    }

cout << "The probability of tossing a coin 10 times and getting exactly 0, 1, 2, ... ,10 heads is as follows: "    << endl;

for (int j = 0; j <= 10; j++)
    {
        cout << "X = " << prob[j][0] << "   P = " << prob[j][1] << endl;
    }

return 0;
}


Related Solutions

In tossing a fair coin 16 times, what is the probability of getting at most 4...
In tossing a fair coin 16 times, what is the probability of getting at most 4 tails?
When tossing a fair coin three times what is the probability of getting 0, 1, 2,...
When tossing a fair coin three times what is the probability of getting 0, 1, 2, or 3 heads (as opposed to tails)? Write the answers in fractional notation, corresponding to the order given.
a)  A coin is flipped 6 times, find the probability of getting exactly 4 heads.  Hint: The Binomial...
a)  A coin is flipped 6 times, find the probability of getting exactly 4 heads.  Hint: The Binomial Distribution Table can be very helpful on questions 19-21.  If you use the table for this question, give your answer exactly as it appears.  If you calculated your answer, round to the thousandths place. b) A coin is flipped 6 times. Find the probability of getting at least 3 heads. If you used a table to help find your answer, give it to the thousandths place....
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Suppose you toss a fair coin 10 times. (a) Calculate the probability of getting at least...
Suppose you toss a fair coin 10 times. (a) Calculate the probability of getting at least 6 heads, using the exact distribution. (b) Now repeat the calculate above, but approximate the probability using a normal random variable. Do your calculation both with and without the histogram correction. Which one is closer to the true answer? Now suppose you toss a fair coin 1000 times. (c) What is the probability of getting at least 520 heads? You can approximate this using...
What’s the probability of getting no heads after flipping a fair coin 10 times? What’s the...
What’s the probability of getting no heads after flipping a fair coin 10 times? What’s the probability of getting no 3’s after rolling a fair 6-sided die 9 times? What’s the probability of getting a 4 at least once after rolling a fair 4-sided die 5 times? What’s the probability of getting a 5 exactly once after rolling a fair 8-sided die 7 times?
Consider flipping a fair coin 10 times. What is the probability that (a) you get exactly...
Consider flipping a fair coin 10 times. What is the probability that (a) you get exactly 7 heads? (b) you get at least 4 heads? (c) you get two more heads than tails?
A coin will be tossed 7 times. Find the probability that there will be exactly 2...
A coin will be tossed 7 times. Find the probability that there will be exactly 2 heads among the first 4 tosses, and exactly 2 heads among the last 3 tosses. (Include 2 digits after the decimal point.)
I toss a fair coin 20 times. (a) Calculate the probability of tossing 18 or more...
I toss a fair coin 20 times. (a) Calculate the probability of tossing 18 or more heads exactly. (b) Now perform the same calculation, approximating the actual binomial distribution with a normal distribution, picking a proper random variable, and using the correct mean and variance. (c) Do the results reasonably agree?
A coin is tossed 50 times. Find the probability that the tail appears. A) Exactly  30 times...
A coin is tossed 50 times. Find the probability that the tail appears. A) Exactly  30 times B at least 35 times. Using normal distribution
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT