c++ iterative combination function not working -
i have homework problem asking write iterative , recursive combination function. place them in given program see takes longer.
i having problem iterative function. have gone on several times , keep getting mach-o-linker error. have tried identifying variables many different ways , still haven't come luck.
any on topic appreciated. think there problem iterator function or possibly factorial function, unable see life of me right now.
thanks again ahead of time
#include <iostream> #include <sys/time.h> #include <cstdlib> using std::cout; using std::endl; double ir; double in; typedef unsigned int uint; uint factorial(uint n) { if (n == 0) return 1; if (n <= 2) return n; else return n * factorial(n - 1); } double combination_recursive(double in, double ir); double combination_iterative(int in, int ir); int main(int argc, const char * argv[]) { typedef struct timeval time; time stop, start; gettimeofday(&start, null); in = 20.0; ir = 3.0; combination_iterative(in, ir); gettimeofday(&stop, null); 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; } double comination_iterative(int, int) { if (in == ir) { return 1;} if (ir == 0 && in!= 0) { return 1;} else return (in * factorial(in-1))/factorial(in-1)*factorial(in-ir); } double combination_recursive(double in, double ir) { if (ir < 0 || ir > in) { return 0; } if (ir < 1) { return 1; } if (in == ir) { return 1; } return combination_recursive(in - 1, ir) + combination_recursive(in - 1, ir - 1); }
i think error due simple misspelling.
you make call function in main
combination_iterative(in, ir);
but have defined as
double comination_iterative(int, int) { if (in == ir) { return 1;} if (ir == 0 && in!= 0) { return 1;} else return (in * factorial(in-1))/factorial(in-1)*factorial(in-ir); }
to fix change function definition match call
double combination_iterative(int, int) { if (in == ir) { return 1;} if (ir == 0 && in!= 0) { return 1;} else return (in * factorial(in-1))/factorial(in-1)*factorial(in-ir); }
happy coding , learning c++
Comments
Post a Comment