Thread: recursion...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35

    recursion...

    really makes my brain sore!

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35

    what do you think?

    can't seem to grasp this. maybe I could use a pointer for the count variable?

    Code:
    //8:Redo exercise 7, but this time use a recursive function.
    
    #include<stdio.h>
    
    double power(double n, int p); 
    
    int main(void) {
    
    	double x, xpow;
    	int exp;
    
    
    
    	printf("Enter a number and the positive integer power");
    	printf(" to which\nthe number will be raised. Enter q");
    	printf(" to quit.\n");
    
    	while (scanf("&#37;lf%d", &x, &exp) == 2) {
    
    		xpow = power(x,exp);
    
    		printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
    		printf("Enter next pair of numbers or q to quit.\n");
    
    	}
    	printf("Hope you enjoyed this power trip -- bye!\n");
    	return 0;
    }
    
    
    
    double power(double num, int pow) {
    
    	int count;
    	float ans = 1;
    	
    	
    	if (pow == 0)
    		ans = 1;
    
    	else if (num  == 0)
    		ans = 0;
    
    	else if (pow < 0) {
    		<STRIKE>//write a for loop  that is for negative powers</STRIKE>	*EDIT*
                           //use recursion!
    		
    		if (count >= pow) {
    			ans *= num;
    			power(count - 1, pow);
    		}
    		ans = 1 / ans;
    	}
    
    	else {
    		for (count = 1; count <= pow; count++)
    			ans *= num;
    	}
    	
    		
    
    	return ans;
    
    }
    Last edited by kbat82; 12-09-2008 at 09:30 PM. Reason: had an old comment in there

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35

    solved

    yatta! it only took me FOREVER, but I figured it out all on my own. isn't it a proud feeling? Using a function made it so much easier. Any advice?

    Code:
    //8:Redo exercise 7, but this time use a recursive function.
    
    #include<stdio.h>
    
    double power(double, int); 
    double neg_power(double, int, double);
    
    int main(void) {
    
    	double x, xpow;
    	int exp;
    
    
    
    	printf("Enter a number and the positive integer power");
    	printf(" to which\nthe number will be raised. Enter q");
    	printf(" to quit.\n");
    
    	while (scanf("%lf %d", &x, &exp) == 2) {
    
    		xpow = power(x,exp);
    
    		printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
    		printf("Enter next pair of numbers or q to quit.\n");
    
    	}
    	printf("Hope you enjoyed this power trip -- bye!\n");
    	return 0;
    }
    
    
    
    double power(double num, int pow) {
    
    	int count;
    	float ans = 1;
    	
    	
    	if (pow == 0)
    		//a number with power of 0 = 1
    		ans = 1;
    	else if (num  == 0)
    		//number with power 0 = 0
    		ans = 0;
    	else if (pow < 0) 
    		//negative powers
    		ans = neg_power(num, pow, ans);
    	else { 
    		//positive powers 
    		for (count = 1; count <= pow; count++)
    			ans *= num;
    	}
    	return ans;
    }
    
    double neg_power(double number, int neg_pow, double answer) {
    	
    	if (neg_pow < 0)
    		//use recursion to solve for negative powers.
    		answer = 1/number * neg_power(number, neg_pow + 1, answer);
    	
    	return answer;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM

Tags for this Thread