Using both a Recursive and Iterative Functions

This is a discussion on Using both a Recursive and Iterative Functions within the C Programming forums, part of the General Programming Boards category; This code needs to have both a recursive and iterative function, with the iterative function having the loop. How does ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    48

    Using both a Recursive and Iterative Functions

    This code needs to have both a recursive and iterative function, with the iterative function having the loop. How does this need to be done?

    Code:
    //Program Purpose: computes the greatest common divisor (gcd) of two numbers given that 
    //				   gcd is the product of the integersí common factors.
    
    #include <stdio.h>
    #include <math.h>
    int main (void)
    {
    	/*Define Variables*/
    	int x;
    	int y;
        int remainder;
    	int gcm;
    
    	
    	/*Get user Input*/
    	printf("Input first number-> ");
    	scanf("%d", &x);
    	printf("Input second number-> ");
    	scanf("%d", &y);
    
    	/* Compute */
    	x=abs(x);
    	y=abs(y);
    	
    
    	do{
    		remainder=x%y;
    		x=y;
    		y=remainder;
    	}
    	while(remainder!=0);
    	
    	gcm=x;
    	printf("gcm = %d \n", gcm);
    	
    	return 0;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    What is it that you fail to understand?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    well the specifications of the way my professor wants this program to be written is with two functions, one a recursive and the other an iterative function with a loop. From what I heard from her, both have to be used for me to get any credit and im not sure how to do this.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Basically, you have an iterative function already, so that part is easy.
    Do you know how a recursive function works?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you seem to have the loop/iterative part done, except for the "putting it in a function" part. Now rewrite the calculation using recursion -- instead of loop back to the top, it's call the function with smaller numbers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    so like this?
    Code:
    #include <stdio.h>
    #include <math.h>
    
    void iterative(int x, int y);
    int recursive (int x, int y);
    
    int main (void)
    {
    	/*Define Variables*/
    	int x;
    	int y;
    	
    	/*Get user Input*/
    	printf("Input first number-> ");
    	scanf("&#37;d", &x);
    	printf("Input second number-> ");
    	scanf("%d", &y);
    
    	/* Compute */
    	x=abs(x);
    	y=abs(y);
    	
    	iterative(x,y);
    	
    	/*End Program*/
    	return 0;
    }
    /*-----------------------------------------------------------*/
    void iterative(int x, int y)
    {
    int remainder;
    int gcm;
    
    	do{
    		remainder=recursive(x, y);
    		x=y;
    		y=remainder;
    	}
    	while(remainder!=0);
    	
    	gcm=x;
    	printf("gcm = %d \n", gcm);
    	return(0);
    	}
    
    /*----------------------------------------------------------*/
    int recursive(int x, int y)
    {
    	return(x%y);
    }

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    i mean it still gives me the right output, but i dont think im calling the recursive again am I?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    No, not quite... You may have named it recursive, but it is still only iterative.
    I will show an example of what iterative and recursive is:
    Code:
    // Iterative version: counts up to 1000
    void iterative()
    {
        int count = 0;
        for (int i = 0; i < 1000; i++)
            count++;
    }
    
    // Recursive: counts up to 1000. Initially called with recursive(0).
    int recursive(int x)
    {
        if (x == 1000) return 1000;
        else recursive(x + 1);
    }
    Now it should be up to you to turn your example into a recursive and iterative version.
    Iterative basically means achieving a result through loops, and recursion basically means achieving a result through repeatedly calls in a function of itself (ie recursive calls recursive until the result is achieved).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    so what needs to be in the recursive part, do you think?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Obviously, the fact that the function must call itself to achieve its result.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    I think i figured it out or at least i am getting the right results with my test, but does this make sense to you guys?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void iterative(int x, int y);
    int recursive (int x, int y, int remainder);
    
    int main (void)
    {
    	/*Define Variables*/
    	int x;
    	int y;
    	int remainder=1;
    	
    	/*Get user Input*/
    	printf("Input first number-> ");
    	scanf("%d", &x);
    	printf("Input second number-> ");
    	scanf("%d", &y);
    
    	/* Compute */
    	x=abs(x);
    	y=abs(y);
    	
    	printf("Recursive GCM = %d \n", recursive(x, y, remainder));
    	iterative(x,y);
    	/*End Program*/
    	return 0;
    }
    /*-----------------------------------------------------------*/
    void iterative(int x, int y)
    {
    int remainder;
    int gcm;
    
    	do{
    		remainder=x%y;
    		x=y;
    		y=remainder;
    	}
    	while(remainder!=0);
    	
    	gcm=x;
    	printf("Iterative GCM = %d \n", gcm);
    	return(0);
    	}
    
    /*----------------------------------------------------------*/
    int recursive(int x, int y, int remainder)
    {
    	if(remainder==0)
    		return x;
    	else
    	{
    		remainder=x%y;
    		x=y;
    		y=remainder;
    		return recursive(x, y, remainder);
    	}
    }
    /*--------------------------------------------------------*/

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    i can just print both of the functions results to the screen and i think that may work for my professor

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Not bad so far. I expect your final program shold be calling both and outputting the answer for both.
    Your 'recursive' function can be simplified a bit.
    Instead of y=remainder, you could simply pass remainder for the last two params. Then you can do the same simplification again, passing y instead of x.
    You can also do it without the extra 'remainder' parameter. When one of x or y is zero, the other should be the answer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 04:42 AM
  2. splitting linked list recursive vs. iterative
    By Micko in forum C Programming
    Replies: 7
    Last Post: 03-17-2005, 04:51 PM
  3. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Iterative Tree Traversal using a stack
    By BigDaddyDrew in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2003, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21