# need some help thanks in advance

Printable View

• 05-06-2006
Mshock
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; }```
• 05-06-2006
Dave_Sinkula
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; }```
• 05-06-2006
Mshock
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.
• 05-06-2006
Dave_Sinkula
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.
• 05-06-2006
Mshock
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
• 05-06-2006
Mshock
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.
• 05-06-2006
dwks
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.)
• 05-07-2006
Mshock
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.