Euclid's Output

closed account (E098vCM9)
Hey all,

This is my first post to this site, although I use it all the time - great job!

I'm writing a euclid's program and I finally got it to work, but the out put seems a little off. i'm getting the right answer though.

I know there's a simpler way to write the program, but this is a lead up to incorporate it into a stack - so i need it in this format.

The last line should actually be 10 5 5 and not 15 5 10

Thank you!!


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

#include <iostream>
#include <iomanip>
using namespace std;


// Function Prototypes
void display();
void output(int, int, int);


// Main Program
	void main()
	{
		int a = 65, b = 15, c = 0, gcd = 0;
				
		display();
				
		output(a, b, c);
				
		c = (a - b);
		
		while((c > b) && (c > 0))
		{
			a = c;
			c = a - b;
			output(a, b, c);
		}
		while((c < b) && (c > 0))
		{
			a = b;
			b = c;
			c = a - b;
			output(a, b, c);
		}
		gcd = b;
		cout << "\nGCD = " << gcd << "\n\n\n";
		
		
	}


// Functions

void display()
{
	cout << "*************************\n";
	cout << "*  EUCLID'S ALOGORITHM  *\n";
	cout << "*************************\n\n";
	cout << "    A      B      C \n\n";
}


void output(int a, int b, int c)
{
	cout << setw(5) << a << "  " << setw(5) << b << "  " << setw(5) << c << "\n";
}
closed account (E098vCM9)
Just realized that i probably should have put this into the beginners forum, sowwy!!
closed account (E098vCM9)
really? nobody willing to help??
closed account (Dy7SLyTq)
well your not being very clear with whats wrong. what output? you have about what ends up being (im just hazarding a rough guess im too tired to work it out in my head) 20 diffrent outputsl. do you mean over all. a specific iteration of the loop, or one of the loops entirely
That looks to be a lot more complicated than it needs to be, besides not working.

See:
http://en.wikipedia.org/wiki/Euclidean_algorithm
Skip down to "Implementations"

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
using namespace std;


// Function Prototypes
void display();
void output(int, int, int);


// Main Program
int main()
{
	int a = 65, b = 15, c = 0, gcd = 0;
				
	display();
				
	output(a, b, a-b);				

    while ( a && b )
    {
        if ( a > b )
            a = a - b ;
        else
            b = b - a ;

        output(a, b, abs(a-b)) ;

        // or, if you want the highest displayed on the left, always:
        // output(std::max(a,b), std::min(a,b), abs(a-b)) ;
    }

	gcd = a ? a : b ;
	cout << "\nGCD = " << gcd << "\n\n\n";		
}


// Functions

void display()
{
	cout << "*************************\n";
	cout << "*  EUCLID'S ALOGORITHM  *\n";
	cout << "*************************\n\n";
	cout << "    A      B      C \n\n";
}


void output(int a, int b, int c)
{
	cout << setw(5) << a << "  " << setw(5) << b << "  " << setw(5) << c << "\n";
}
Last edited on
closed account (E098vCM9)
thanks, sorry i've spent (wasted) countless hours on this. i know it seems a lot more complicated than it needs to be, but i need to transfer it into a stack format - which this will make easier. many thanks!
Topic archived. No new replies allowed.