Segmentation fault?

Hello,

I'm trying to write a C++ binomial stats program.

For example, let's say I had a box of 3 lightbulbs. There is a 0.25 chance that a lightbulb is broken.

I want the program to output the following:

The chance of 0 lightbulbs being broken
The chance of exactly 1 lightbulb being broken
The chance of exactly 2 lightbulbs being broken
The chance of exactly 3 lightbulbs being broken

but in a form like this:

P(x=0)= ...
P(x=1)= ...
P(x=2)= ...
P(x=3) = ...

The probability of getting exactly 2 broken lightbulbs is:

3C2 * 0.25^2 * 0.75 = 0.140625

The "C" represents the C button on your calculator. You don't need to worry too much what this is since i'm fairly sure i've got that part sorted.

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

/*

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)

*/

#include <iostream>
#include <cmath>

using namespace std;

int fact(int n);
int C(int n, int r);

int main(){ 

			int n;
			cout << "Please enter the sample size: ";
			cin >> n;

			double p;
			cout << "Enter the probabilty of a certain behaviour occurring: ";
			cin >> p;

			double k;

					for(int i = 0; i <= n; i++){ 
	
					k = C(n,i)*pow(p,i)*pow(1-p,n-i);
					
					cout << "P(x = " << i << ")=" << k << '\n'; 

			} 

	return 0;

}

int fact(int n){

	int answer;

	if (n==1) return(1);
	answer = fact(n-1)*n;
	return (answer);

}

int C(int n, int r){

		int a;

		a = fact(n) / (fact(r)*fact(n - r) );

		return a;

}


I think it's right, but I get this when I run it:

name@computer ~/Documents $ g++ binomial.cpp -o binomial
name@computer ~/Documents $ ./binomial
Please enter the sample size: 5
Enter the probabilty of a certain behaviour occurring: 0.12
Segmentation fault


What is this segmentation fault error? I've never had this pop up before, and I can't see what i've done wrong.

I'd appreciate any help.

EDIT: I manage to get an output if I get rid of the C(n,i) in line 39. Why is this?

EDIT 2:

I managed to get the output that I wanted. The problem is calling a recursive function inside a for loop.

Here is the code that works:

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
/*

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)

)

*/

#include <iostream>
#include <cmath>

using namespace std;

int fact(int n);
int C(int n, int r);

int main(){ 

			int n;
			cout << "Please enter the sample size: ";
			cin >> n;

			double p;
			cout << "Enter the probabilty of a certain behaviour occurring: ";
			cin >> p;

			double k;
			int i;

					for(i = 0; i <= n; i++){
	
					k = C(n,i)*pow(p,i)*pow(1-p,n-i);
					
					cout << "P(x = " << i << ")=" << k << '\n'; 

			} 

	return 0;

}

int fact(int n){

	int t, answer;
	
	answer = 1;
	for(t=1;t<=n;t++) answer = answer * t;
	
	return(answer);
}

int C(int n, int r){

		int a;

		a = fact(n) / (fact(r)*fact(n - r) );

		return a;

}


For example,

Please enter the sample size: 10
Enter the probabilty of a certain behaviour occurring: 0.1
P(x = 0)=0.348678
P(x = 1)=0.38742
P(x = 2)=0.19371
P(x = 3)=0.0573956
P(x = 4)=0.0111603
P(x = 5)=0.00148803
P(x = 6)=0.000137781
P(x = 7)=8.748e-06
P(x = 8)=3.645e-07
P(x = 9)=9e-09
P(x = 10)=1e-10


(i've also corrected the word "probabilty" since posting this).

So, my question now is; why can't you put a recursive function inside a for loop?

Since this weakness exists for recursive functions, why use them at all?
Last edited on
segmentation fault means that you have touched some memory you should not.
In your case it is stack overflow.
Part of call stack at the moment of crash: http://puu.sh/jtZwp/273a72aebb.png
As you can see there is an attempt to calculate factorial of negative number which leads to calling more and more functions until you exaust all stack space.

Problem arises when r >= n in function C (you will try to call fact(n-r) there). And in loop for(int i = 0; i <= n; i++){ you will call C with r==n at the last iteraton (C(n,i), at last iteration i==n).

Solution: remember that 0! == 1 and add this to the fact function.
Last edited on
Thanks!

I'll have to remember that a segmentation fault is a memory problem and, in this case, it was a stack overflow.

I can't believe I didn't notice that I hadn't defined 0!. Rookie mistake.
Topic archived. No new replies allowed.