Answered step by step
Verified Expert Solution
Link Copied!

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 10 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 43.1
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 calculateAverageWeight(double[] weights){
// Initialize variables for maximum and minimum values
double max = weights[0];
double min = weights[0];
double sum = weights[0];
// Find maximum and minimum values
for (int i =1; i < weights.length; i++){
if (weights[i]> max){
max = weights[i];
}
if (weights[i]< min){
min = weights[i];
}
// Calculate sum of all weights
sum += weights[i];
}
// Subtract maximum and minimum values from sum
sum -=(max + min);
// Calculate average (excluding max and min)
double average = sum /(weights.length -2);
return average;
}
public static void main(String[] args){
// Example data: weights recorded for 10 days
double[] weights ={43.1,42.5,45.3,41.8,44.6,46.2,43.7,42.0,44.9,45.7};
// Calculate and print the average weight (excluding max and min)
double averageWeight = calculateAverageWeight(weights);
System.out.println("Average 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

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

OpenStack Trove

Authors: Amrith Kumar, Douglas Shelley

1st Edition

1484212215, 9781484212219

More Books

Students also viewed these Databases questions

Question

=+a) Fit a regression model with just Year as the predictor.

Answered: 1 week ago