Hello! I am needing some help with getting this code to work. I know this is a homework assignment, but I am unable to get everything working. Our teacher gave us a binomial distribution assignment to calculate a coin flip.
My problem is that he is wanting us to calculate the probability of tossing a coin 10 times and getting exactly 0,1,2,3, ect. heads. Success is 0.5 and fail is 0.5 chance. He wants us to store the probability in an array and to use a "for" loop from 0-10 where r is the number of successes. I have slowly written step-by-step the different calculations, first the denominator then the numerator and so forth.
The binomial distribution code is:
p(x)=N!/X!(N-X)!*(0.5)^x(1-0.5)^N-X
I have gotten the code to show the calculations for each section so far except the exponent array. I need to store these values in an array I assume because they have to be accessed later on so since the problem has you raise the value of 0.5 to N-x, the values need to descrease starting with 10 is what I figured so I would go from 10 to 0 backwards, store that value and then call it when I calculated the answer, but I am getting a conflicting error saying "invalid types double [11] [double]" where I calculate the exponent.
Please remember this is my very first year of C++ and I just started the class this fall. Any hints would be appreciated. I have read my chapters in my book and I am racking my brain about this code. it is due today at midnight and I have been working on it for days now. Please, any help or hints would be amazing. The teacher and other students are unhelpful and this is an online class.
Here is my code so far, it is a work in progress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <iostream> //to use cout and cin and endl// allows using cout without std::cout
#include <string> // to use string data type
#include <cstring> //to use strlen, strcmp, strcpy
#include <cmath> //for pow function,sqrt,abs
#include <iomanip> // for set precision, setw,
#include <fstream> //for file input
#include <cassert> // to use assert to disable place before #define NDEBUG
#include <cstdlib>//for random numbers, exit function
#include <ctime>//time function used for rand seed
#include <cctype> // for toupper, tolower
#include <algorithm>
#include <locale.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
double factorial=1.0; //an integer would be too small
int row =1;
const double N=3628800;
double firstPart=0.0;
int counter=1;
double denominator=0;
double second[11]={0.0};
double third=0.0;
int r=0;
double exponent[11]={0.0};
double x=10.0;
double fourthPart=0.0;
double answer[11]={0.0};
double factorialarray[30]={0};//rows 0-29
cout<<fixed<<setprecision(0);
//calculate factorials 0-10
for(row=0;row<=10;row++)//outer loop //here is where I calculate the factorials needed for later on. This section was written by the teacher.
{
factorial=1.0;
for(counter=row;counter>=1;counter--)
{
factorial=factorial*counter;
}//bottom of inner loop
factorialarray[row]=factorial;
}//bottom of outer loop
//My code starts here
//We have to have a header for r and for the decimal probability.
cout<<"Number of flips Probability of Heads"<<endl;
cout<<" "<<endl;
//here is where I am calculating (0.5)^N-X
for (double x=10.0;x>0.0;x--)
{
exponent[x]=pow(0.5,x);//error here stating the double [11] [double] problem
//Start of positive loop to do everything together
for (int r=0;r<=10;r++)
{
denominator=factorialarray[r]*factorialarray[10-r]; //denominator calculation
firstPart=N/denominator;
second[row]=pow(0.5,r);
answer=firstPart*second[row]*exponent[x]; //I made a fourth part, but the calculations were giving me higher numbers
cout<<setw(4)<<r<<setw(15)<<setprecision(4)<<answer<<endl;
}
}
return 0;
}
|