944,156 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5173
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 2nd, 2005
0

gcd problem

Expand Post »
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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mdbrock7 is offline Offline
17 posts
since Apr 2005
Apr 2nd, 2005
0

Re: gcd problem

The traditional (if inefficient) algorithm is:
C++ Syntax (Toggle Plain Text)
  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
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 2nd, 2005
0

Re: gcd problem

Thanks for the reply but I'm still confused
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mdbrock7 is offline Offline
17 posts
since Apr 2005
Apr 3rd, 2005
0

Re: gcd problem

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 3rd, 2005
0

Re: gcd problem

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!
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Apr 3rd, 2005
0

Re: gcd problem

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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mdbrock7 is offline Offline
17 posts
since Apr 2005
Apr 3rd, 2005
0

Re: gcd problem

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 3rd, 2005
0

Re: gcd problem

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mdbrock7 is offline Offline
17 posts
since Apr 2005
Apr 3rd, 2005
0

Re: gcd problem

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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mdbrock7 is offline Offline
17 posts
since Apr 2005
Apr 3rd, 2005
0

Re: gcd problem

Why do you return m after the loop??

There's no need to :!:

And look at your loop, look how you defined it :!:
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Formating
Next Thread in C++ Forum Timeline: Loading and accessing 2 Dimensional tables





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC