passing data between functions

This is a discussion on passing data between functions within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that is supposed to reduce a fraction to its simplist terms. I can only use ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    66

    passing data between functions

    I am writing a program that is supposed to reduce a fraction to its simplist terms. I can only use <iostream.h> and <string.h>. I have a function working that finds the greatest common divisor and I have a function that will take that and simplify the fraction.

    How do I get the data from the first function (the GCD) into the second function (so It can do the math)?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The GCD function should have its output as its return value. Also, you should use <iostream> and <string> (I don't use strings myself so I'm not sure of the name but the .h headers are not standard in C++). Also, for efficiency you should use the Euclidean algorithm to compute the GCD if you weren't doing that (trying to find common factors of the numerator and denominator is horribly inefficient by comparison).

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    You can use a function call as an argument to another function as long as the previous function return type is the same (or is convertible) as the expected parameter type.

    int function1(int);
    void function2(int, int);

    int value = 7;
    function2(function1(value), 13);
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    How would that fit into this exactly?
    Code:
    int main()
    {
    	int w, x;
       char junk;
    
    
    	cout << "This program calculates the GCD of a fraction and simplifies the fraction\n";
    	cout << "Value of first numerator: ";
    	cin >> w;
    	cout << "Value of first denominator: ";
    	cin >> x;
    
    
    	cout << "\nThe Greatest Common Divisor of "
    	     << w << " and " << x << " is " << GCD(w, x) << endl;
    
       cout << "\nThe Simplified Fraction is "
    	     << simplify(w,x) << endl;
    
       cin >> junk; //This input allows for display.
    
    	return 0;
    }

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    It wouldn't. But then and again you asked how could you pass the result of GCD into another function. You need to create a third argument on the simplify() function that will take the gcd
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    Like this?
    Code:
    int simplify(x, y, GCD);

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    C'mon you can do better than that...

    Think...

    What is the return type of the gcd function?
    What does that number represent?
    How are you going to use it inside the simplify function?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    GCD returns type int.
    GCD is the greatest common divisor.
    I am going to divide W and X by the GCD to get a new fraction.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    So? What is that you need to pass to simplify()?

    an int representing the numerator
    an int representing the denominator
    and...?

    Then look at my previous example showing how to use a function call inside another.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    66
    This is GCD.
    Code:
    int GCD(int a, int b )
    {
    	int remainder, quotient, divisor,numerator;
    		if (a > b)
    		{	remainder=a%b;
    		quotient=a/b; divisor=b;
    		}else{
    			remainder=b%a;
    		quotient=b/a;divisor=a;
    		}
    
    		while( remainder!=0 )
    		{
    		numerator=divisor;
    		divisor=quotient;
    		remainder=numerator%divisor;
    		quotient=numerator/divisor;
    		}
             return divisor;
    
    }

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Good luck solving this problem. The solution I already gave you.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Your GCD code is way too complicated. Look at the Wikipedia pseudocode. You can do it in around 5 lines and using only remainder and not division.

    http://en.wikipedia.org/wiki/Euclidean_algorithm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. typecasting data for parameter passing
    By daluu in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 01:58 PM
  3. Replies: 0
    Last Post: 01-23-2002, 12:07 PM
  4. Passing data/pointers between functions
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-01-2001, 11:59 PM
  5. passing functions with variable
    By itld in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 10:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21