1,105,450 Community Members

Recursive design to input coupon and money.

Member Avatar
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

This is another homework problem. I thought it was easier, and I wouldn't need help, but I keep getting a segmentation fault when running the program.
The assignment is to use a recursive function to output the amount of candybars we can buy with a user-inputted amount of money. Each candybar costs $1, and each candybar gives a coupon. 7 coupons can be redeemed for an additional candybar. The example he gave us is:

"For example, if we have $20 dollars then we can initially buy 20 candy bars. This gives us 20 coupons. We can redeem 14 coupons for 2 additional candy bars. These two additional
candy bars have 2 more coupons, so we now have a total of 8 coupons when added to the 6
left over from the original purchase. This gives us enough to redeem for 1 more candy bar.
As a result we now have 23 candy bars and 2 left over coupons."

Here is the code I have. When I don't get a segmentation fault, I get it returning zero.

//File Name: assg3.cpp

#include <iostream>
using namespace std;

int candybars=0;

int recursivecandy(int A, int B) //A is money, B is coupons
{
    do
    {
       candybars = candybars + A +(B/7);
       B = A + B/7, B%7;
       A = 0;
       return recursivecandy(A, B);
    }while (B>=7);
}

int main()
{    
    int money;
    int coupons=0;
    cout << "Please enter the number of dollars you have: $";
    cin >> money;
    cout << "With " << money << " dollars, you can buy " << recursivecandy(money, coupons) << " candy bars.\n";
    
    return 0;
}

Fairly straightforward, I use a do-while loop to run inside the recursive function because I want it run at least once (you start with no coupons, which is my stopping case). Does anyone see anything wrong? Any help would be greatly appreciated.

Member Avatar
daviddoria
Posting Virtuoso
1,968 posts since Feb 2008
Reputation Points: 334 [?]
Q&As Helped to Solve: 204 [?]
Skill Endorsements: 8 [?]
Featured
 
0
 

I suggest using a debugger to step through the code. It will show you exactly where/when the program crashes, as well as you can look at the conditions (values of variables, etc) when it occurs.

David

Member Avatar
arkoenig
Practically a Master Poster
691 posts since Jun 2010
Reputation Points: 340 [?]
Q&As Helped to Solve: 110 [?]
Skill Endorsements: 9 [?]
 
0
 

I don't even need to ask how your program is supposed to work because it can't -- there is no way for recursivecandy to return that does not involve a recursive call, so there is no way for it ever to return a meaningful value.

Member Avatar
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What do you mean? I call the recursive function in the main function (line 25) and within the recursive function, I call the recursive function (line 15).

Member Avatar
arkoenig
Practically a Master Poster
691 posts since Jun 2010
Reputation Points: 340 [?]
Q&As Helped to Solve: 110 [?]
Skill Endorsements: 9 [?]
 
0
 

I mean that once your recursivecandy function has been called, there is no legitimate way to get out of it. Consider:

int foo()
{
    return foo();
}

There is no way out of this function, because in order to return, it must first call itself, and in order for that call to return, it must first call itself again, and so on. Let's try to solve that problem by making the call conditional:

int bar(int n)
{
    if (n < 100)
        return bar(n+1);
}

Now there are two ways to leave this function. One is by falling off the end, which returns garbage; the other is by calling itself first. So again there is no legitimate way to leave it.

Your recursivecandy function has the same problem as my bar function.

Member Avatar
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

So you're saying I need two options, one that will evaluate the recursive function, then when that is completed, the function is returned by the other option?
Something Like:

int recursivecandy(int A, int B, int C) //A is money, B is coupons, C is candybars
{
    if (B>=7)
    {
       C = C + A +(B/7);
       B = A + B/7, B%7;
       A = 0;
       recursivecandy(A, B, C);
    }
    else
    {
       return recursivecandy (A, B, C);
    }
}
Member Avatar
template<>
Junior Poster
165 posts since Feb 2011
Reputation Points: 37 [?]
Q&As Helped to Solve: 26 [?]
Skill Endorsements: 0 [?]
 
0
 

I would think a terminal condiion is needed, some way to end it all...

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 
(int A, int B, int C) //A is money, B is coupons, C is candybars

Allow me to stick my nose in here for a peripheral point, but there's one surefire way to make sure that someone reading your code knows what is money, what is coupons, and what is candybars. Name your variables "money","coupons",and "candybars."

Meaningful names for your variables are important, especially when you get to page 110 of a piece of code and your comment about A,B, and C is many pages back.

Member Avatar
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes, but the only time 'A, B, and C' are mentioned are in the small recursive function. When the function is called, I use named variables - that's just based off how my instructor has written the examples.

Member Avatar
daviddoria
Posting Virtuoso
1,968 posts since Feb 2008
Reputation Points: 334 [?]
Q&As Helped to Solve: 204 [?]
Skill Endorsements: 8 [?]
Featured
 
1
 

Haha, you will quickly learn that just because your instructor does it doesn't mean it is right :) I'd follow jonsca's advice on this one.

Member Avatar
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I realize that, and in a real-world situation, I would do so. Just now I follow my instructor's example.

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

It's all good, it was more of a suggestion for the future than anything else anyway.

Member Avatar
markshah2
Newbie Poster
2 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 

Are these coupon still available in the market?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: