Home > OS >  How to iterate through two arrays and find the GCD of the corresponding elements and store these in
How to iterate through two arrays and find the GCD of the corresponding elements and store these in

Time:03-23

So I am tasked with doing the following:

I am given two arrays with a sentinel value. I need to create a function that will iterate through these, finds the GCD of the corresponding elements (ex: array1[0] and array2[0]), and store the result into a result array until it hits the sentinel value.

I already have the part done where it calculates the GCD but I am a little stuck on the iteration part. Here's what I have so far:

#include <stdio.h>

int GCD(int array1[], int array2[]);

int main(void) {
 int array1[] = {245,  92, 16,  1,   3, 22, 255}; 
int array2[] = {57,  226, 240, 15,  22,  3, 255};

int result = GCD(array1, array2);

printf("%d\n", result);
    
return 0;
}

int GCD(int array1[], int array2[]) {

int tempvar1;
int tempvar2;
int result;
int remainder;
int i;
// this part is where I need help iterating
tempvar1 = array1[0];
tempvar2 = array2[0];
remainder = tempvar1;

// this part calculates the GCD
        while (remainder > 0) {
    
                remainder = tempvar1;
            
                while (remainder >= tempvar2) {
                        remainder = remainder - tempvar2;
                    }
            
                tempvar1 = tempvar2;
                tempvar2 = remainder;
            }
    // end GCD calculation 

result = tempvar1;

return result;
}

So far this just calculates the GCD of the numbers in the 0th element but I need it to calculate the GCD for every pair of elements and store them in a result array.

I have tried the 0th element and it works.

CodePudding user response:

Recursive gcd() is faster.

#include <stdio.h>

int gcd (int p, int q) { // Recursive
    if (q) return gcd (q, p % q);
    return p;
}

void GCD_array (int a1[], int a2[], int count, int ans[]) {
    for (int ni = 0; ni < count;   ni)
        ans[ni] = gcd (a1[ni], a2[ni]);
}

int main (void) {
    int array1[] = {245,  92, 16,  1,   3, 22, 255};
    int array2[] = {57,  226, 240, 15,  22,  3, 255};
    int ans [24];
    int count =  sizeof (array1) / sizeof (int);
    GCD_array (array1, array2, count, ans);
    for (int ai = 0; ai < count;   ai)
        printf ("gcd (%d, %d) = %d\n", array1[ai], array2[ai], ans[ai]);

    return 0;
}

CodePudding user response:

Modify GCD as shown to accept two integers

int GCD(int var1, int var2) {
int tempvar1;
int tempvar2;
int result;
int remainder;
int i;

tempvar1 = var1;
tempvar2 = var2;

In the main, add code to loop through the arrays

 int i = 0;
 int array_len = sizeof(array1)/4;
 
 int array_result[array_len];
 
 for (i = 0; i < array_len;   i )
 {
    array_result[i] = GCD(array1[i], array2[i]);
    printf("%d\n", array_result[i]);    
 }

CodePudding user response:

There are few things could be mentioned & improved from your implementation/code:

  1. using minus to get the effect of division/modulo.
       while (remainder >= tempvar2) {
           remainder = remainder - tempvar2;
       }
  1. your iterations are to check single element only, outer loop stopped when remainder become negative value.
tempvar1 = array1[0];
tempvar2 = array2[0];

/*
if your code are logically correct and returning the correct value, 
you could put more loop, 
to replace the tempvar1 and tempvar2 for checking next pair of single element.
*/
  1. your function written to return single int value & what would return in function is also a single int value.
int GCD(int array1[], int array2[]) {

//...ur implementation

result = tempvar1;

return result;
}

FYI, language C has no way to return an array, but to return address of an array. This is applicable when result changed to array-type variable, feel free to explore more at here: In C,how do I make this program return the address of an array,instead of address of first element?

  • Related