Thread: need some help thanks in advance

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    51

    need some help thanks in advance

    I need to write an iterative function of the program I wrote as recursive.

    I am a total rookie at this c programing game. I got my program working with a recursive function and it does what it is suppose to do.

    Example: 6 and 15 = 3 ; 15 and 22 =1

    I gave it a shot writing the program without the recursive function but it doesnt compute properly.

    Can someone help me out with this.


    Code:
    /* HERE IS MY RECURSIVE PROGRAM IT WORKS PERFECTLY /*
    /* #include <stdio.h>  */
    /* int gcd (int a, int b);  */
    /* int main()   */
    /* {   */
    
    /*   int a=0;  */
    /*   int b=0;  */
    /*  printf(" This program will compute greatest common divisor of two integers. \n");  */
    /*   printf("Please enter two integers: \n\n"); */
    /*   scanf("%d", &a); */
    /*   scanf("%d", &b); */
    /*   printf("\n The greatest common denominator of %d and %d is %d \n", a, b, gcd(a,b)); */
    /*  return 0; */
    /* }  */
        /* Recursive Function */
    		
    /*	int gcd (int a,int b)*/
    /*	{  */
    /*		int r;  */
    /*		if ((r=a%b) == 0) */
    /*			return b; */
    /*		else  */
    /*			return gcd(b, r); */
    /*	} */
    
    /*  HERE IS MY ATTEMPT TO DO THIS ITERATIVE FUNCTION */
    #include <stdio.h>
    int gcd (int a, int b);
    int main()
    {
    
       int a=0;  
       int b=0;
       int r=0;
       printf(" This program will compute greatest common divisor of two integers. \n");
       printf("Please enter two integers: \n\n"); 
       scanf("%d", &a);
       scanf("%d", &b);
       r=(a%b);
       printf("\n The greatest common denominator of %d and %d is %d \n", a, b, r);
      return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Let's take a look at the recursive version (slightly modified).
    Code:
    int gcd_r(int a, int b)
    {
       int r = a % b;
       if ( r )
       {
          return gcd_r(b, r);
       }
       return b;
    }
    As long as a % b is nonzero, it will recursively call itself -- or loop. The (recursive) loop takes b as a and r as b when it repeats. Or...
    Code:
    int gcd_i(int a, int b)
    {
       int r = a % b; /* same */
       while ( r ) /* mostly the same */
       {
          a = b; /* replaces parameter 1 of recursive call */
          b = r; /* replaces parameter 2 of recursive call */
          r = a % b; /* replaces recursive call */
       }
       return b;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    I understand how the recursive function works... I need to have help with the iteritive version of the program... making it non recursive.

    for some reason it is mis calculating.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Those snippets were from actual working code.
    Code:
    int main()
    {
       int a = 6, b = 15;  
       printf("gcd_r(%d, %d) = %d\n", a, b, gcd_r(a,b));
       printf("gcd_i(%d, %d) = %d\n", a, b, gcd_i(a,b));
       a = 15,b = 22;  
       printf("gcd_r(%d, %d) = %d\n", a, b, gcd_r(a,b));
       printf("gcd_i(%d, %d) = %d\n", a, b, gcd_i(a,b));
       return 0;
    }
    
    /* my output
    gcd_r(6, 15) = 3
    gcd_i(6, 15) = 3
    gcd_r(15, 22) = 1
    gcd_i(15, 22) = 1
    */
    Perhaps you could try a closer look at what you are brushing aside. And think about what I wrote in regard to changing a recursive loop into an interative loop.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    not brushing aside any of the help you offer. I am glad you responded.. i have been trying to get the code you posted to work. I think i understand. my problem now is I cant get it to print out the reminder. Trying this latest post of yours now.

    thanks for the help

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Okay i have tried and tried to put your code into my program with no luck getting a printout.

    I need it to printout like this:

    if i enter 15 22

    it shows 15 22 and says the remainder is 1

    Just like my recursive program at the top.

    I am thinking if i am having problems with these easy ones, I am in big trouble laters on. This is just an excercise in the book. Trying to figure it out.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, to convert a recursive function to an iterative one, you need at least a loop of some sort. (Maybe two for loops would work.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    I just wanted to say thanks dave for the help with this program. It was driving me crazy. I worked most of yesterday on it and quit. This morning got up read your posts and for some reason I got it. The program is working perfectly. I even understand how and why it is working.

    Thanks again for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Introduction and Thanks in Advance!
    By Gallaton in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2009, 12:37 AM
  2. Help w/C++ program.. thanks in advance.
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 08:06 AM
  3. Advance Reverse string
    By sturm100 in forum C Programming
    Replies: 6
    Last Post: 10-29-2002, 04:08 PM
  4. Should't the Forums subcategorized into.. Newbie.. Advance.. and Expert Users
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-09-2002, 04:28 PM
  5. GameBoy Advance
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-11-2001, 02:25 PM