I was trying to make a function to find the GCD... What's wrong with it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int divisorlist[100];
int findGCD (int& a, int& b)
{
int x=2;
int i=0;
int divisor;
if (a > x && b > x && i < 100 && ((a % x) + (b % x) == 0))
{
divisorlist[i] = x;
i++;
x++;
}
else
{
x = divisorlist[i];
return x;
}
}
You are going to need to use some kind of loop to keep incrementing x and then checking it against your formula. As you have it now, your if statement only executes one time (probably always evaluating to false) and then you are returning an element of an uninitialized integer array.