-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuclideanAlgorithmCalculator.cpp
More file actions
82 lines (65 loc) · 2.38 KB
/
Copy pathEuclideanAlgorithmCalculator.cpp
File metadata and controls
82 lines (65 loc) · 2.38 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cstdio>
using namespace std;
int checkInputInt(int& getNum);
int findGcd(int& bigNum, int& smallNum);
unsigned int smallNumberVectorSize, bigNumberVectorSize;
int bigNumber, smallNumber, nextSmall, modGet, gcd;
vector<int> smallNumberVector;
vector<int> bigNumberVector;
//TODO thought process, create a loop that will go until nextSmall is zero 0.
//to fill up the vectors and then sequentially display the variables inside the vectors.
//check for correct input and correct big number small number input.
//check for 0 input to avoid runtime errors.
int main(){
cout << "This is a program that tries to calculate the Greatest Common Divisor\n";
cout << "(GCD) for two numbers. It uses the Eucledian Algorithm.\n";
//ask for input
cout << "Big Number: \n";
cin >> bigNumber;
bigNumber = checkInputInt(bigNumber);
cout << "Small Number: \n";
cin >> smallNumber;
smallNumber = checkInputInt(smallNumber);
//add to vector
smallNumberVector.push_back(smallNumber);
bigNumberVector.push_back(bigNumber);
//execution
gcd = findGcd(bigNumber, smallNumber);
//result
cout << "GCD: " << gcd << endl;
getchar();
return 0;
}
int checkInputInt(int& getNum){
int holdNum = getNum;
if (holdNum < 0)
holdNum = holdNum * -1;
return holdNum;
}
int findGcd(int& bigNum, int& smallNum){
//calculate test
bigNumber = bigNum;
smallNum = smallNumber;
do{
if (smallNumber == 0)
break;
modGet = bigNumber / smallNumber;
nextSmall = bigNumber - (smallNumber * modGet);
bigNumber = smallNumber;
smallNumber = nextSmall;
smallNumberVector.push_back(smallNumber);
bigNumberVector.push_back(bigNumber);
}while(smallNumber >= 0);
//a loop to display the output of the vectors
for(bigNumberVectorSize = 0; bigNumberVectorSize < bigNumberVector.size(); bigNumberVectorSize++){
cout << "Big Number: " << bigNumberVector[bigNumberVectorSize] << endl;
}
for(smallNumberVectorSize = 0; smallNumberVectorSize < smallNumberVector.size(); smallNumberVectorSize++){
cout << "Small Number: " << smallNumberVector[smallNumberVectorSize] << endl;
}
bigNumberVectorSize = bigNumberVector.size() - 1;
return bigNumberVector[bigNumberVectorSize];
}