I don't know if it can get any more efficient without assuming the input is already sanitised.
Need help
check once in main and then program the GDC function
Just realised this doesn't work because it doesn't check if the result is a divisor of the original first number.
Fixed and reimplemented with wrapper function. I don't know if this will count for OP's homework though lmao.
int mcd_iter(int guess, int a, int b, int c)
{
if (a % guess == 0 && b % guess == 0 && c % guess == 0)
return guess;
else
return mcd_iter(guess - 1, a, b, c);
}
int mcd(int a, int b, int c)
{
if (a < 1 || b < 1 || c < 1)
return -1;
int guess = smallest(a, b, c,)
return mcd_iter(guess, a, b, c);
}
>without assuming the input is already sanitised
What happens when you feed it prime numbers
Arbitrary input array version inspired by int gcd_iter(int guess, int v[])
{
for (int i = 0; v[i] > 0; ++i)
if (v[i] % guess != 0)
return gcd_iter(guess - 1, v);
return guess;
}
/* Get GCD of array of integers, terminated with nonpositive value. */
int gcd(int v[])
{
if (v[0]
for loop instead of if ?
print it on a piece of paper and draw lines between each iterations call and return. seriously. do this wish a few values until you understand. its just a function that properly handles calling itself, not magic
There's an if statement inside the for loop. I need to use a loop instead of explicitly checking each number because I don't know how many numbers I need to check. The array can be arbitrarily long.
don't you have ti specify how big the array will be as tou delcare it tho?