Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 14, Problem 4P

Explanation of Solution

Recursive function for “handshake” function:

The recursive function definition for “handshake” function is shown below:

//Function definition for "handshake" function

int handshake(int n)

{

      /* If "n" is equal to "1", then */

      if(n == 1)

            //Returns "0"

            return 0;

      /* If "n" is equal to "2", then */

      else if(n == 2)

            //Returns "0"

            return 1;

      //Otherwise

      else

     /* Recursively call the function "handshake" with subtracting the value of "n" with "1" */

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

}

Explanation:

The above function is used to compute the number of handshakes in a room using recursive function.

  • In this function, If the value of “n” is equal to “1”, then returns “0”.
  • If the value of “n” is equal to “2”, then returns “1”.
  • Otherwise, that is if the value of “n” is greater than “2”, then returns the value by “n - 1 + handshake(n - 1)”...

Blurred answer
Students have asked these similar questions
Write a recursive function that takes as a parameter a nonnegative integer    and generates the following pattern of stars. If the nonnegative integer is 4,    the pattern generated is as follows:        ****    ***    **    *    *    **    ***    ****        Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive function to generate the pattern. For    example, specifying 4 as the number of lines generates the preceding pattern.
Write a recursive function called draw_triangle() that outputs lines of '*' to form a right side up isosceles triangle. Function draw_triangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '*' on the first line for correct formatting. Hint: The number of '*' increases by 2 for every line drawn. Ex: If the input of the program is: 3 the function draw_triangle() outputs: * *** Ex: If the input of the program is: 19 the function draw_triangle() outputs:    *    ***   *****  *******  ********* *********** ************* *************** ***************** ******************* Note: No space is output before the first '*' on the last line when the base length is 19. if __name__ == '__main__':    base_length = int(input())    draw_triangle(base_length)
No plagarism! Correct and detailed answer ( code with comments and output screenshot if necessary)
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning