Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 11, Problem 5PP

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 help the people, but the king refused. One day a small group of rebels attempted 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 king’s 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.

Blurred answer
Students have asked these similar questions
A quaker was walking down a country road when he was suddenly confronted by a robber brandishing a pistol. The robber exclaimed, "Your money or your life!" My friend replied, "I cannot deliver my money because I should be aiding thee in evildoing, but exchange is lawful, and I will give thee my purse for the pistol." The thief consented to accept the purse, then the quaker pointed the gun to the robber's head and said, "Now buddy, give me back my pocketbook or the weapon may go off fire," according to the robber.
A bandit armed with a revolver approached a quaker as he strolled down a rural road. A robbery was in progress, and he said, "Your money or your life!" My buddy answered, "I can't give you my money because then I'd be helping you do evil, but exchange is legal, so here, take my pocketbook in return for your gun." The robber accepted the purse, but the Quaker put the pistol to his head and said, "Now friend, give me back my pocketbook or the weapon may go off fire," as recounted by the thief.
Last weekend, Mr. Moki took Daniel to a family reunion. Since Daniel had never attended a reunion, Mr. Moki introduced him to the four other kids in the room. Five kids then sat together. When Mr. Brown asked about their age, the first kid A said, "I am 2 years older than B on my left." B decided to do the same and said, "I am 2 years older than C on my left as well." C imitated, "I am 2 years older than D on my left." D said, "I am 2 years older than Daniel." When it came to Daniel, he answered honestly that he was 10. Mr. Moki burst into laughter and asked, "This is fun. How will you solve this problem?" "I am 10, so D is 10+2=12, C is 12+2=14, B is 14+2=16, and A is 16+2=18." Daniel answered quickly. "Well done!" said Mr. Moki, "Can any of you generalize a formula?" B reckoned that this was a recursive relation. A, who had been learning to program, said that this could be easily implemented by a loop Work required: Design the algorithm and implement the algorithm using an iterative…

Chapter 11 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 11.2 - What Java statement will sort the following array,...Ch. 11.2 - How would you change the class MergeSort so that...Ch. 11.2 - How would you change the class MergeSort so that...Ch. 11.2 - If a value in an array of base type int occurs...Ch. 11.3 - Convert the following event handler to use the...Ch. 11 - What output will be produced by the following...Ch. 11 - What output will be produced by the following...Ch. 11 - Write a recursive method that will compute the...Ch. 11 - Write a recursive method that will compute the sum...Ch. 11 - Complete a recursive definition of the following...Ch. 11 - Write a recursive method that will compute the sum...Ch. 11 - Write a recursive method that will find and return...Ch. 11 - Prob. 8ECh. 11 - Write a recursive method that will compute...Ch. 11 - Suppose we want to compute the amount of money in...Ch. 11 - Prob. 11ECh. 11 - Write a recursive method that will count the...Ch. 11 - Write a recursive method that will remove all the...Ch. 11 - Write a recursive method that will duplicate each...Ch. 11 - Write a recursive method that will reverse the...Ch. 11 - Write a static recursive method that returns the...Ch. 11 - Write a static recursive method that returns the...Ch. 11 - One of the most common examples of recursion is an...Ch. 11 - A common example of a recursive formula is one to...Ch. 11 - A palindrome is a string that reads the same...Ch. 11 - A geometric progression is defined as the product...Ch. 11 - The Fibonacci sequence occurs frequently in nature...Ch. 11 - Prob. 4PPCh. 11 - Once upon a time in a kingdom far away, the king...Ch. 11 - There are n people in a room, where n is an...Ch. 11 - Prob. 7PPCh. 11 - Prob. 10PPCh. 11 - Prob. 12PP

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
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
Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole
Computational Software for Intelligent System Design; Author: Cadence Design Systems;https://www.youtube.com/watch?v=dLXZ6bM--j0;License: Standard Youtube License