# Calculate the mean and standard deviation in a text file of doubles.

Page 1 of 1

## 9 Replies - 313 Views - Last Post: 06 December 2017 - 04:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=408063&amp;s=5bc9c6e4402a23515be48063cf6ea298&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 forumer444

Reputation: 1
• Posts: 35
• Joined: 19-September 17

# Calculate the mean and standard deviation in a text file of doubles.

Posted 06 December 2017 - 11:01 AM

Hi guys, so I need help calculating the mean and standard deviation of a text file in my program. For the mean, I'm sure I'm supposed to use a for loop somewhere, but not sure where, but for the standard deviation, that's what confuses me the most. I've already done the maximum and minimum. Any tips would be great since we only have two days to do it and it's due tomorrow.

Here's what I have so far:

```/*
* Create a Java program to read a file of floating point numbers and compute
* the following statistics from the data file:
*
* 1. Maximum
* 2. Minimum
* 3. Arithmetic Average (Mean)
* 4. Standard Deviation
*
* Do not assume anything about the large numbers in the file. They could be
* positive or negative, and their magnitude could be extremely large or
* extremely small.
*/

import java.io.*;
import java.util.Scanner;

public class DataFile {

public static void main(String[] args) {
double n;
double max;
double min;
// double mean;
// double standardDeviation;

Scanner fileStream = null;

try {
fileStream = new Scanner(new File("RawData.txt"));

max = fileStream.nextDouble();
min = max; // same for the first number

while(fileStream.hasNextDouble()) {
n = fileStream.nextDouble();

if(n > max) {
max = n;
}

else if(n < min) {
min = n;
}

}
fileStream.close();
System.out.println("Maximum-File-Number: " + max);
System.out.println("Minimum-File-Number: " + min);
// System.out.println("Average-File-Number: " + mean);
// System.out.println("Standard-Deviation : " + standardDeviation);

}

catch(FileNotFoundException e) {
System.out.print("Error; Program Terminated!");
System.exit(1);

}

}

}

```

Is This A Good Question/Topic? 0

## Replies To: Calculate the mean and standard deviation in a text file of doubles.

### #2 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,480
• Joined: 12-December 12

## Re: Calculate the mean and standard deviation in a text file of doubles.

Posted 06 December 2017 - 11:47 AM

Presumably you have some details or formulas that tell you how to work out the mean and standard deviation? A reference book or internet resource? Post a link or describe how you would work these out.

### #3 forumer444

Reputation: 1
• Posts: 35
• Joined: 19-September 17

## Re: Calculate the mean and standard deviation in a text file of doubles.

Posted 06 December 2017 - 12:04 PM

This is all the information I have.

*** Well-Known Formulas for Mean and Standard Deviation ***
Let 𝑥1, 𝑥2, 𝑥3, ... 𝑥𝑛 be n numbers in the data file. Represent their sum by (∑ from 𝑖=1 to 𝑖=𝑛 of 𝑥𝑖 (sigma notation)). Then the
average or arithmetic mean, m, of the n numbers is their sum divided by n: 𝑚 = (∑ from 𝑖=1 to 𝑖=𝑛 of 𝑥𝑖) / n.

To compute the standard deviation, first compute the square of the standard deviation, 𝑠2, known as the variance. The variance is computed as follows: 𝑠2 = ((∑ from 𝑖=1 to 𝑖=𝑛 of 𝑥^2(sub 𝑖)) / n) - m^2.

Note that you compute the sum of the squares of the data numbers, then divide the sum by n to obtain their
average. Next, subtract 𝑚2 from that quotient to obtain the variance, 𝑠2. Finally, the standard deviation, s, is the
square root of the variance: 𝑠 = √𝑠2.

Here's my code cleaned up a little bit:

```/*
* Create a Java program to read a file of floating point numbers and compute
* the following statistics from the data file:
*
* 1. Maximum
* 2. Minimum
* 3. Arithmetic Average (Mean)
* 4. Standard Deviation
*
* Do not assume anything about the large numbers in the file. They could be
* positive or negative, and their magnitude could be extremely large or
* extremely small.
*/

import java.io.*;
import java.util.Scanner;

public class DataFile {

public static void main(String[] args) {
// declare variables
double number;
double maximum;
double minimum;
double sum; 				// not used yet
double mean; 				// not used yet
double standardDeviation; 	// not used yet

Scanner fileStream = null;

/* -------------------------------------------------------------------------- */
try {
fileStream = new Scanner(new File("RawData.txt"));
}

catch(FileNotFoundException e) {
System.out.print("Error; The program was terminated!");
System.exit(1);
}

/* -------------------------------------------------------------------------- */
// maximum and minimum initialized
maximum = fileStream.nextDouble();
minimum = maximum;

while(fileStream.hasNextDouble()) {
number = fileStream.nextDouble();

if(number > maximum) {
maximum = number;
}

else if(number < minimum) {
minimum = number;
}

} // end while loop

fileStream.close();
System.out.println("Maximum-File-Number: " + maximum);
System.out.println("Minimum-File-Number: " + minimum);
System.out.println("Average-File-Number: ");
System.out.println(" Standard-Deviation: ");

/* -------------------------------------------------------------------------- */
} // end method main

} // end class DataFile

```

### #4 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,480
• Joined: 12-December 12

## Re: Calculate the mean and standard deviation in a text file of doubles.

Posted 06 December 2017 - 12:14 PM

You must be able to make an attempt at the mean. You are reading all the numbers, so you need to sum them as you do so, and count how many you are reading.

If you haven't already done so, then calculate the mean and standard deviation first using pen and paper (and a hand calculator).

### #5 forumer444

Reputation: 1
• Posts: 35
• Joined: 19-September 17

## Re: Calculate the mean and standard deviation in a text file of doubles.

Posted 06 December 2017 - 12:43 PM

Yeah, I'm trying to right now, but should the while loop calculating the maximum and minimum be inside the for loop calculating the mean? Or should they be two separate loops? Does it matter?

This post has been edited by andrewsw: 06 December 2017 - 12:53 PM
Reason for edit:: Removed previous quote, just press REPLY

### #6 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,480
• Joined: 12-December 12

## Re: Calculate the mean and standard deviation in a text file of doubles.

Posted 06 December 2017 - 12:53 PM

You are looping through all the numbers from the file. While you are doing this, calculate whatever you need to so that, when the looping is complete, you will be able to determine the statistics that you need. Basically, you only need to loop through the file-values once.

### #7 forumer444

Reputation: 1
• Posts: 35
• Joined: 19-September 17

## Re: Calculate the mean and standard deviation in a text file of doubles.

Posted 06 December 2017 - 02:40 PM

Ok, this is what I have for the mean so far and it works but there's a small problem... It counts how many floating-point-numbers there are in a file and adds there sum, but it's skipping the first line in the file. It still adds it to the count, but isn't adding it to the sum. I assume it has to do something with the nextDouble() that I assign to number, but I don't have any idea what to put there...

```/*
* Create a Java program to read a file of floating point numbers and compute
* the following statistics from the data file:
*
* 1. Maximum
* 2. Minimum
* 3. Arithmetic Average (Mean)
* 4. Standard Deviation
*
* Do not assume anything about the large numbers in the file. They could be
* positive or negative, and their magnitude could be extremely large or
* extremely small.
*/

import java.io.*;
import java.util.Scanner;

public class Test {

public static void main(String[] args) {
// declare variables
double number;
double maximum;
double minimum;
int count;
double sum;
double mean;
double standardDeviation; // not used

Scanner file = null;

/* -------------------------------------------------------------------------- */
try {
file = new Scanner(new File("SampleData.txt"));
}

catch(FileNotFoundException e) {
System.out.print("Error; The program was terminated!");
System.exit(1);
}

/* -------------------------------------------------------------------------- */
// initialize variables
maximum = file.nextDouble();
minimum = maximum;
sum = 0;
count = 1;

while(file.hasNextDouble()) {
number = file.nextDouble();

if(number > maximum) {
maximum = number;
}

else if(number < minimum) {
minimum = number;
}

sum += number;
count += 1;
System.out.print(number + " ");

} // end while loop

file.close();

/* -------------------------------------------------------------------------- */
// mean calculation
mean = sum / count;

// standard deviation calculation
// .....

// display statistics
System.out.println();
System.out.println("Maximum-File-Number: " + maximum);
System.out.println("Minimum-File-Number: " + minimum);
System.out.println("Sum: " + sum);
System.out.println("Count: " + count);
System.out.println("Mean: " + mean);

} // end method main

} // end class DataFile

```

### #8 g00se

• D.I.C Lover

Reputation: 3536
• Posts: 16,027
• Joined: 20-September 08

## Re: Calculate the mean and standard deviation in a text file of doubles.

Posted 06 December 2017 - 02:50 PM

Quote

```maximum = file.nextDouble();
```
consumes the first value. It doesn't get added to the sum. Initialize it to Double.MIN_VALUE instead

This post has been edited by g00se: 06 December 2017 - 02:50 PM
Reason for edit:: Clarification

### #9 forumer444

Reputation: 1
• Posts: 35
• Joined: 19-September 17

## Re: Calculate the mean and standard deviation in a text file of doubles.

Posted 06 December 2017 - 03:35 PM

Ok that seems to work, thanks.

I just have one last thing. I need to calculate the standard deviation, but I don't even understand what that is. I tried looking it up in several places but can't seem to understand it. Could someone give me an easy example for my dumb self to comprehend... ?

### #10 g00se

• D.I.C Lover

Reputation: 3536
• Posts: 16,027
• Joined: 20-September 08

## Re: Calculate the mean and standard deviation in a text file of doubles.

Posted 06 December 2017 - 04:38 PM

http://www.mathsisfu...-deviation.html should be simple enough. You might need to store your numbers first or read the file twice