iterative and recursive functions

I am writing a program that uses the iterative and recursive functions to find solution (and times) for factorial using the equation
c(n,r)= n! /(r! * (n - r)!) while r=3, n=20 and then when r = 10 and n = 1000.

This is my code so far....?

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
 #include <iostream>
 #include <sys/time.h>
 #include <cstdlib>

 using std::cout;
 using std::endl;

 long double recursive_func(long double n, long double r);
 long double iterative_func(long double n, long double r);

 int main()
 {
         typedef struct timeval time;
         time stop, start;

         long double result;

         gettimeofday(&start, NULL);

         iterative_func (20, 3);   //call the function to be timed with these numbers
        recursive_func(20,3);    //call the function to be timed with these numbers
         iterative_func(1000, 10);  //call the function to be timed with these numbers
         recursive_func(1000, 10); //call the function to be timed with these numbers

         gettimeofday(&stop, NULL);
	
         resultrec1 = recursive_func(n,r); //somehow need to be able to print out each of the results 
	
	cout << result <, endl; //somehow need to be able to print out each of the results

         if (stop.tv_sec > start.tv_sec)
            cout << "Seconds: " << stop.tv_sec - start.tv_sec << endl;
         else
            cout << "Micro: " << stop.tv_usec - start.tv_usec << endl;
         return 0;
 }
 long double recursive_func(long double n, long double r)
 {
         if (n == r || r == 0)
            return 1;
         else
           {
            return (recursive_func(n / r)) * (recursive_func(n-1, r-1));
            }
} 
long double iterative_func(long double n, long double r)
{
	int total = 1;
	int i;

	cin >> n;
	cin>>r;

	for (i = 1; i <= n; i++)
 	{
	   total *= i;
        }
}
Any ideas on how to correct my code?

Thanks!
Your code needs correction?

Hard to solve a problem when we don't know what the problem is.

Try telling us what's wrong with the code and maybe you'll get a better response.
Last edited on
Topic archived. No new replies allowed.