Hello,
I've got two projects on the go (and a possible third this weekend). I posted my first project (to do with primes) a few days ago, and was overwhelmed with the replies! I've got a lot of reading to do for that one.
I've been dealing with this other program that i've written. I've posted it before (and changed it according to responses). Numerically, it's fine. The problem is to do with columns.
Without further ado, here's what i've got:
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
|
/*
Calculates the probabilities of getting exactly i things in a sample size of n, if there is p chance of getting one of the things in the first place.
(ie. if there are 3 lightbulbs, and there is a 0.25 chance of a bulb
being broken, then outputs:
P(x=0)
P(x=1)
P(x=2)
P(x=3)
)
Outputs single probabilities and cumulative.
*/
#include <iostream>
#include <cmath>
#include<iomanip>
using namespace std;
unsigned long long int fact(int n);
unsigned long long int C(int n, int r);
int main(){
cout<<setfill('-')<<setw(80)<<"-"<<endl;
cout << "You can use this to find the probabilities for a binomial expansion." << '\n';
cout << "Please check that the individual probabilities add up to 1." << '\n';
cout<<setfill('-')<<setw(80)<<"-"<<endl;
for(; ; ){ int n;
cout << "Please enter the sample size: ";
cin >> n;
double p;
cout << "Enter the probability of a certain behaviour occurring: ";
cin >> p;
long double k = 0;
int i;
long double j;
for(i = 0; i <= n; i++){
j = C(n,i)*pow(p,i)*pow(1-p,n-i);
cout << "P(x = " << i << ")=" << j;
cout << setfill(' ') << setw(30);
cout << "P(x <= " << i << ")=" << j+k;
cout << '\n';
k = j+k;
} }
return 0;
}
unsigned long long int fact(int n){
unsigned long long int t;
unsigned long long int answer;
answer = 1;
for(t=1;t<=n;t++) answer = answer * t;
return(answer);
}
unsigned long long int C(int n, int r){
unsigned long long int a = 1;
unsigned long long int p = n-r;
for(n; n>p; n--){ a = a*n; }
a = a/fact(r);
return a;
}
|
Here's an example of an output:
--------------------------------------------------------------------------------
You can use this to find the probabilities for a binomial expansion.
Please check that the individual probabilities add up to 1.
--------------------------------------------------------------------------------
Please enter the sample size: 10
Enter the probability of a certain behaviour occurring: 0.5
P(x = 0)=0.000976562 P(x <= 0)=0.000976562
P(x = 1)=0.00976562 P(x <= 1)=0.0107422
P(x = 2)=0.0439453 P(x <= 2)=0.0546875
P(x = 3)=0.117188 P(x <= 3)=0.171875
P(x = 4)=0.205078 P(x <= 4)=0.376953
P(x = 5)=0.246094 P(x <= 5)=0.623047
P(x = 6)=0.205078 P(x <= 6)=0.828125
P(x = 7)=0.117188 P(x <= 7)=0.945312
P(x = 8)=0.0439453 P(x <= 8)=0.989258
P(x = 9)=0.00976562 P(x <= 9)=0.999023
P(x = 10)=0.000976562 P(x <= 10)=1
Please enter the sample size: |
As you can see, the second column isn't in line.
I tried using the setw and setfill commands (which led me to the first line of "-"), but I can't get the columns to line up. A lot of the tutorials like this:
http://www.cprogramming.com/tutorial/iomanip.html
seem to do everything using arrays (or vectors?). I haven't encountered this yet, so i'm trying to find a way of doing it by editing this part:
1 2 3 4
|
cout << "P(x = " << i << ")=" << j;
cout << setfill(' ') << setw(30);
cout << "P(x <= " << i << ")=" << j+k;
cout << '\n';
|
to include setw. I tried writing left and right, but that didn't help.
As with all my threads, i'd appreciate any help.