A human pyramid is a way of stacking people vertically in a triangle. With the exception of the people in the bottom row, each person splits their weight evenly on the two people below them in the pyramid. For example, in the pyramid to the right, person A splits her weight across people B and C, and person H splits his weight – plus the accumulated weight of the people he’s supporting – onto people L and M.
write a recursive function
double weightOnBackOf(int row, int col);
that takes as input the row and column number of a person in a human pyramid, then returns the total weight on that person's back. The row and column are each zero-indexed, so the person at row 0, column 0 is on top of the pyramid, and person M in the above picture is at row 4, column 2. So if we look at the above example, weightOnBackOf(1, 1) would return 100 pounds (since person C is shouldering 100 pounds on her back), and weightOnBackOf(4, 2) should return 625 (since person M is shouldering a whopping 625 pounds on her back).
Your implementation of weightOnBackOf must be implemented recursively and must not use any loops (for, while, or do ... while).
Your program will ask for a weight for the humans in your human pyramid . You can assume that all humans in the pyramid (like in the example above everyone weighs 200lbs) will be that weight . You should test for up to 5 rows. If it works for five it will work for an infinite number of rows and columns. This assignment MUST use RECURSION in Java.
SOLUTION
//Assuming that everyones' weight is 200lbs
import java.util.Scanner;
public class backweight {
static double every_ones_weight;
public static void main(String[] args) {
Scanner sc= new
Scanner(System.in);
System.out.println("Enter
everyones' weight:");
every_ones_weight=sc.nextDouble();
System.out.println("Whose weight on
the back you want to know. Please enter row and column
value:");
System.out.print(weightOnBackOf(sc.nextInt(),sc.nextInt()));
}
static double weightOnBackOf(int row, int col)
{
if (row == 0 && col ==
0){
return 0;
}
if (row
>= 1 && col == 0){
return
(every_ones_weight)/2 + 0.5*weightOnBackOf(row-1,col);
}
if (row
>= 1 && col == row){
return
(every_ones_weight)/2 + 0.5*weightOnBackOf(row-1,col-1);
}
if (row
>= 1 && (col > 0 && col < row)){
return
every_ones_weight + 0.5*(weightOnBackOf(row-1,col-1) +
weightOnBackOf(row-1,col));
}
// returns
0.0 if the row and col are out of bounds.
return 0.0;
}
}
HOPE THIS HELPS! IF YOU LIKE THE ANSWER PLEASE RATE IT.
THANKS!
A human pyramid is a way of stacking people vertically in a triangle. With the exception...
Advanced Object-Oriented Programming using
Java
Assignment 4: Exception Handling and Testing in
Java
Introduction - This assignment is
meant to introduce you to design and implementation of
exceptions in an object-oriented language. It will
also give you experience in testing an
object-oriented support class.
You will be designing and implementing a version of the game
Nim. Specifically, you will design and implement a
NimGame support class that stores all actual
information about the state of the game, and detects and throws...
Assignment 2 In this assignment, you will write two short programs to solve problems using recursion. 1. Initial Setup Log in to Unix. Run the setup script for Assignment 2 by typing: setup 2 2. Towers of Hanoi Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by...
Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...
+ Run C Code IMPORTANT: • Run the following code cell to create the input file, biostats.csv, which you will be using later. 74, In [ ]: N %%file biostats.csv Name, Sex, Age, Alex, M, 41, Bert, M, 42, Dave, M, 39, Elly, F, 30, Fran, F, 33, Jake, M, F, Luke, M, 34, F Myra, M, M, 38, Ruth, F, 28, 22 22 323 47 47, Height, Weight 170 200 167 70 115 143 139 280 98 75, 350...
i need some help with this lab ASAP please!
HUMAN GENETICS It to study because of the relatively long life span and the limited number In addition, the number of chromosome pairs (23) increases the possible number of genetic combinations. It is possible, however, to take a sample from human frequency of a trait and the possible ways a given trait is inherited. populations to estimate the Objectives .Investigate the inheritance of some human traits. Estimate the frequency of selected...
AssignmentBitmap files map three 8-bit (1-byte) color channels per pixel. A pixel is a light-emitting "dot" on your screen. Whenever you buy a new monitor, you will see the pixel configuration by its width and height, such as 1920 x 1080 (1080p) or 3840x2160 (4K). This tells us that we have 1080 rows (height), and each row has 1920 (width) pixels.The bitmap file format is fairly straight forward where we tell the file what our width and height are. When...
Because of its inability to control film and personnel
costs in its radiology department, Sanger General Hospital wants to
replace its existing picture archive and communication (PAC) system
with a newer version. The existing system, which has a current book
value of $2,250,000, was purchased three years ago for $3,600,000
and is being depreciated on a straight-line basis over an
eight-year life to a salvage value of $0. This system could be sold
for $800,000 today. The new PAC system...
Help I have taken this test so many times : These tests are intended for master's and doctoral students. Read these directions carefully! The below test includes 10 questions, randomly selected from a large inventory. Most questions will be different each time you take the test, You must answer at least 9 out of 10 questions correctly to receive your Certificate. You have 40 minutes to complete each test, and you must answer all 10 questions in order to to...
Match the following terms with the appropriate description
below:
a. alleles b. autosomes c. dominant allele d. genotype e.
heterozygous f. homozygote g. phenotype h. recessive allele i. sex
chromosomes
1. ________________ genetic make-up
2. ________________ how genetic make-up is expressed
3. ________________ chromosomes that dictate most body
characteristics
4. ________________ alternative forms of the same gene
5. ___________an individual bearing two alleles that are the same
for a particular trait 6. ________________ an allele that is
expressed, whether in...
Part I— Just Bad Luck? Brrrring! Brrrring! Jane checked the caller ID on her phone. “Sam! Great!” she thought. It was always nice to get a call from her older brother. But a little twinge of worry tugged at her. It was just a couple of weeks ago that he had mentioned making an appointment with his doctor about some abdominal pain he had been having. “Hi Sam! It’s great to hear from you,” Jane answered. “Hi Jane. Well I...