473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Euclid's Algorithm

Can someone help me with my C++ program. For some reason I Compile the program, but when I run it I have problems. I type in the first integer, then I type in the second integer and the program times out (ie it disappears). I start over and it does the same thing.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2.  
  3. using namespace std ;
  4.  
  5. int main() 
  6.  
  7.     int x, y, temp, remainder;
  8.  
  9.     // read in the two integers
  10.  
  11.     cout << endl ;
  12.     cout << "Enter the first number (integer) : " ; 
  13.     cin >> x ;
  14.     cout << "Enter the second number (integer) : " ; 
  15.     cin >> y ;
  16.  
  17.     // echo inputs
  18.  
  19.     cout << "Input numbers are: " << x << " , " << y << endl;
  20.  
  21.     {// exchange values of x and y 
  22.     if(x == y)
  23.     {
  24.         return x;
  25.     }
  26.     else if(x < y)
  27.     {
  28.         y = y - x ;
  29.     }
  30.     else
  31.     {
  32.         x = x - y ;
  33.     }
  34. }
  35.  
  36.     /* At this point we will always have x >= y */
  37.  
  38.     //Initialize remainder.
  39.  
  40.     while (x > 0)
  41.         {         
  42.             temp = x % y;
  43.             x = y;
  44.             y = temp;
  45.         }
  46.  
  47.     // display the result
  48.     cout << endl ;
  49.     cout << "The GCD is: " << y << endl ;
  50.  
  51.     system("PAUSE");
  52.     return (0); // terminate with success
  53. }
Oct 28 '07 #1
4 1448
Ganon11
3,652 Expert 2GB
I can see 2 things wrong.

1) If x == y, then you return x - but you're returning it inside main(). main() is expecting a 0 to be returned, signaling that everything worked as expected, no errors. When you return x, any non-zero value is going to signal that there was an error somewhere along the line. Maybe you should simply ignore this case?

2) Your next two if...branches don't work like your comment says they should. The first half is correct - if y is greater than x, subtract x from y. But if x is greater than y, you subtract y from x, making x smaller than y. So in the case that x is initially greater than y, your comment is false, because afterwards y > x.

Fixing these may or may not fix your hangtime problem - I'll try and look through your code again.
Oct 28 '07 #2
JosAH
11,448 Expert 8TB
2) Your next two if...branches don't work like your comment says they should. The first half is correct - if y is greater than x, subtract x from y. But if x is greater than y, you subtract y from x, making x smaller than y. So in the case that x is initially greater than y, your comment is false, because afterwards y > x.
Not necessarily; a counter example: x == 21, y == 3. After the subtraction:
x == 18, y == 3 which still makes x > y.

The entire program is just totally wrong. The classic GCD algorithm can be found
anywhere on Google so I leave it at that.

kind regards,

Jos
Oct 28 '07 #3
Ganon11
3,652 Expert 2GB
Not necessarily; a counter example: x == 21, y == 3. After the subtraction:
x == 18, y == 3 which still makes x > y.

The entire program is just totally wrong. The classic GCD algorithm can be found
anywhere on Google so I leave it at that.

kind regards,

Jos
In which case, the first if...branch is also incorrect, using the same values you had, but reversed (x==3, y==21).
Oct 28 '07 #4
JosAH
11,448 Expert 8TB
In which case, the first if...branch is also incorrect, using the same values you had, but reversed (x==3, y==21).
Yep, but I was too lazy to show two examples; as I wrote: the entire algoritm
as shown is completely wrong; no need to dig into the (incorrect) details more
than necessary, i.e. one counter example is more than enough.

kind regards,

Jos
Oct 28 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
12
by: Erik the Red | last post by:
In Fundamental Algorithms (The Art of Computer Programming), the first algorithm discussed is Euclid's Algorithm. The only idea I have of writing this in python is that it must involve usage of...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
32
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
4
by: sdlt85 | last post by:
Hi, Can someone help me with an idea on how to start writing a C++ code for generating greatest common divisor and the linear combination of two intergers represented as gcd(m, n)= mx + ny and...
0
by: aruna | last post by:
hey guys i earlier had very valuable responses from you all for base64 encoding algorithm.so thank for that. so now i need your assistance to do a float encoding algorithm. you may wonder why i'm...
11
lotus18
by: lotus18 | last post by:
Hello experts The Euclid's Method: If u is greater then v then the greatest common divisor of u and v is the sum as the greatest common divisor of v and u-v. Now my question is, how can I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.