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.
#include <iostream>
#include <string>
#include <cassert>
usingnamespace 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;
}
#include <iostream>
#include <cstdlib>
#include <vector>
usingnamespace 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;
}