Hello,

I am trying to compute the GCD non-recursively. I have no errors but when I run the program it aborts because it says c is not intlized.

#include "stdafx.h"
#include "stdio.h"

  int main (void)
{
    int a,b,c,r,result;
    printf("enter a value for a\n");
	scanf ("%d", &a);
	printf("enter a value for b\n");
	scanf ("%d", &b);
	printf("The result is %d\n", c);
{
	int c;
if (a < b)      
      c = a;   
      a = b;
      b = c;
	  return 0;
}
	
while(1)
    {
  	c = a%b;
  	if(c==0)
  	  return b;
  	a = b;
  	b = c;
    }
  }

Please help me find this issue!

The last call to printf(), it is being made before variable c has anything meaningful assigned to it.

That while() loop doesn't have a way of stopping. Of course, it would not even get there if a is greater than b.

[Edit:] Why do you think you need to #include "stdafx.h"?; you are not using variable result for anything.

1. If you are using stdafx.h (in MS VC++), place all system includes in stdafx.h, not in your source modules.
2. Why #include "stdio.h" ? Must be #include <stdio.h> directive in stdafx.h.

Now, the program when it runs it says result is not initlized because of this it aborts.

#include "stdafx.h"
#include "stdio.h"

int main (void)
{
    int a,b,c,r,result;
    printf("enter a value for a\n");
	scanf ("%d", &a);
	printf("enter a value for b\n");
	scanf ("%d", &b);
	printf("The result is %d\n",result);
{
	
if (a < b)      
      c = a;   
      a = b;
      b = c;
	  return 0;
}
	
while(1)
int c;
    {
  	c = a%b;
  	if(c==0)
  	  return b;
  	a = b;
  	b = c;
    }
  }

Well you are printing "result" without initializing it or assigning it a value. Is result supposed to be your GCD ? Then you probably want to move your print to after you have computed your GCD and assigned it to the "result" variable.

Did you intend to have your "if" and "while" loop be separate functions or a part of the main function ? Since I see you are swapping "a" and "b" a < b and then trying to compute the gcd. With your code you'll never get to computing the GCD if that is the case !!

now, (19) : error C2061: syntax error : identifier 'a'

int main (void)
{
    int a,b,c,r,result;
    printf("enter a value for a\n");
	scanf ("%d", &a);
	printf("enter a value for b\n");
	scanf ("%d", &b);
	printf("The result is %d\n", c);
{
	int result,a;
	if a = 0 return b
     while b ≠ 0
         if a > b
             a := a − b
         else
             b := b − a
	printf("The result is %d\n", c);
     return result;
}
}

I also need to figure out how to include r into the algo and int r

This

{
	int result,a;
	if a = 0 return b
     while b ≠ 0
         if a > b
             a := a − b
         else
             b := b − a
	printf("The result is %d\n", c);
     return result;
}

is not C or C++ code. Where are you getting this from ? If its pseudo code you need to rewrite it in C.

I still have one error (34) : error C2065: 'result' : undeclared identifier

#include "stdafx.h"
#include "stdio.h"

int main (void)
{
    int a,b,c,result;
    printf("enter a value for a\n");
	scanf ("%d", &a);
	printf("enter a value for b\n");
	scanf ("%d", &b);
	printf("The result is %d\n", c);
}
	 int gcd(int a,int b)
  {
    int c;
    if(a<b)
    {
        c = a;
        a = b;
        b = c;
    }
    while(1)
    {
  	c = a%b;
  	if(c==0)
  	  return b;
  	a = b;
  	b = c;
	result = gcd(a, b);
    }
  }
This question has already been answered. Start a new discussion instead.