1

I'm trying to answer this question from my C++ book that says: There are n people in a room, where n is an integer greater than or equal to 2. Each person shakes hands once with every other person. What is the total number of handshakes in the room? Write a recursive function to solve this problem. I've written a program, but nothing is being outputted from the function. I'm pretty sure my stuff inside the handshake function is correct, but nothing is being outputted by the function inside the main function. It keeps giving me an error: Unhandled exception at 0x00c01639 in Problem2.exe: 0xC00000FD: Stack overflow. Thank you for your help in advance!

#include <iostream>
#include <conio.h>

using namespace std;

int handshake(int n);

int main()
{
    int i, n;

cout<<"How many people are in the room? ";
cin>>i;


for (n = 1; n = i; n++)
{
    handshake(n);
}

cout<<"There are "<<handshake(n)<<" in the room"<<endl;

getche();
return 0;
}

int handshake(int n)
{
    if (n == 1)
    {
            return 0;
        }
    else if (n == 2)
    {
                        return 1;
}
else
{
    return (handshake(n) + (n - 1));
}
}
1
  • 1
    The for loop in the main function looks wrong. Are you sure you want to use the assignment operator in the condition, and not the less than operator? Commented Oct 27, 2012 at 5:12

2 Answers 2

1

Your problem is here:

return (handshake(n) + (n - 1));

Notice you're returning handshake(n) on a call of handshake(n), throwing you into infinite recursion land (and causing the call stack to overflow, hence your error).

This sounds like homework so I won't give any specific solution. I will make the following remarks:

  • Recursive functions need to have a base case (which you have) and a recursive step (which you need to fix). In particular, if f(x) is a recursive function, its result should depend on f(x-e), where e is strictly greater than 0.
  • You only need to call a recursive function once in your main code; it will take care of calling itself when needed.
Sign up to request clarification or add additional context in comments.

1 Comment

How would I go about going around that? Isn't there a way to call the function in itself as a functioning recursion?
1

The problem is because of stackoverflow which happens because the call to handshake(n) is making a recursive call to handshake(n) again and this infinite recursive calls causes the stack to explode.

Recursive calls should be called with arguments that are closer to the values that lead to base conditions, 0 and 1 in your case.

In your case the correct recursive call is:

return (handshake(n-1) + (n - 1));

As the recursive formula for number of hand shakes in a party is:

H(n) = H(n-1) + n-1

with the base condition of H(1) = 0 and H(2) = 1 where n is the number of people in the party and H(n) is the number of handshakes when every person in the party shakes hand with every other person in the party.

Also the condition in your for loop:

for (n = 1; n = i; n++)

will assign i to n. But what you want is to compare the value as:

for (n = 1; n <= i; n++)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.