Having trouble with vector of integers in which I need to make sure that the gcd between any two numbers is 1

The vector's size is determined by the user so the solution needs to be generalized to a n sized vector. I have the solution to find the GCD between the whole set but It does test for example if the GCD between the 1st element and the third element is 1 or if the GCD between the 1st element and the 4th is 1.
PLEASE HELP!
If you have any questions, please ask.

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
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <cassert>


using namespace std;
int greatestCommonDivisor(int, int);

int main()
{
	vector<int> jars = { 24,5 ,11,13 };

        int gcd = jars[0];
	for (int i = 1; i < jars.size(); i++)
	{
		 gcd = greatestCommonDivisor(gcd, jars[i]);


	}

	if (gcd != 1)
	{
		cerr << "ERROR : The gcd of the capacity between any two Jars must equal 1.\n\n\n";
		assert(!(gcd != 1));
	}
	system("pause");
	return 0;
}

int greatestCommonDivisor(int a, int b)
{
	int t;
	while (a)
	{
		t = a;
		a = b%a;
		b = t;
	}
	return b;
}
Last edited on
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
34
35
36
37
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;

int greatestCommonDivisor(int, int);

int main()
{
	vector<int> jars = { 24, 5, 11, 13 };
        int gcd;
        
        for ( int i = 0; i < jars.size(); i++ )
	{
           for ( int j = i + 1; j < jars.size(); j++ )
           {
	      gcd = greatestCommonDivisor(jars[i], jars[j]);
              if ( gcd > 1 ) { cout << "ERROR : The gcd of the capacity between any two Jars must equal 1."; return 1; }           
           }
	}

	cout << "All is wonderful" << endl;
	system("pause");
	return 0;
}

int greatestCommonDivisor(int a, int b)
{
	int t;
	while (a)
	{
		t = a;
		a = b%a;
		b = t;
	}
	return b;
}
Last edited on
Topic archived. No new replies allowed.