Greatest Common Divisor's Array

hello all, i could not find any mistake on the problem that finds the pair of numbers with maximum gcd.

for example: array is {16,24, 50, 4} output should be 8 cause of number of pair 16,24

what am i missing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>

using namespace std;

static int gcd(int a, int b){
       
       return (b== 0) ? a : gcd(b, a%b );
      
 }
       
       static int array(int a[]){
       int value= 0;
       
       for(int i = 0;i < sizeof(a)-1; i++){
        for(int j = 1;j < sizeof(a); j++){
       if(gcd(a[i],a[j]) > value )
                       
         value = gcd(a [i],a[j]);
                }
               }
     return value;     
              }

int main() {
int a[] = {12,24,50,4};
 
  cout<<array(a);
	
	 system("PAUSE");
    return EXIT_SUCCESS;
	
}
The sizeof operator doesn't do what you think it does. (Don't do that!)

Make sure to pass in the length of the array.

 
int array(int a[], int N){
 
cout<<array(a,4);

You are also missing an important condition between lines 15 and 16.

Hope this helps.
ah yes, i was using wrong way to find array length. it's ok for now.

but i did not find an important condition yet.

*thinking, thinking*
Try running it and see what answers it gives you.
Oh now, output is 0 for now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

static int gcd(int a, int b){
       
       return (b== 0) ? a : gcd(b, a%b );
       }       
 static int array(int a[]){
       int value= 0;
       int length = sizeof(a) / sizeof(*a);
       
       for(int i = 0;i < length -1; i++){
               for(int j = 1;j < length; j++){
                              if(gcd(a[i],a[j]) > value ){
                       
                         value = gcd(a[i],a[j]);
                   }
               }
               }
     return value;     
              }

int main() {	
	int a[] = {12,24,50,4};
 
  cout<<array(a);
	
	 system("PAUSE");
    return EXIT_SUCCESS;
	
}
You're still using sizeof wrong. a in your function array is not an array. It is a pointer. The a in main and the a in array are not the same variable and do not have the same type despite sharing the same name.

You cannot determine the length of a in array. You must pass the length in as Duoas has already suggested.

i think, i am not good at this issue that in as Duoas has suggested.

so i have started to solve it by vector.
I am still getting a wrong output. I will be damned. *grr*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
using namespace std;

static int gcd(int a, int b){
       
       return (b== 0) ? a : gcd(b, a%b );
       }     
      
 static int vgcd(vector<int> v){
       int value= 0;
      
       for(int i = 0;i < v.size()-1; i++){
               for(int j = 1;j < v.size(); j++){
                if(gcd(v[i],v[j]) > value ){                       
               value = gcd(v[i],v[j]);
                   }
               }
               }
     return value;     
              }

int main() {	
    
static const int arr[] = {16,24,4,50};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
cout<<vgcd(vec);
	 system("PAUSE");
    return EXIT_SUCCESS;
	
}
I'm not sure if I can help.
You're trying to find the gcd of 4 numbers right?
I'm not sure how you would implment it but this for loop will find the gcd for 4 integers.

1
2
3
4
5
6
7
8
for( int i = num1; i > 0; i--)
{
if (num1 % i == 0 && num2 % i == 0 && num3 == 0 && num4 ==0)
              { 
                gcd = i;
                break;
              }
}

@Momothegreat
OP is not trying to find the GCD of four numbers. Read his problem statement again.

@GSISONYA
I don't understand why you are spending so much time mucking around with sizeof. My advice remains the same: get rid of it. It is distracting you from your actual problem.

If you must, C++ provides a number of ways too query the compiler for an array's size, the simplest/easiest of which is probably:

array(a,end(a)-begin(a))

No matter what you do, though, you must in some way pass the length of your array as argument to the function.

Hope this helps.
Topic archived. No new replies allowed.