simple array loop

alaina peeler
Greenhorn
Posts: 29
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/n-1);
System.out.println("The Standard deviation is " + (sd));

}

}
}

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
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)

marc weber
Sheriff
Posts: 11343
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 ]

Garrett Rowe
Ranch Hand
Posts: 1296
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

Stephen Foy
Ranch Hand
Posts: 143
If i understood you correctly you wish to do something like this.

alaina peeler
Greenhorn
Posts: 29
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/n-1);
}

System.out.println("The mean is " + (mean));
System.out.println("The standard deviation is " + (sd));

Layne Lund
Ranch Hand
Posts: 3061
So how does it work? Have you fixed all your problems? If not what happens when you compile and run this? The more specific your questions, the better we will be able to help you out.

Layne

alaina peeler
Greenhorn
Posts: 29
no errors except in the end when i try to print the mean and sd. It says they may have not been initialized...but i initialized them within the for loop that does the math. If i put those println statements within the for loop it works but it prints it repeatidly.

Garrett Rowe
Ranch Hand
Posts: 1296

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.

Layne Lund
Ranch Hand
Posts: 3061
Do you really need to calculate the mean and standard deviation EACH time through the for loop? What is the purpose of the for loop? Think about this and the answers might help you understand how to fix your program.

Layne

alaina peeler
Greenhorn
Posts: 29
i might not need to process it with a for loop each time..but what else would i use ...an if statement?? i have just learned about arrays and how to proccess each element within them...and the for loops is what i have learned...and how else would i initialize sum and val?

Garrett Rowe
Ranch Hand
Posts: 1296
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.

Ranch Hand
Posts: 135
If you're doing this commercially (not in school), check out theJakata Commons statistics classes.

Layne Lund
Ranch Hand
Posts: 3061
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 so-called "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