Binary Euclid's Algorithm

Hi folks.

My class is working on the whole fraction thing, which of course involves reducing. I found something interesting via a search of the boards here, the binary Euclid's algorithm.

Anyway I tried crunching the rules into code, and came up with this:

Code:

`#include <iostream>`

#include <cmath>

using namespace std;

int main (int n, int m)

{

cout<<"enter n";

cin>>n;

cout<<"enter m";

cin>>m;

if (n>=m)

{

if (n%2==0 && m%2==0)

{

n /= 2;

m /= 2;

}

else if ((n%2==0 && m%2!=0)||(n%2!=0 && m%2==0))

{

n /= 2;

m = m;

}

else if ((n-m)%2==0)

{

m-=n;

}

cout<<"gcd:"<<m;

}

else if (m>n)

{

if (m%2==0 && n%2==0)

{

m /= 2;

n /= 2;

}

else if( (m%2==0 && n%2!=0)||(m%2!=0 && n%2==0))

{

m /= 2;

n = n;

}

else if ((m-n)%2==0)

{

n-=m;

}

cout<<"gcd:"<<n;

}

system ("pause");

return 0;

}

I'm getting some weird results, but for the life of me I can't figure out where things are going wrong.

So, I have two questions:

1. Any hints as to where my logic is flawed?

2. I am currently using the new bloodshed compiler. One thing that would help me greatly is if I could "track" a variable through the program to check where it went wrong. How do I do that?

Thanks