Sunday, October 30, 2011 Posted In CS and IT Edit This
Question:
Write a program that computes the average and standard deviation of
the text floating point numbers in a file. Use the following formulas
for the average and the standard deviation of N values. The formulas
compute the sum, the sum of squares, the average, the average square,
the variance, and finally, the standard deviation.
sum = x1 + x2 + x3 + ... + xN-1 + xN
sumSQ = x12 + x22 + x32 + ... + xN-12 + xN2
avg = sum/N
avgSQ = sumSQ/N
var = avgSQ - avg2
sd = var(1/2)
The input file will contain any number text floating point numbers,
similar to the following:
10.5
12.9
9.67
12.05
8.23
10.08
10.23
7.7
10.4
11.34
Numbers could be several, or none per line, and negative numbers are
perfectly OK.
Solution:
Input Text:
10.0
20.0
30.0
40.0
50.0
............
SAVE THIS FILE IN .JAVA
import java.io.*;
import java.util.*;
import javax.swing.*;
public class assignment1{
public static void main( String args[] ){
try{
FileReader fr = null;
BufferedReader br = null;
fr = new FileReader ("input.txt");
br = new BufferedReader (fr);
String line = br.readLine();
double sum =0 ;
while (line != null) {
double num = Double.parseDouble(line);
sum += num;
line = br.readLine();
}
JOptionPane.showMessageDialog(null, "Sum = " + sum);
br.close();
fr.close();
}
catch(NumberFormatException nfEx) {
System.out.println(nfEx);
}
catch(FileNotFoundException fnfEx){
System.out.println(fnfEx);
}
catch(IOException ioEx){
System.out.println(ioEx);
}
}
}
--
Please visit http://www.weblyceum.com and get registered for Past Papers, Quiz, Assignments, GDBs and much more...
To post to this group, send email to vu-experts@googlegroups.com
To unsubscribe from this group, send email to vu-experts-unsubscribe@googlegroups.com
Do write to admin.bilal@weblyceum.com for Help, suggestion and Complaint.
No comments:
Post a Comment