#TC1017 #QUIZ6
Soooo I had no idea what a GDC was, well I had forgotten what it was. Lucky for me Euclide’s formula was quite easy to understand anfd implement, so I chose to do it with arrays, just cause.
It was simple and effective, but I did struggle with a compiling error that was due to a bad implementation of some logic. (When either number is 0). I just had to rearrange some lines of code and it ran like a charm.
Source Code below: [GitHub Link: https://github.com/diegodamy/Quiz6]
#include <iostream>
using namespace std;
int GetGDC(int numbers[], int size){
int quotient;
int modulus;
do {
if (numbers[0] == 0){
return 1; //cout << “GDC is ” << numbers [1] << endl;
} else if (numbers[1] == 0){
return 0; //cout << “GDC is ” << numbers [0] << endl;
}
quotient = numbers[0]/numbers[1];
modulus = numbers[0]%numbers[1];
numbers [0] = (numbers [1]*quotient)+modulus;
numbers [0] = numbers [1];
numbers [1] = modulus;
} while ((numbers[0] == 0)||(numbers[1] == 0));
}
int main(){
int array [2];
cout << “In order to find the GCD of two numbers, please input two positive integers:” << endl;
for (int i=0; i<2; i++) {
cin >> array[i];
}
if (GetGDC(array,2)==1) {
cout << “The GDC of given numbers is ” << array[1] << endl;
} else if (GetGDC(array,2)==0) {
cout << “The GDC of given numbers is ” << array[0] << endl;
} else {
cout << “The GDC of the given numbers is ” << GetGDC(array,2) << endl;
}
}
———————————————-
GCD: What were you? by diegodamy is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.