Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In java 5. Once upon a time in a kingdom far away, the king hoarded food and the people starved. His adviser recommended that the

In java

5. Once upon a time in a kingdom far away, the king hoarded food and the people starved. His adviser recommended that the food stores be used to 11.2 programming with Recursion 863 help the people, but the king refused. One day a small group of rebels at- tempted to kill the king but were stopped by the adviser. As a reward, the adviser was granted a gift by the king. The adviser asked for a few grains of wheat from the kings stores to be distributed to the people. The number of grains was to be determined by placing them on a chessboard. On the first square of the chessboard, he placed one grain of wheat. He then placed two grains on the second square, four grains on the third square, eight grains on the fourth square, and so forth. Compute the total number of grains of wheat that were placed on k squares by writing a recursive method getTotalGrains(k, grains). Each time getTotalGrains is called, it places grains on a single square; grains is the number of grains of wheat to place on that square. If k is 1, return grains. Otherwise, make a recursive call, where k is reduced by 1 and grains is doubled. The recursive call computes the total number of grains placed in the remaining k 1 squares. To find the total number of grains for all k squares, add the result of the recursive call to grains and return that sum.

6. 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 method to solve this problem with the following header: public static int handshake(int n) where handshake(n) returns the total number of handshakes for n people in the room. To get you started, if there are only one or two people in the room, then: handshake(1) = 0 handshake(2) = 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions