posted 12 years ago
please help! im trying to finish my program that calculates the mean and standard deviation of an array of integers entered by the user. I asked the user to enter an integer "n". then i stored that as the length of my array. Now i want the user to enter that many values in "X1  Xn" and store those in my array.....i can't exactly figure out whats going wrong...here is my code...
public static void main(String[] args)
{
int n;
double mean, sd;
Scanner scan = new Scanner(System.in);
System.out.println("Enter an Integer: ");
n=scan.nextInt();
int values[] = new int[n];
for(int count=values.length; count >= 0; count++)
{
System.out.println("Enter " + (count) + "values: ");
values[xi] = scan.nextInt();
}
for(int value : values)
{
double sum = 0.0;
sum = sum + x[i];
mean = sum/values.length;
System.out.println("The mean is " + (mean));
}
for(int value : values)
{
double val = 0.0;
val = Math.pow((x[i]  mean), 2) + val;
sd = Math.sqrt(val/n1);
System.out.println("The Standard deviation is " + (sd));
}
}
}
public static void main(String[] args)
{
int n;
double mean, sd;
Scanner scan = new Scanner(System.in);
System.out.println("Enter an Integer: ");
n=scan.nextInt();
int values[] = new int[n];
for(int count=values.length; count >= 0; count++)
{
System.out.println("Enter " + (count) + "values: ");
values[xi] = scan.nextInt();
}
for(int value : values)
{
double sum = 0.0;
sum = sum + x[i];
mean = sum/values.length;
System.out.println("The mean is " + (mean));
}
for(int value : values)
{
double val = 0.0;
val = Math.pow((x[i]  mean), 2) + val;
sd = Math.sqrt(val/n1);
System.out.println("The Standard deviation is " + (sd));
}
}
}
posted 12 years ago
This loop control line:
for(int count=values.length; count >= 0; count++)
says to start "count" out at values.length, and as long as "count" is greater than or equal to 0, keep going. After each time through the loop, but before doing that test, add one to "count". So "count" will be greater than or equal to zero... for a long time!
It's much more traditional to start at zero and go up:
for (int count=0; count < values.length; ++count)
But if you really want to count down, then you need to subtract, rather than add, to "count"
for (int count=values.length; count >= 0; count)
for(int count=values.length; count >= 0; count++)
says to start "count" out at values.length, and as long as "count" is greater than or equal to 0, keep going. After each time through the loop, but before doing that test, add one to "count". So "count" will be greater than or equal to zero... for a long time!
It's much more traditional to start at zero and go up:
for (int count=0; count < values.length; ++count)
But if you really want to count down, then you need to subtract, rather than add, to "count"
for (int count=values.length; count >= 0; count)
posted 12 years ago
Take a look at this line...
for(int count=values.length; count >= 0; count++)...
You're setting count to the length of the array, and then saying "as long as count is greater than or equal to zero, keep increasing it." Looks like an infinite loop to me. (Well, technically not, since eventually the int would reach its upper limit and "wrap" to a negative value, but you get the idea.)
The typical way to do this is to switch "values.length" and "0"...
for(int count = 0; count < values.length; count++) ...
[ January 29, 2006: Message edited by: marc weber ]
for(int count=values.length; count >= 0; count++)...
You're setting count to the length of the array, and then saying "as long as count is greater than or equal to zero, keep increasing it." Looks like an infinite loop to me. (Well, technically not, since eventually the int would reach its upper limit and "wrap" to a negative value, but you get the idea.)
The typical way to do this is to switch "values.length" and "0"...
for(int count = 0; count < values.length; count++) ...
[ January 29, 2006: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
posted 12 years ago
Your first problem begins in this for loop
The format of a for loop is for(initialization;condition;update). So in your for loop you initilaize count to be the length of your array, check to see if count >= 0, and then increment the count variable. This loop will never terminate. You'll have to fix that.
In the body of the for loop you have a statement: The problem is (as the compiler will surely inform you) is that the compiler has no idea what the variable xi is. You haven't declared it anywhere. My suggestion... throw it out. Once you correct your for loop, you'll already have a variable that you can use in its place. There are other problems further down the line but I would start here.
Garrett
The format of a for loop is for(initialization;condition;update). So in your for loop you initilaize count to be the length of your array, check to see if count >= 0, and then increment the count variable. This loop will never terminate. You'll have to fix that.
In the body of the for loop you have a statement: The problem is (as the compiler will surely inform you) is that the compiler has no idea what the variable xi is. You haven't declared it anywhere. My suggestion... throw it out. Once you correct your for loop, you'll already have a variable that you can use in its place. There are other problems further down the line but I would start here.
Garrett
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.  Laurence J. Peter
alaina peeler
Greenhorn
Posts: 29
posted 12 years ago
ok this is my updated program...
import java.text.NumberFormat;
import java.util.Scanner;
public class Calculations
{
/** Creates a new instance of Calculations*/
public static void main(String[] args)
{
int n;
double mean, sd;
Scanner scan = new Scanner(System.in);
System.out.println("Enter an Integer: ");
n=scan.nextInt();
int values[] = new int[n];
for(int count = 0; count < values.length; count++)
{
System.out.println("Enter " + (count) + " values: ");
values[count] = scan.nextInt();
}
for(int count : values)
{
double sum = 0.0;
double val = 0.0;
sum = sum + values[count];
mean = sum/values.length;
val = Math.pow((values[count]  mean), 2) + val;
sd = Math.sqrt(val/n1);
}
System.out.println("The mean is " + (mean));
System.out.println("The standard deviation is " + (sd));
import java.text.NumberFormat;
import java.util.Scanner;
public class Calculations
{
/** Creates a new instance of Calculations*/
public static void main(String[] args)
{
int n;
double mean, sd;
Scanner scan = new Scanner(System.in);
System.out.println("Enter an Integer: ");
n=scan.nextInt();
int values[] = new int[n];
for(int count = 0; count < values.length; count++)
{
System.out.println("Enter " + (count) + " values: ");
values[count] = scan.nextInt();
}
for(int count : values)
{
double sum = 0.0;
double val = 0.0;
sum = sum + values[count];
mean = sum/values.length;
val = Math.pow((values[count]  mean), 2) + val;
sd = Math.sqrt(val/n1);
}
System.out.println("The mean is " + (mean));
System.out.println("The standard deviation is " + (sd));
alaina peeler
Greenhorn
Posts: 29
Garrett Rowe
Ranch Hand
Posts: 1296
posted 12 years ago
Each time you come back to the beginning of the for loop here, you reinitilaize sum and val to 0.0 . You can't get the sum of all values that way. You need to rethink how you are impelmenting this loop.
Each time you come back to the beginning of the for loop here, you reinitilaize sum and val to 0.0 . You can't get the sum of all values that way. You need to rethink how you are impelmenting this loop.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.  Laurence J. Peter
Layne Lund
Ranch Hand
Posts: 3061
alaina peeler
Greenhorn
Posts: 29
Garrett Rowe
Ranch Hand
Posts: 1296
posted 12 years ago
If you initialize the sum variable outside the loop and inside the loop you add each value in the array to it, when the loop terminates the sum variable will be the sum of all values in the array. Then you can use that sum to calulate the mean, and then you can use the mean to calulate the standard deviation. Don't get caught up on trying to do all the work inside the loop.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.  Laurence J. Peter
posted 12 years ago
If you're doing this commercially (not in school), check out theJakata Commons statistics classes.
Layne Lund
Ranch Hand
Posts: 3061
posted 12 years ago
Perhaps it might help if we back away from the Java syntax a little bit and describe the steps in English. First of all, it seems like there are 3 different tasks you need to accomplish:
When you translate this into Java, you could put each calculation in its own method. But for now, let's just pretend we will do it in a single method and elaborate on each step a little further. So how do we "sum up the elements in the array". Well, we can flesh this out like this
Notice that I have replaced the first line with some details that are a little closer to the way we actually do it in Java. Similarly, we can elaborate on the last two steps:
Notice that this time I used symbols that look a little more like Java than English. This is because I found it more convenient and expressive. I also ignored a few details like what the value of n is. If I were writing this out for myself, I would know that I mean that n stands for the number of elements in the array.
Using socalled "pseudocode" in this manner to describe the steps to solve a problem is often useful. One advantage is that you can concetrate on how to solve the problem and you don't have to worry about syntax details. Notice how on each iteration, I added more details so that it is now to the point that I can translate each line of "pseudocode" into a line of actual Java code. Also notice how I split up the three tasks to start with. This lead me to eventually use two different for loops since I need to know the mean before I can calculate the standard deviation. The lesson is that you shouldn't try to do too many things at once.
If you have learned about methods, it would be a good exercise to write three different methods to do these calculations. The calculateMean() method could use the calculateSum() method and the calcualteStdDev() method could use the calculateMean() method to avoid repitition of code.
I hope this helps illustrate one approach to writing code to solve a given problem. There are many other approaches, but one thing they have in common is that you rarely just jump into writing code at the beginning.
Layne
When you translate this into Java, you could put each calculation in its own method. But for now, let's just pretend we will do it in a single method and elaborate on each step a little further. So how do we "sum up the elements in the array". Well, we can flesh this out like this
Notice that I have replaced the first line with some details that are a little closer to the way we actually do it in Java. Similarly, we can elaborate on the last two steps:
Notice that this time I used symbols that look a little more like Java than English. This is because I found it more convenient and expressive. I also ignored a few details like what the value of n is. If I were writing this out for myself, I would know that I mean that n stands for the number of elements in the array.
Using socalled "pseudocode" in this manner to describe the steps to solve a problem is often useful. One advantage is that you can concetrate on how to solve the problem and you don't have to worry about syntax details. Notice how on each iteration, I added more details so that it is now to the point that I can translate each line of "pseudocode" into a line of actual Java code. Also notice how I split up the three tasks to start with. This lead me to eventually use two different for loops since I need to know the mean before I can calculate the standard deviation. The lesson is that you shouldn't try to do too many things at once.
If you have learned about methods, it would be a good exercise to write three different methods to do these calculations. The calculateMean() method could use the calculateSum() method and the calcualteStdDev() method could use the calculateMean() method to avoid repitition of code.
I hope this helps illustrate one approach to writing code to solve a given problem. There are many other approaches, but one thing they have in common is that you rarely just jump into writing code at the beginning.
Layne
Enjoy the full beauty of the english language. Embedded in this tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
