Aligning columns. Yes, that problem.

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.
When using setw() you need to apply it every time you want to space a value. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
        cout << setfill(' ') << right << fixed << showpoint << setprecision(1); // Set the "sticky" manipulators just one time.
        for(i = 0; i <= n; i++)
        {

            j = C(n,i)*pow(p,i)*pow(1-p,n-i);

            cout << "P(x = " << setw(2) << i <<  ") =" << setw(p+1) << j;  // The width of j varies with p so use p to set the width.
            cout << setw(10) << "P(x <= " << setw(2) << i << ")=" << setw(p+1) << j+k;
            cout << '\n';

            k = j+k;

        }


Which produces the following output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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: 12
Enter the probability of a certain behaviour occurring: 22
P(x =  0) =     7355827511386641.0   P(x <=  0)=     7355827511386641.0
P(x =  1) =   -92473260143146336.0   P(x <=  1)=   -85117432631759695.0
P(x =  2) =   532822117967652736.0   P(x <=  2)=   447704685335893041.0
P(x =  3) = -1860648665918787328.0   P(x <=  3)= -1412943980582894287.0
P(x =  4) =  4385814712522855936.0   P(x <=  4)=  2972870731939961649.0
P(x =  5) = -7351460851466882048.0   P(x <=  5)= -4378590119526920399.0
P(x =  6) =  8985118818459523072.0   P(x <=  6)=  4606528698932602673.0
P(x =  7) = -8068269959433040896.0   P(x <=  7)= -3461741260500438223.0
P(x =  8) =  5282795806771634176.0   P(x <=  8)=  1821054546271195953.0
P(x =  9) = -2459714449713776640.0   P(x <=  9)=  -638659903442580687.0
P(x = 10) =   773053112767186944.0   P(x <= 10)=   134393209324606257.0
P(x = 11) =  -147248211955654656.0   P(x <= 11)=   -12855002631048399.0
P(x = 12) =    12855002631049216.0   P(x <= 12)=                  817.0
Please enter the sample size:

Yeah don't worry about it too much @OP. iomanip does take a little getting used to, unlike vectors or strings where it's usually pretty clear what each function in the given library does. Just gotta play around with it a bit.

setfill fills in your chosen spacefilling characters FROM the LEFT.

setprecision extends your output TO the number you set it starting FROM the LEFT

std::fixed (which is not in iomanip, but in std) modifies setprecision to start counting FROM the DECIMAL place

setw makes your MAXIMUM field length whatever you set it to.
Hi,

There is also std::left and std::right - they might help :+)
You might also consider converting what you want to format into a string. It is often easier to use the iomanip facilities on a logical chunk of text rather than on the individual pieces that compose it.

Modified slightly to make it a little more ideone friendly:
http://ideone.com/Q0N0Ub
Topic archived. No new replies allowed.