java, counting Standard Deviation. stuck in my last code - java

public int deviasi(){
//sum
int jumlah=0;
for (int i=0; i<banyak; i++){
jumlah = jumlah+nilai[i];
}
//mean
int rata2;
rata2=jumlah/banyak;
//menghitung deviasi
double deviasi = 0;
for (int i=0;i<banyak;i++){
deviasi += Math.pow(nilai[i] - rata2,2);
}
return
Math.sqrt (deviasi/banyak);
}
i got error in my last code. it told me the problem because of different data type between deviasi and banyak. but, i've changed the data type to make them have the same data type. but the error warning still told me the problem because of different data type between deviasi and banyak. i got stuck. T_T

Like mentioned in comments you need to work with double as variable type
public class StandDev {
public double standardDevioation(int[] numbers){
int count = numbers.length;
double sum = 0.0;
for (int i = 0; i < count; i++){
sum += numbers[i];
}
double mean = sum / count;
double sumDiff = 0.0;
for (int i = 0; i< count; i++){
sumDiff += Math.pow(numbers[i] - mean,2);
}
return Math.sqrt (sumDiff/count);
}
public static void main(String[] args) {
int[] numbers = {-5, 1, 8, 7, 2};
StandDev standDev = new StandDev();
System.out.println(standDev.standardDevioation(numbers));
}
}

// Code returns 3.7549966711037173
public class Devs {
public static double deviasi(){
//sum
int jumlah= 0;
int[] nilai = {4,7,6,3,9,12,11,12,12,15};
int banyak = 10;
for (int i=0; i<banyak; i++){
jumlah += nilai[i];
}
//mean
int rata2;
rata2=jumlah/banyak;
//menghitung deviasi
double deviasi = 0;
for (int i=0;i<banyak;i++){
deviasi += Math.pow(nilai[i] - rata2,2);
}
return Math.sqrt (deviasi/banyak);
}
public static void main(String args[]) {
System.out.println(deviasi());
}
}

Related

Program ignoring the last row in the file in calculation

I have a text file with data that looks like this (TestData.txt):
Name|Test1|Test2|Test3|Test4|Test5|Test6|Test7|Test8|Test9|Test10
John Smith|82|89|90|78|89|96|75|88|90|96
Jane Doe|90|92|93|90|89|84|97|91|87|91
Joseph Cruz|68|74|78|81|79|86|80|81|82|87
Suzanne Nguyen|79|83|85|89|81|79|86|92|87|88
Richard Perez|100|84|73|81|92|84|95|96|95|100
Ivan Dyer|77|91|90|75|97|94|76|89|90|92
Craig Palmer|91|84|98|89|82|75|78|96|100|97
Madeline Rogers|75|79|78|93|91|76|80|88|100|81
Chelsea Roxas|87|94|89|96|95|85|88|92|86|86
Jasper Bautista|100|83|93|100|98|97|96|97|97|98
Tyler Perez|82|89|90|78|89|96|75|88|90|96
My code parses the file and does some calculations with it.
However, in the method arrangeList() within which calls another method called getTestAvg() (calculates column means), the program ignores Tyler Perez's scores.
I noticed that the results I am getting were inaccurate so I went and printed the whole 2d array with all the test scores and the last column is nowhere to be found.
My entire code is below and I hope someone could point out what causes this.
I keep getting an IndexOutOfBounds error whenever I try to switch N (# of students) and M (# of tests) to see what happens. At first, I have 10 students and 10 tests, and all the calculations were correct, but when I added another student, the calculations became inaccurate.
I apologize in advance if my code isn't as well-designed as I'm not an experienced programmer.
import java.util.*;
import java.io.*;
public class TestAverages
{
private static int[] grades;
private static int[] testTotal;
private static int N;
private static double classTotal;
private static int M;
public static void main(String[] args) throws FileNotFoundException
{
File input = new File("TestData.txt");
Scanner in = new Scanner(input);
parseFile(in);
}
public static void parseFile(Scanner in) throws FileNotFoundException
{
TestAverages t = new TestAverages();
in.nextLine();
double total = 0.0;
ArrayList<Double> testScores = new ArrayList<Double>();
int index = 0;
while(in.hasNextLine())
{
String line = in.nextLine();
String[] data = line.split("\\|");
String name = data[0];
grades = new int[data.length - 1];
N = grades.length;
for(int i = 0; i < N; i++){
grades[i] = Integer.parseInt(data[i + 1]);
testScores.add((double)grades[i]);
}
System.out.println(name + "\t");
System.out.println("Student Average: " + t.getStudentAvg(grades) + "%\n");
total += t.getStudentAvg(grades);
M++;
}
t.arrangeList(testScores);
System.out.printf("\nClass Average: %.1f%%\n", t.getClassAvg(total));
}
public double getStudentAvg(int[] grades)
{
double total = 0.0;
double avg = 0.0;
int N = grades.length;
for(int i = 0; i < N; i++){
total += grades[i];}
avg = total / N;
return avg;
}
public double getClassAvg(double total)
{
double classAvg = total / M;
return classAvg;
}
public double[][] arrangeList(ArrayList testScores)
{
double[][] tests = new double[N][N];
int len = tests.length;
for(int i = 0; i < len; i++)
{
for(int j = 0; j < len; j++)
{
tests[i][j] = (Double) testScores.get(i*N + j);
}
}
for(int i = 0; i < len; i++)
{
double avg = getTestAvg(tests, i);
System.out.printf("\nTest " + (i + 1) + " Average: %.1f%%\n", avg);
}
return tests;
}
public double getTestAvg(double[][] testScores, int index)
{
double testAvg = 0.0;
for(int i = 0; i < N; i++)
{
testAvg += testScores[i][index];
}
return testAvg / N;
}
}
Here are the numbers I'm supposed to be getting (top) compared to what my program outputs (bottom).
As the other responses already stated, you had quite the issue with your variables and loops. I now changed N to # of students and M to # of tests to be as you stated in your question.
Next time, maybe try to improve your variable naming, so you don't get confused. (e.g. switch out n and m for s (students) and t (tests), if you like your variable names short).
This should work now. Just check against your code to see the changes.
import java.util.*;
import java.io.*;
public class TestAverages {
private static int[] grades;
private static int n = 0; // amount of students
private static int m; // amount of tests
public static void main(String[] args) throws FileNotFoundException {
File input = new File("TestData.txt");
Scanner in = new Scanner(input);
parseFile(in);
}
public static void parseFile(Scanner in) throws FileNotFoundException {
TestAverages t = new TestAverages();
in.nextLine();
double total = 0.0;
ArrayList<Double> testScores = new ArrayList<Double>();
while (in.hasNextLine()) {
String line = in.nextLine();
String[] data = line.split("\\|");
String name = data[0];
grades = new int[data.length - 1];
m = grades.length;
for (int i = 0; i < m; i++) {
grades[i] = Integer.parseInt(data[i + 1]);
testScores.add((double) grades[i]);
}
System.out.println(name + "\t");
System.out.println("Student Average: " + t.getStudentAvg(grades) + "%\n");
total += t.getStudentAvg(grades);
n++;
}
t.arrangeList(testScores);
System.out.printf("\nClass Average: %.1f%%\n", t.getClassAvg(total));
}
public double getStudentAvg(int[] grades) {
double total = 0.0;
double avg = 0.0;
for (int i = 0; i < grades.length; i++) {
total += grades[i];
}
avg = total / grades.length;
return avg;
}
public double getClassAvg(double total) {
double classAvg = total / n;
return classAvg;
}
public double[][] arrangeList(ArrayList<Double> testScores) {
double[][] tests = new double[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
tests[i][j] = (Double) testScores.get(i * m + j);
}
}
for (int i = 0; i < m; i++) {
double avg = getTestAvg(tests, i);
System.out.printf("\nTest " + (i + 1) + " Average: %.1f%%\n", avg);
}
return tests;
}
public double getTestAvg(double[][] testScores, int index) {
double testAvg = 0.0;
for (int i = 0; i < n; i++) {
testAvg += testScores[i][index];
}
return testAvg / n;
}
}
You need to account for the different sizes. I think you want primarily the number of TESTS (not students), but you can't just use len for both index bounds.
double[][] tests = new double[N][M];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
tests[i][j] = (Double) testScores.get(i*N + j);
}
}
Note that it didn't just resize the array, but it changed the loop conditions to loop the proper amount for inner and outer loop.
In the line
double[][] tests = new double[N][N];
of function arrangeList
you make your test array as N X N.
I believe youh should do something like
double[][] tests = new double[M][N];
It's just a suggestion as in your code it seems M = number of students and N = number of tests, differently from what you write in your question.
In general you should review all the method arrangeList and probably getTestAvg too (loop on N, instead of M), as the loops on variable len are intended for a N X N array, which is not the case.

Simple task for beginner (array with return) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Sorry for my english not the best yet.
I made a simple task i'm just learning the java yet, my question is how I can do it this task better?
The task is, made an array with not whole numbers, put these numbers to the array, and after this write it minimum, maximum, and average, with return method. I did it but I don't like it what a did.
How can I do it this task better? Or Where the return method have the for cycle? Or its impossible?
My code is:
public class program {
public static class Main {
public static void main(String[] args) {
// write your code here
double[] tömb;
int max = 10;
tömb = new double[max];
for (int i = 0; i < max; i++) {
tömb[i] = i * 1.65223;
}
//-------------------------------------------------------------------------------------------------------------------
for (int i = 0; i < max; i++) {
konvertáló(i);
System.out.println("to whole number "+konvertáló(i));
}
//--------------------------------------------------------------------------------------------------------------------
double minimum=tömb[0];
for (int i = 1; i < max; i++) {
if(minimum(i)< minimum){
minimum=minimum(i);
}
}
System.out.println("minimum value "+minimum);
//-------------------------------------------------------------------------------------------------------------------
double avg=0;
for (int i = 0; i <max ; i++) {
avg+=avg(i);
}
System.out.println("az average value "+avg/max);
double maximum=tömb[0];
for (int i = 0; i <max ; i++) {
if(maximum <maximum(i)){
maximum=maximum(i);
}
}
System.out.println("maximum value "+maximum);
}
public static double konvertáló(double x) {
return Math.round(x);
}
public static double minimum(double y) {
return Math.round(y);
}
public static double avg(double y) {
return Math.round(y);
}
public static double maximum(double y) {
return Math.round(y);
}
}
}
Check this code
public static void main(String[] args) {
// write your code here
double[] tomb;
int max = 10;
tomb = new double[max];
for (int i = 0; i < max; i++) {
tomb[i] = i * 1.65223;
}
System.out.println("Minimum: " + getMin(tomb));
System.out.println("Maximum: " + getMax(tomb));
System.out.println("Average: " + getAverage(tomb));
}
private static double getMin(double[] tomb) {
double min = tomb[0];
for (double d:tomb) {
if (d < min) {
min = d;
}
}
return min;
}
private static double getMax(double[] tomb) {
double max = tomb[0];
for (double d:tomb) {
if (d > max) {
max = d;
}
}
return max;
}
private static double getAverage(double[] tomb) {
double avg = 0;
for (double d:tomb) {
avg+=d;
}
return avg/tomb.length;
}
do it in one method. and there is a lot of similar question on stack overflow, someone will mark your question as duplicate. any way. check next code, it will print min, max, and avr
public static void max_min_avr(int[] numbers) {
int maxValue = numbers[0];
int minValue = numbers[0];
int sum = 0;
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > maxValue)
maxValue = numbers[i];
if (numbers[i] < minValue)
minValue = numbers[i];
sum += numbers[i];
}
System.out.println("Max: " + maxValue);
System.out.println("Min: " + minValue);
System.out.println("AVR: " + sum + numbers[0] / numbers.length);
}
if you need return values. and do not want to write a lot in main. break code in methods.
public static void main(String[] args) {
System.out.println(min(new double[]{3.0,4.0,5.0,7.0,9.0}));
System.out.println(max(new double[]{3.0,4.0,5.0,7.0,9.0}));
System.out.println(avr(new double[]{3.0,4.0,5.0,7.0,9.0}));
}
public static double max(double[]numbers){
double maxValue = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > maxValue)
maxValue = numbers[i];
}
return maxValue;
}
public static double min(double[] numbers){
double minValue = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < minValue)
minValue = numbers[i];
}
return minValue;
}
public static double avr(double[] numbers){
double sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum / (double) numbers.length;
}
if you are familiar with stream. most short code would be next.
List<Double> d = Arrays.asList(3.0,4.0,5.0,7.0,9.0);
d.stream().max(Double::compare).ifPresent(System.out::println);
d.stream().min(Double::compare).ifPresent(System.out::println);
d.stream().mapToDouble(x -> x).average().ifPresent(System.out::println);
put it in main method and you will have min, max, and avr in 4 lines of code.

Use boolean to search for outliers of standard dev and then print the outliers using java

What I'm trying to do is figure out a way to print the outliers of standard deviation here. The outliers are defined as having a variance greater than 2x the standard deviation. I can't figure out how but I've started by creating a boolean flag, however I don't understand the dynamics of this. Could someone please help me figure out how to print out the outliers somehow? Thanks.
public class Main {
public static void main(String[] args)
{
{
Algebra n = new Algebra();
System.out.println(" ");
System.out.println("The maximum number is: " + n.max);
System.out.println("The minimum is: " + n.min);
System.out.println("The mean is: " + n.avg);
System.out.println("The standard deviation is " + n.stdev);
}
}
}
2nd part:
public class Algebra
{
static int[] n = createArray();
int max = displayMaximum(n);
int min = displayMinimum(n);
double avg = displayAverage(n);
double stdev = displayStdDev(n);
public boolean outliers() {
for(int i = 0; i < n.length; i++)
{
boolean flag = (n[i] < stdev*2);
}
return
}
public Algebra()
{
this(n);
System.out.println("The numbers that are outliers are ");
for(int i = 0; i < n.length; i++)
{
System.out.print(" " + (n[i] < stdev*2));
}
}
public Algebra(int[] n)
{
createArray();
}
public static int[] createArray()
{
int[] n = new int[100];
for(int i = 0; i < n.length; i++)
n[i] = (int)(Math.random()*100 + 1);
return n;
}
public int displayMaximum(int[] n)
{
int maxValue = n[0];
for(int i=1; i < n.length; i++){
if(n[i] > maxValue){
maxValue = n[i];
}
}
return maxValue;
}
public int displayMinimum(int[] n)
{
int minValue = n[0];
for(int i=1;i<n.length;i++){
if(n[i] < minValue){
minValue = n[i];
}
}
return minValue;
}
protected double displayAverage(int[] n)
{
int sum = 0;
double mean = 0;
for (int i = 0; i < n.length; i++) {
sum += n[i];
mean = sum / n.length;
}
return mean;
}
protected double displayStdDev(int[] n)
{
int sum = 0;
double mean = 0;
for (int i = 0; i < n.length; i++) {
sum = sum + n[i];
mean = sum/ n.length;
}
double squareSum = 0.0;
for (int i = 0; i < n.length; i++)
{
squareSum += Math.pow(n[i] - mean, 2);
}
return Math.sqrt((squareSum) / (n.length - 1));
}
}
Variance is defined as the squared difference from the mean. This is a fairly straight forward calculation.
public static double variance(double val, double mean) {
return Math.pow(val - mean, 2);
}
You define an outlier as an instance that has a variance greater than x2 the standard deviation.
public static boolean isOutlier(double val, double mean, double std) {
return variance(val, mean) > 2*std;
}
You then just need to iterate through the values and print any values that are evaluated as an outlier.
public void printOutliers() {
for (int i : n) {
if (isOutlier(i, avg, stdev)) {
...
}
}
}
You should note that if one value is defined as an outlier and subsequently removed, values previously classified as an outlier may no longer be. You may also be interested in the extent of an outlier in the current set; One value may be an outlier to a greater extent than another.

Incompatible types: double[][] can not be converted to double

I am trying to resolve an issue for this assignment I was given for Homework. I am currently stuck and would appreciate any help that could guide me in correcting the program.
The original assignment is as follows:
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low.
This is the code I have assembled so far and have an error that I am not able to resolve. It is " incompatible types: converting double[][] cannot be converted to double. The lines in question are Line 8, and Line 110 ( the last return in the program).
import java.util.*;
public class Weather
{
public static void main(String[] args)
{
double[][] tempData = getData();
printTempData(tempData);
double avgHigh = averageHigh(tempData);
double avgLow = averageLow(tempData);
int indexHigh = indexHighTemp(tempData);
int indexLow= indexLowTemp(tempData);
System.out.format("The average high temperature is %4.1f%n", avgHigh);
System.out.format("The average low temperature is %4.1f%n", avgLow);
System.out.format("The index of high temperature is %2d%n", indexHigh);
System.out.format("The index of low temperature is %2d%n", indexLow);
}
private static void printTempData(double[][] tempData)
{
System.out.format("%6s:%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%n","Month","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
System.out.format("%6s:","Low");
for (int i = 0; i < tempData[0].length;i++)
{
System.out.format("%4.1s", tempData[0][i]);
}
System.out.format("%n");
System.out.format("%6s: ","High");
for (int i = 0; i < tempData[1].length; i++)
{
System.out.format("%4.1f", tempData[1][i]);
}
System.out.format("%n");
}
private static int indexLowTemp(double[][] tempData)
{
int index = 0;
double temp = tempData[0][0];
for (int i = 0; i < tempData[0].length; i++)
{
if (temp > tempData[0][i])
{
temp = tempData[0][i];
index = i;
}
}
return index +1;
}
private static int indexHighTemp(double[][] tempData)
{
int index = 0;
double temp = tempData[1][0];
for(int i = 0; i< tempData[1].length; i++)
{
if ( temp < tempData[1][i])
{
temp = tempData[1][i];
index = i;
}
}
return index + 1;
}
private static double averageHigh(double[][] tempData)
{
double avg = 0.0;
for(int i=0; i < tempData[0].length; i++)
{
avg += tempData[0][i];
}
avg /= tempData[0].length;
return avg;
}
private static double averageLow(double[][] tempData)
{
double avg = 0.0;
for(int i=0; i > tempData[1].length; i++)
{
avg += tempData[0][i];
}
avg /= tempData[0].length;
return avg;
}
private static double getData()
{
double[][] tempData = new double[2][12];
Random r = new Random();
for (int j = 0; j < tempData[0].length; j++)
{
tempData[0][j] = 30 + Math.sqrt(j) - r.nextDouble();
tempData[1][j] = 30 + Math.sqrt(j) + r.nextDouble();
}
return tempData;
}
}
Your method private static double getData() should be private static double[][] getData()
You already declared an array
double[][] tempData = getData();
but you are trying to call
private static double getData()
thus the error "converting double[][] cannot be converted to double."
Hence change to
private static double[][] getData()
The method should be private static double[][] getData()

Standard deviation of an ArrayList [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This code is supposed to find the Standard deviation of an random integers in an ArrayList. However, my code for the Standard deviation doesn't show the right outcome. It shows another number then expected.
What am I doing wrong?
import java.io.*;
import java.util.*;
public class Assignment4 {
public static void main(String[] args)
{
ArrayList<Integer> values = new ArrayList<Integer>();
int count = 0;
int total = 0;
Random r = new Random();
for (int i = 1; i <= 10; i++) {
values.add(r.nextInt(90)+ 1);
System.out.println(values);
}
System.out.println(mean(values));
System.out.println(sd(values));
}
public static double mean (ArrayList<Integer> table)
{
int total = 0;
for ( int i= 0;i < table.size(); i++)
{
int currentNum = table.get(i);
total+= currentNum;
}
return total/table.size();
}
public static double sd (ArrayList<Integer> table)
{
double mean= mean(table);
double temp =0;
for ( int i= 0; i <table.size(); i++)
{
temp= Math.pow(i-mean, 2);
}
return Math.sqrt(mean( table));
}
public static void selectionSort(ArrayList<Integer> table)
{
int count = table.size();
for(int pos = 0; pos < count - 1; pos++)
{
int locMin = pos;
for(int i = pos + 1; i < count; i++)
{
if(table.get(i) < table.get(locMin))
locMin = i;
}
int temp = table.get(pos);
table.set(pos, table.get(locMin) );
table.set(locMin, temp);
}
}
}
Your standard deviation calculation has errors:
This is the algorithm for finding the standard deviation:
Step 1: Find the mean.
Step 2: For each data point, find the square of its distance to the mean.
Step 3: Sum the values from Step 2.
Step 4: Divide by the number of data points.
Step 5: Take the square root.
Therefore, your code should look like:
public static double sd (ArrayList<Integer> table)
{
// Step 1:
double mean = mean(table);
double temp = 0;
for (int i = 0; i < table.size(); i++)
{
int val = table.get(i);
// Step 2:
double squrDiffToMean = Math.pow(val - mean, 2);
// Step 3:
temp += squrDiffToMean;
}
// Step 4:
double meanOfDiffs = (double) temp / (double) (table.size());
// Step 5:
return Math.sqrt(meanOfDiffs);
}
Note: Your mean calculation has a loss of precision.
You have:
return total/table.size();
It should be:
return (double) total / (double) table.size();
Here is a sample Standard Deviation Program:
// Beginning of class Deviation
public class Deviation
{
// Beginning of method main
public static void main(String[] args)
{
// Declare and create an array for 10 numbers
double[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Print numbers
printArray(numbers);
// Display mean and deviation
System.out.println("The mean is " + findMean(numbers));
System.out.println("The standard deviation is " +
findDeviation(numbers));
} // End of main
/* Method for computing deviation of double values */
// Beginning of double findDeviation(double[])
public static double findDeviation(double[] nums)
{
double mean = findMean(nums);
double squareSum = 0;
for (int i = 0; i < nums.length; i++)
{
squareSum += Math.pow(nums[i] - mean, 2);
}
return Math.sqrt((squareSum) / (nums.length - 1));
} // End of double findDeviation(double[])
/* Method for computing deviation of int values */
// Beginning of double findDeviation(int[])
public static double findDeviation(int[] nums)
{
double mean = findMean(nums);
double squareSum = 0;
for (int i = 0; i < nums.length; i++)
{
squareSum += Math.pow(nums[i] - mean, 2);
}
return Math.sqrt((squareSum) / (nums.length - 1));
} // End of double findDeviation(int[])
/** Method for computing mean of an array of double values */
// Beginning of double findMean(double[])
public static double findMean(double[] nums)
{
double sum = 0;
for (int i = 0; i < nums.length; i++)
{
sum += nums[i];
}
return sum / nums.length;
} // End of double getMean(double[])
/** Method for computing mean of an array of int values */
// Beginning of double findMean(int[])
public static double findMean(int[] nums)
{
double sum = 0;
for (int i = 0; i < nums.length; i++)
{
sum += nums[i];
}
return sum / nums.length;
} // End of double getMean(int[])
/* Method for printing array */
// Beginning of void printArray(double[])
public static void printArray(double[] nums)
{
for (int i = 0; i < nums.length; i++)
{
System.out.print(nums[i] + " ");
}
System.out.println();
} // End of void printArray(double[])
} // End of class Deviation

Categories