LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-16-2004, 12:10 PM   #1
dontcare
LQ Newbie
 
Registered: Oct 2004
Posts: 28

Rep: Reputation: 15
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 <iostream>
 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.
 
Old 10-16-2004, 07:20 PM   #2
jordanGSU
Member
 
Registered: Sep 2004
Distribution: Debian, Slackware, Arch
Posts: 65

Rep: Reputation: 15
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
 
Old 10-16-2004, 09:50 PM   #3
dontcare
LQ Newbie
 
Registered: Oct 2004
Posts: 28

Original Poster
Rep: Reputation: 15
Thanks for the advice, the problem i had earlier was solved.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
editing the linux cpu scheduling algo cathiejcm Linux - Software 1 11-11-2005 07:25 AM
Algo for Relatively prime No. LinuxTiro Programming 5 11-17-2003 09:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration