having trouble with euclidean algorithm displaying

Dec 15, 2011 at 4:57am
i dont know if this works, i try putting breaks, even a system pause, it may be working, but it wont stay on the screen

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
#include<iostream>
#include<math.h>

using namespace std;

int n;
int d;
int a;
int b;
int gcd;

int main(){
	cout<<"Please enter numerator:"<<endl;
	cin>>a;
	cout<<endl;
	cout<<"Please enter denominator:"<<endl;
	cin>>b;
	cout<<endl;



while (b != 0) {
    int gcd = a % b;
    a = b;
    b = gcd;
}
return a;

cout<<"the reduced fraction is:"<<endl
	<<a/gcd<<"/"<<b/gcd;

system("pause");
return 0;
}
Dec 15, 2011 at 5:07am
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
#include<iostream>
#include<math.h>

using namespace std;

int n;
int d;
int a;
int b;
int gcd;

int main(){
	cout<<"Please enter numerator:"<<endl;
	cin>>a;
	cout<<endl;
	cout<<"Please enter denominator:"<<endl;
	cin>>b;
	cout<<endl;



while (b != 0) {
    int gcd = a % b;
    a = b;
    b = gcd;
}
return a;// <----------- this is where your problem is

cout<<"the reduced fraction is:"<<endl
	<<a/gcd<<"/"<<b/gcd;

system("pause");
return 0;
}

You are closing the function as soon as you return 0, so simply get rid of that line and you should be fine
Dec 15, 2011 at 5:16am
okay, i got that, when i run it, it always goes "the reduced fraction is 1/1" i enter for numerator 2491 denominator 3127, it should end with 47/59 with the gcd=53
Dec 15, 2011 at 7:02am
bump
Dec 15, 2011 at 11:48am
On line 23 'int gcd' hides that one on line 10. Means: the 'gcd' variable outside the while loop will never be changed and remains uninitialized. Remove the int on line 23, but then gdc will be 0 at the end. Take care that the 0 will not be assigned to gcd
Dec 16, 2011 at 12:57am
how would i take care of the zero, i try doing gcd!=0 b!=0 really any way to make it so it wont equal zero but it doesnt work
Dec 16, 2011 at 1:27am
it always goes "the reduced fraction is 1/1"
Liar. while (b != 0)

I'm going to guess that you just copy-paste that snip of code. And that that was inside a function.
¿Haven't you realized that you are modifying 'a' and 'b' ?
At the end of the loop, 'b' will be zero and 'a' will be the greatest common divisor that you were looking for. But you lost the original values.
Dec 16, 2011 at 1:56am
i changed it so n=a and d=b; but i cant get around it dividing by zero.

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
// problem 1.cpp : Defines the entry point for the console application.
//


#include<iostream>
#include<math.h>

using namespace std;

int n;
int d;
int a;
int b;
int gcd;

int main(){
	cout<<"Please enter numerator:"<<endl;
	cin>>n;
	cout<<endl;
	cout<<"Please enter denominator:"<<endl;
	cin>>d;
	cout<<endl;



	a=n;
	b=d;
	
     a % b;
    a = b;
    b = gcd;
	
	

cout<<"the reduced fraction is:"<<endl
	<<n/gcd<<"/"<<d/gcd;

return 0;

}
Dec 16, 2011 at 3:22am
bump
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
// problem 1.cpp : Defines the entry point for the console application.
//


#include<iostream>
#include<math.h>

using namespace std;

int n;
int d;
int a;
int b;
int gcd;

int main(){
	cout<<"Please enter numerator:"<<endl;
	cin>>n;
	cout<<endl;
	cout<<"Please enter denominator:"<<endl;
	cin>>d;
	cout<<endl;

	a=n;
	b=d;

	while (gcd!=0) { gcd = a % b; a = b; b = gcd; } 
	



cout<<"the reduced fraction is:"<<endl
	<<n/gcd<<"/"<<d/gcd;

return 0;

}
Dec 16, 2011 at 5:22am
I wrote:
At the end of the loop, (...) 'a' will be the greatest common divisor that you were looking for
Topic archived. No new replies allowed.