gcd problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 17
Reputation: mdbrock7 is an unknown quantity at this point 
Solved Threads: 0
mdbrock7 mdbrock7 is offline Offline
Newbie Poster

gcd problem

 
0
  #1
Apr 2nd, 2005
I am in a beginner C++ class and I have a homework problem where I must to find the greatest common divisor through subtraction. I am totally lost. Can anyone show me what I am doing wrong?

#include <iostream>
using namespace std;


int main ()
{
int m;
int n;
int firstNum = m;
int secondNum = n;



cout << "1st number:" << endl;
cin >> firstNum;
cout << "2nd number:" << endl;
cin >> secondNum;

while (m != 0)
{


if (n > m )
{
int t = m; m = n; n = t;
}

m = n - m;

}

cout << "the gcd of" << endl;
cout << firstNum;
cout << "and" << endl;
cout << secondNum;
cout << "is" << endl;
cout << n;

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: gcd problem

 
0
  #2
Apr 2nd, 2005
The traditional (if inefficient) algorithm is:
  1. # Pseudocode
  2. gcd ( m, n )
  3. begin
  4. while m != n do
  5. if m > n
  6. m := m - n
  7. else
  8. n := n - m
  9. loop
  10.  
  11. return m
  12. end
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 17
Reputation: mdbrock7 is an unknown quantity at this point 
Solved Threads: 0
mdbrock7 mdbrock7 is offline Offline
Newbie Poster

Re: gcd problem

 
0
  #3
Apr 2nd, 2005
Thanks for the reply but I'm still confused
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: gcd problem

 
0
  #4
Apr 3rd, 2005
>Thanks for the reply but I'm still confused
Well, aside from spelling out the algorithm so that it's trivial to convert to C++, there's not much more I can do to help you. Search google for analyses on the algorithm. It's been around for a while, so you shouldn't have trouble finding anything.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: gcd problem

 
0
  #5
Apr 3rd, 2005
Hi,

Ever tought about using the Algorithm of Euclides?

Example of getting the greatest common devider gcd(1147, 851)
Steps:
1) 1147 = 1x851 + 296
2) 851 = 2x296+259
3) 296 = 1x259 + 37
4) 259 = 7x37
Rest equals 0.
Then the gcd equals 37.

If you can translate this into C++ code, you should be able to find the solution.

If it's not clear, google for the Algorithm of Euclides!
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 17
Reputation: mdbrock7 is an unknown quantity at this point 
Solved Threads: 0
mdbrock7 mdbrock7 is offline Offline
Newbie Poster

Re: gcd problem

 
0
  #6
Apr 3rd, 2005
The problem is I have to use this basic shell to get my answer. This is what I have so far. No matter what two numbers I enter I always get an answer of zero which is obviously not the gcd. I have spent hours trying to adjust it but I just can't get it to work. Any thoughts on a fix?

#include <iostream>
using namespace std;


int main ()
{
int m;
int n;
int firstNum;
int secondNum;


cout << "1st number:" << endl;
cin >> firstNum;
cout << "2nd number:" << endl;
cin >> secondNum;

m = firstNum;
n = secondNum;

while (m != 0)
{


if (n > m )
{
int t = m; m = n; n = t;
}

m = m - n;

}

cout << "the gcd of" << endl;
cout << firstNum;
cout << "and" << endl;
cout << secondNum;
cout << "is" << endl;
cout << m;

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: gcd problem

 
0
  #7
Apr 3rd, 2005
>Ever tought about using the Algorithm of Euclides?
The pseudocode I gave was the Euclidean algorithm. It was an earlier formulation of the problem using subtraction instead of the later (more efficient) algorithm that uses the remainder of integer division.

>The problem is I have to use this basic shell to get my answer.
Then it's homework, and we won't do it for you. Unless you're incapable of doing simple math, it's a small step to convert the algorithm I gave you into something that fits your shell.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 17
Reputation: mdbrock7 is an unknown quantity at this point 
Solved Threads: 0
mdbrock7 mdbrock7 is offline Offline
Newbie Poster

Re: gcd problem

 
0
  #8
Apr 3rd, 2005
You can't give me a hint where my code goes wrong? It compiles and executes without any errors but I'm just not getting the right answer. Is the problem in my If statement?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 17
Reputation: mdbrock7 is an unknown quantity at this point 
Solved Threads: 0
mdbrock7 mdbrock7 is offline Offline
Newbie Poster

Re: gcd problem

 
0
  #9
Apr 3rd, 2005
i tried using your pseudocode and this is what I now have. It doesn't return any value. Any suggestions?

#include <iostream>
using namespace std;


int main ()
{
int m;
int n;
int firstNum;
int secondNum;


cout << "Enter 1st number:" << endl;
cin >> firstNum;
cout << "Enter 2nd number:" << endl;
cin >> secondNum;

m = firstNum;
n = secondNum;

while (m != n);
{
if (m > n)
m = m - n;
else
n = n - m;
}

return m;



cout << "the gcd of" << endl;
cout << firstNum;
cout << "and" << endl;
cout << secondNum;
cout << "is" << endl;
cout << m;

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: gcd problem

 
0
  #10
Apr 3rd, 2005
Why do you return m after the loop??

There's no need to :!:

And look at your loop, look how you defined it :!:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC