Trouble with (propaply) an array.

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;
    }
}

[edit] Wrong title :P...
Last edited on
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.

-psault
Yeah, just put a for loop on the outside of the if/else and you should be fine.
And how the for loop should be?
Topic archived. No new replies allowed.