Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- //function prototype
- void getHits(int arr[], int n);
- float getProbability(int num, int count);
- //global variable (*evil*) probability array
- float probability[6];
- int main(int argc, char* argv[]){
- //declare hits array
- int hits[6] = {0,0,0,0,0,0};
- //initialize number of trials
- int n;
- cout << "input number of trials"<< endl;
- cin >> n;
- //call to getHits()
- getHits(hits, n);
- //output probability
- for(int i = 0; i <6; i++){
- cout << "number of hits for J" << i + 1<< " is " << probability[i] << endl;
- }
- system("PAUSE");
- return 0;
- }
- /*getHits() takes an array arr[] and increments a cell by 1 when its corresponding statement evaluates to true and goes through the loop n times*/
- void getHits(int arr[], int n){
- int irandomNumber ;
- float randomNumber;
- //seed for psuedo-random numbers
- srand(time(NULL));
- for(int i = 0; i < n; i++){
- irandomNumber = rand()% 7;//we generate random numbers between 0 and 7
- randomNumber = static_cast<float>(irandomNumber)/6.0f;//a random hit probability
- /*check for hits*/
- if(randomNumber >= 0 &&randomNumber <= (1.0f/6.0f)) arr[0]++;
- if(randomNumber >= (1.0f/6.0f) && randomNumber <= (2.0f/6.0f))arr[1]++;
- if(randomNumber >=(2.0f/6.0f) && randomNumber <= (3.0f/6.0f)) arr[2]++;
- if(randomNumber >= (3.0f/6.0f )&& randomNumber <=(4.0f/6.0f)) arr[3]++;
- if(randomNumber >= (4.0f/6.0f) && randomNumber <= (5.0f/6.0f)) arr[4]++;
- if(randomNumber >= (5.0f/6.0f) && randomNumber <= 1) arr[5]++;
- }
- for(int i = 0; i < 6; i++){
- //calculates probability and assigns it to probability array
- probability[i] = getProbability(arr[i], n);
- }
- }
- /*calculates probability by dividing num with count and returns the result*/
- float getProbability(int num, int count){
- return float((float)num/count);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement