Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
APComputer Science Lab Help?
Here is the prompt for a classwork lab assignment, I've tried this lab twice, but I keep generating the wrong answer. My code is error-free, but for some reason the standard deviation I result with does not comply with the actual standard deviation the assignment offers. (I'm lucky to be able to try out my code to see if it matches the answer key)
The standard deviation of a set of numbers is a measure of the spread of their values. It is defined as the square root of the average of the squared differences between each number and the mean. To calculate the standard deviation of the numbers stored in data:
Calculate the mean of the numbers.
For each number, subtract it from the mean and square the result.
Find the mean of the numbers calculated in step 2.
Find the square root of the result of step 3. This is the standard deviation.
Write code to calculate the standard deviation of the numbers in data and store the result in the double sd.
To find the square root of a non-negative double d, use the expression
double s = Math.sqrt( d );
// Enter values to test here
double[] data = { };
double sd;
Here is my code..
int i = 0;
double sum1 = 0;
double sum2 = 0;
double mean;
double t = 0;
while ( i < data.length )
{
sum1+= data[i];
i++;
}
mean = (sum/data.length);
while ( i < data.length )
{
t += mean - data[i];
t = Math.sqrt(data[i]);
i--;
sum2 +=
}
mean = (sum/data.length);
sd = Math.sqrt (mean);
___________________
It would very helpful If I could get some tips on how to fix this..
1 Answer
- RichardLv 51 decade agoFavorite Answer
Hi, at first glance you should probable reset i to zero before the second while and use i++
also what is sum2 += ?
and in mean = ... shouldn't you have sum1 instead of sum