Floating point exception ?

Nov 12, 2012 at 1:01am
This program is supposed to use 3 functions to calculate the factorial of 2 inputs and then find the # of combinations between the two factorials. It compiles correctly but when I run it and put in my inputs the output is "Floating Exception".

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
// --------------------------------------------------------------------
// File name:   simpleMath.cpp
// Assign ID:   LAB10
// Due Date:    11/07/12 at 7:30 pm 
//
// Purpose:     Performs a simple addiion between 2 items.
//
// Author:      bfields Byron Fields
// --------------------------------------------------------------------


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;


int input_check(int n, int r)
{
if( n > r)
 return 1;
else 
   return 0;
}

unsigned long int fact(int n, int r,int& a, int& b, int& c)
{

int factn = 1;

do 
{
for (;n>=1;n--)
{

factn *= n;
a = factn;

}
}while(n != 0);


int factr = 1;

do 
{
for (;r>=1;r--)
{

factr *= r;
b = factr;

}
}while(r != 0);



c = n-r;


}
 

unsigned long int comb(int a, int b, int c, int& combb)
{
combb = a/(b*c);

return combb;
}

int main()
{
int n,r,combb;
int a;
int b;
int c;

cout << "(c) 2012, bfields Byron Fields" << endl;

cout << "Enter the n value" << endl;
cin >> n;

cout << "Enter the r value" << endl;
cin >> r;


if(input_check(n,r) == 1)
  fact(n,r,a,b,c);
else
  cout << "Wrong Input" << endl;

comb(a,b,c,combb);

cout << a << b << c << combb << endl;



cout << "(c) 2012, bfields Byron Fields" << endl;


    
return 0;
}
Nov 12, 2012 at 3:30am
Line 66 will fail with a division by zero fault (c will always be 0), which stems from line 58.

Both n and r are 0 at line 58, because you decremented them in the for loops on line 33 and 47.

You can fix this by moving line 58 above the two for loops.
Nov 12, 2012 at 3:56am
FINALLY... after 4 days i finally got this done. thanks man
Topic archived. No new replies allowed.