Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

  1. Home >
  2. All Categories >
  3. Computers & Internet >
  4. Programming & Design >
  5. Resolved Question
mikehockstein mikehock...
Member since:
June 11, 2006
Total points:
441 (Level 2)

Resolved Question

Show me another »

Can you return multiple values in a C++ function?

I have to use recursion to figure out how many 2's and 5's it will take to make an integer greater than 4. For example with the integer 14, you need two 2's and two 5's. For 13, you need four 2's and one 5.

It basically works like this. As you count up, if there is more than two 2's then you take away two 2's and add a 5, otherwise you minus one away from the 5's and add three 2's. I am asked to do this in a recursive function but i am unsure as to how. The inputs are the integer wanted and I assume the 2's and 5's. So i think it is

fcnName(n,two,five)
{
if(n==4)
return two 2's and zero 5's

unsure of what else to put in here
}
justme by justme
A Top Contributor is someone who is knowledgeable in a particular category.
Member since:
March 31, 2006
Total points:
30,360 (Level 7)
Badge Image:
A Top Contributor is someone who is knowledgeable in a particular category.

Best Answer - Chosen by Asker

you can only return 1 variable using "return". there is a way to do what you want though. basically you pass the address of the "two" and "five" variables.
example:

int n, two, five;
fcnName(n, &two, &five) //no need to return anything
{
//your function code
}
  • 1 person rated this as good
Asker's Rating:
3 out of 5
Asker's Comment:
thanks

There are currently no comments for this question.

Other Answers (3)

  • G. Whilikers by G. Whilikers
    Member since:
    July 02, 2006
    Total points:
    28,573 (Level 7)
    The easy way to pass multiple values around is to create a single object with the variables inside, then pass that one object around. This can be done with a class or a struct. Here's an incomplete example...

    class ResultsObject {
    int subtotal = 0;
    int twos = 0;
    int fives = 0;
    }

    // the recursive function

    ResultsObject doSomething( int target, ResultsObject results ) {
    if (we_are_done) {
    return results;
    }

    if (we_should_add_five) {
    results.fives += 1;
    results.subtotal += 5;
    }
    else {
    results.twos += 1;
    results.subtotal += 2;
    }

    return doSomething(target, results);

    }

    // main

    void main ()
    {
    int target = 14;
    ResultsObject scratch, answer;

    answer = doSomething (target, scratch);
    }


    That's one way to do it, probably close to what you were thinking of. You could also do it by leaving the variables in one place and passing a pointer around (you don't need to return the pointer); or you could actually make the recursive function a member of the class itself so you have instant access to the variables and only have to send the answer back. I'll leave that as an exercise for the student...
  • John Moore by John Moore
    Member since:
    April 24, 2006
    Total points:
    336 (Level 2)
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;


    string twoFive(int n,int &two, int &five){
    string note;
    if((n<=0)||(n==1)){
    note="";
    cout<<"the number have "<<two<<" twos and "<<five<<" fives"<<endl;
    return note;
    }else {
    if((n-2>0)&&(two<2)){
    two++;
    n-=2;
    note = twoFive(n,two,five);
    }else if((n-5>=0)&&(five<=2)){
    five++;
    n-=5;
    note = twoFive(n,two,five);
    }
    return note;
    }
    }

    int main(){
    int tr=13;
    int two=0;
    int five=0;
    cout<<twoFive(tr,two,five);
    cout<<"the number "<<tr<<" have "<<two<<" twos and "<<five<<" fives"<<endl;
    return 0;
    }

    this is your program but think how it works.

Answers International

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer.

Help us improve Yahoo! Answers. Send Feedback