In: Computer Science
Below, the prompts for the rings’ distances are unclear in terms of which ring is being examined. Rewrite your ring-distance prompt code so that users know which ring the prompt is for. The first two ring-distance prompts should look like this:
Enter ring #1’s distance from the center in cm:
Enter ring #2’s distance from the center in cm
Write a pseudocode algorithm that analyzes a tree’s annual growth rings. Your algorithm should initially ask the user for the number of annual growth rings that are to be entered. Use a counter loop to ensure that the loop repeats that number of times. Inside the loop, your algorithm should ask the user to enter the distance in centimeters from the tree’s center for each ring, with the smaller ring values entered first. Also inside the loop, your algorithm should calculate the amount of growth that took place for the year associated with the current ring. Below the loop, print the growth for the year where the greatest growth took place.
Thanks for the question.
Here is the Pseudo code for this problem. Comments are included so
that you understand whats going on. Let me know if you have any
doubts or if you need anything to change.
Thank You !!
========================================================================================
Pseudo Code
// ask user how many rings the tree has
// store it in variable ring_count
ring_count=input('Enter the number of annual growth rings: ')
// this variable will store the largest growth set it to 0
max_growth=0
// this variable will store the year with the largest growth set
it to year 1
year_with_largest_growth=1
// will store the previous year growth so that we can find the
difference
// between two successive years
distance_from_center=0
// for loop that runs for the number of rings the tree has
for (i=1; i<=ring_count; i++) {
// ask user to enter the distance for the current year
distance=input('Enter ring #{}\'s distance from the center in cm:
',i)
// measure the growth by subtracting previous year distance from
current year distance
grown_this_year=distance-distance_from_center
// store the current year distance to the variable
distance_from_center=distance
// prints the growth for the current year
print('This year the tree has grown {} cm',grown_this_year)
// compare the current year growth with the max growth
// if the current year growth exceeds all time
// then set the current year growth as max growth and year
if grown_this_year>max_growth:
max_growth=grown_this_year
year_with_largest_growth=i
}
// at the end print the max growth and the growth amount
print('The tree has grown maximum on {}th year by {}
cms',year_with_largest_growth,max_growth)
=========================================================================================
Python Code to demonstrate the algoirthm works correct.
ring_count=int(input('Enter the number of annual growth rings: ')) max_growth=0 year_with_largest_growth=1 distance_from_center=0 for i in range(1,ring_count+1): distance=float(input('Enter ring #{}\'s distance from the center in cm: '.format(i))) grown_this_year=distance-distance_from_center distance_from_center=distance print('This year the tree has grown {} cm'.format(grown_this_year)) if grown_this_year>max_growth: max_growth=grown_this_year year_with_largest_growth=i print('The tree has grown maximum on {}th year by {} cms'.format(year_with_largest_growth,max_growth))
========================================================================================