Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org GCD Algo. Help
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 10-16-2004, 12:10 PM #1 dontcare LQ Newbie   Registered: Oct 2004 Posts: 28 Rep: GCD Algo. Help I need help using the GCD Algo. in my program, I can't figure out to use the algo in my program. The algo: Code: ``` int gcd(int a, int b){ assert(b != 0); int rem = a % b; while(rem !=0 ){ a = b; b = rem; rem = a % b; } return b; The program: #include using namespace std; int Num, Num2, Denom, Denom2; int res_up, res_down; int Low_Value, High_Value; char opchar,answer,symbol; void Calculate() { switch (opchar) { case '+': res_up=(Denom2*Num) + (Denom*Num2); res_down= Denom*Denom2; symbol = '+'; break; case '-': res_up=(Denom2*Num) - (Denom*Num2); res_down= Denom*Denom2; symbol = '-'; break; case '*': res_up= Num * Num2; res_down= Denom * Denom2; symbol = '*'; break; case '/': res_up= Num * Denom2; res_down= Denom * Num2; symbol = '/'; break; default: // If operator is illegal shut program down cout << "Invalid operator." << endl; } } void input() { cout << "Enter an operation to perform { + - / * } "; cin >> opchar; cout << endl; cout << "Enter the numerator and denominator for the first fraction; include a space: "; cin >> Num >> Denom; cout << endl; cout << "Enter the numerator and denominator for the second fraction; include a space: "; cin >> Num2 >> Denom2; cout << endl; } //------------------------------------------------------------------- void output() { cout << "\n " << Num << " " << Num2 << " " << res_up << " " << endl; cout << "--- " << symbol << " --- = --- =\n"; cout << " " << Denom << " " << Denom2 << " " << res_down << endl; cout <<"Would you like to do another fraction? "; cin >> answer; cout << endl; } int main() { do{ input(); Calculate(); output(); } while ((answer == 'y') || (answer == 'Y')); return 0; }``` Last edited by dontcare; 10-16-2004 at 01:39 PM.
 10-16-2004, 07:20 PM #2 jordanGSU Member   Registered: Sep 2004 Distribution: Debian, Slackware, Arch Posts: 65 Rep: The Greatest Common Denominator should return an integer which is the GCD of the two numbers passed into the function. I didnt run your code to see if it works, but the GCD fucnction looks correct, in case its not, here is a version of it that I wrote a while back // returns the greatest common divisor of A and B long GCD(long A, long B) { if( B==0 ) return A; return GCD(B,A%B); } I cant really tell what the rest of your code is doing...hope this helps a little. FYI: global variables are bad programming practice unless they are absolutely necessary. Youve got like 11...makes my head want to explode
 10-16-2004, 09:50 PM #3 dontcare LQ Newbie   Registered: Oct 2004 Posts: 28 Original Poster Rep: Thanks for the advice, the problem i had earlier was solved.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post cathiejcm Linux - Software 1 11-11-2005 07:25 AM LinuxTiro Programming 5 11-17-2003 09:02 PM

All times are GMT -5. The time now is 11:56 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -