Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You are an intern at a local animal sanctuary. One of your tasks is to record how many pounds of bamboo the pandas consume daily
You are an intern at a local animal sanctuary. One of your tasks is to record how many pounds of bamboo the pandas consume daily for days. Write a program to calculate and return the average weight based on the data.
Before calculating the average, you decide to throw out the lowest and highest values.
Your method should do the following:
declare and initialize the array with the recorded weights such as
determine the maximum and minimum values in the array
compute the average of the array contents, excluding the maximum and minimum values array methods may not be used
calculate and return the average, excluding the maximum and minimum values
So far, the work I have is as follows.
public static double calculateAverageWeightdouble weights
Initialize variables for maximum and minimum values
double max weights;
double min weights;
double sum weights;
Find maximum and minimum values
for int i ; i weights.length; i
if weightsi max
max weightsi;
if weightsi min
min weightsi;
Calculate sum of all weights
sum weightsi;
Subtract maximum and minimum values from sum
sum max min;
Calculate average excluding max and min
double average sum weightslength ;
return average;
public static void mainString args
Example data: weights recorded for days
double weights ;
Calculate and print the average weight excluding max and min
double averageWeight calculateAverageWeightweights;
System.out.printlnAverage weight of bamboo consumed daily excluding highest and lowest values: averageWeight;
I have been told I "Should not reset max and min inside for loop every iteration". Can you assist me with this?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started