This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes simple array loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple array loop" Watch "simple array loop" New topic
Author

simple array loop

alaina peeler
Greenhorn

Joined: Dec 04, 2005
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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)


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
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 ]

"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
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
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


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
If i understood you correctly you wish to do something like this.



Stephen Foy - Microsoft Application Development Consultant
alaina peeler
Greenhorn

Joined: Dec 04, 2005
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

Joined: Dec 06, 2001
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


Java API Documentation
The Java Tutorial
alaina peeler
Greenhorn

Joined: Dec 04, 2005
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

Joined: Jan 17, 2006
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

Joined: Dec 06, 2001
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

Joined: Dec 04, 2005
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

Joined: Jan 17, 2006
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.
Adam Richards
Ranch Hand

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

Joined: Dec 06, 2001
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple array loop
 
Similar Threads
Counting in Array List/ Standard deviation
simple array object
How would i break this down into seperate metods?
ArrayIndexOutOfBounds help!
standard deviation