Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
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)
8. A country has coins of denomination 3, 5 and 10 respectively. Write a recursive function canchange() which returns -1 if it is not possible to pay a value of k using these coins. Otherwise, it returns the minimum number of coins needed to make the payment. For example, canchange(7) will return -1. On the other hand, canchange(14) will return 4 because 14 can be paid as 3+3+3+5 and there is no other way to pay with fewer coins
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