| | |
gcd problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2005
Posts: 17
Reputation:
Solved Threads: 0
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;
}
#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;
}
The traditional (if inefficient) algorithm is:
C++ Syntax (Toggle Plain Text)
# Pseudocode gcd ( m, n ) begin while m != n do if m > n m := m - n else n := n - m loop return m end
I'm here to prove you wrong.
>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.
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.
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!
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!
•
•
Join Date: Apr 2005
Posts: 17
Reputation:
Solved Threads: 0
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;
}
#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;
}
>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.
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.
•
•
Join Date: Apr 2005
Posts: 17
Reputation:
Solved Threads: 0
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;
}
#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;
}
![]() |
Other Threads in the C++ Forum
- Previous Thread: Formating
- Next Thread: Loading and accessing 2 Dimensional tables
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






