Euclidean Algorithm

Hello C++ forum!
I am working on a math research project and wanted to write a program in C++. My skills in C++ are decent, but not the best. I was writing my own code for the Euclidean Algorithm and when I output what is supposed to be the GCD I end up with some giant weird number thats the same every time. I gotta be missing something simple if no matter what the numbers are it outputs the same number. Any help would be awesome; Thanks!
Matt
[
#include <iostream>

class Euclidean_gcd{

private:
int arg1, arg2, num1, num2, i=0, size, *array=NULL, *array2=NULL;
float total;

public:
void set_high_low(int arg1, int arg2);
int calculate(int & total);
void setArray(int size);
//void DoubleArray();

};


using namespace std;

int main()
{

Euclidean_gcd q1;

int argu1, argu2, size=20, total;

cout<<"inpute argument 1:";
cin>>argu1;
cout<<"input argument 2:";
cin>>argu2;

q1.setArray(size);
q1.set_high_low(argu1, argu2);
total = q1.calculate(total);

cout<<total;

return 0;
}

void Euclidean_gcd::set_high_low(int arg1, int arg2)
{
if (arg1>arg2)
{
arg1 = num1;
arg2 = num2;
}
else
{
arg1 = num2;
arg2 = num1;
}
}

void Euclidean_gcd::setArray(int size)
{

array=new int [size];
array2=new int [size];

}

int Euclidean_gcd::calculate(int & total)
{

while (num2!=0)
{

array[i] = num1 % num2;

num2 = num1;
num2 = array[i];

cout<<total;

i++;

/*if (i==size)
{
//insert double array function here
}*/

}

return total;
}

/*
void Euclidean_gcd::DoubleArray()
{
while (i=0,i<size,i++)
{
array2[i]=array[i];
delete array[];
size=size*2;
array=new int [size];
array[i]=array2[i];
}
}
*/

]
Sorry about the no indents, it was there before but not after I hit submit
To post code, use the code tags please, they are the ones to the right of the text box and look like this : "<>"

Your problem is not uncommon, a lot of people tend to do this

In main:
total = q1.calculate(total);

The arguement you gave to q1.calculate(int) is a bit recursive in a way in that you are trying to obtain a total, by giving the variable 'total' as an argument. If you don't yet have a value for argument 'total', there is no way it will magically insert a new value to total and calculate. And even if it does magically do this, there is no point in calculating it again seeing as you already have a total.

Solution:
My guess is that you are trying to add up the sum of argu1 and argu2, so a better way to call the function will be:
q1.calculate(argu1 + argu2)

Also noticed the same problem in this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int Euclidean_gcd::calculate(int & total)
{
    
    while (num2!=0)
    {
        
        array[i] = num1 % num2;
        
        num2 = num1;
        num2 = array[i];
        
        cout<<total;
        
        i++;
        
        /*if (i==size)
         {
         //insert double array function here 
         }*/
        
    }
    
    return total;
}


No where in the function did you give the value 'total' a numerical value so it will not be doing what you want it to do

E: num2 = array[i]; you are setting num2 to 0
Last edited on
Topic archived. No new replies allowed.