Need help

>need help
>GDC of 3 numbers using recursive function in C
>go !

Attached: just da main.jpg (218x194, 4K)

Other urls found in this thread:

en.wikipedia.org/wiki/Euclidean_algorithm
twitter.com/NSFWRedditGif

en.wikipedia.org/wiki/Euclidean_algorithm
just read the article and you can do it, man. I believe in you.

int main(int argc, char** argc){
int max = max(a, b, c);

return gdc(max, a, b, c);
}

int max(int n, int a, int b, int c){
if(a % n == 0 && b % n == 0 && c % n == 0)
return n;
else
return max(n - 1, a, b, c)
}

the recursive part really fucks up my brain tho :|

rate mine
int gcd(int a, int b, int c)
{
if (a < 1 || b < 1 || c < 1)
return -1;
else if (a > b)
return gcd(b, a, c);
else if (a > c)
return gcd(c, b, a);
else if (b % a == 0 && c % a == 0)
return a;
else
return gcd(a - 1, b, c)
}

int gcd (int *arr){

if ((arr[0] % arr[3] == 0) && (arr[1] % arr[3] == 0) && (arr[2] % arr[3] == 0)){
return arr[3];
}else{
arr[3] = arr[3] - 1;
return gcd(arr);
}
}

main(){

int temp, i, j, result, first;
int arr[4];

//getting inputs:
printf("enter 3 numbers \n\n");
for (i = 0; i < 3; i++){
printf("enter number %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("\n\n");

//sorting array for biggest number:
for (i = 0; i < 3; i++){
for (j = 0; j < 2-i; j++) {
if (arr[j] < arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1] ;
arr[j+1] = temp;
}
}
}

arr[3] = arr[0];
printf("GCD is: %d", gcd(arr));

}

can't wrap my head around it
beginner here so that's expected i guess

also how can you make that kind of a post on Jow Forums ?
i mean uploading text that way....

It's not very complicated.
>if any number is non-positive, return error
>if the first number is not the smallest, return self but change order of arguments
>if first number is a divisor of the other two, return first number
>else, decrement first number and try again
You can read the first bit of SICP to get comfortable with recursion.

>uploading text that way....
Read the rules, nigger.

this is not efficient at all
each time you recurse the first 2 if statements will always be called

I don't know if it can get any more efficient without assuming the input is already sanitised.

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?

>I need to use a loop
Just for fun, here's a version with a recursive function instead of a loop. (still a glorified loop in this case but w/e)
I should be sleeping aaaaaaa

int is_gcd(int guess, int *v)
{
if (*v

That's not the point. Arrays are declared outside the function, and the function can take any length array.