Question

[JAVA] I need help creating a method to calculate the Standard Deviation of a 2D Array....

[JAVA] I need help creating a method to calculate the Standard Deviation of a 2D Array.

This is what I have so far:

public double calcStdDev() {
   int total = 0;

   for(int i = 0; i < arr.length; i++){
       total += arr[i][i];
   }

   double mean = total / arr.length;
   return mean;
}

The numbers I'm using at just 1,2,3,4,5,6 and the standard deviation is coming out wrong. Can I have help fixing my code?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
package HomeworkLib1;
public class StdDev {
        public static void main(String[] args) {
                int[][] data = new int[3][3];
                int i, j;
                for(i=0; i<3; i++) {
                        for(j=0; j<3; j++) {
                                data[i][j] = (int)(Math.random()*10);                   //Randomly setting values for 2D array of size 3x3
                        }
                }
                int sum = 0;
                for(i=0; i<3; i++) {
                        for(j=0; j<3; j++) {
                                sum += data[i][j];
                        }
                }       
                for(i=0; i<3; i++) {
                        for(j=0; j<3; j++) {
                                System.out.print(data[i][j] + " ");
                        }
                        System.out.println();
                }
                System.out.println("Sum: "  + sum);
                double mean=0.0;                double variance=0.0;
                int size = data[0].length * data.length;
                mean = sum/size;
                for(i=0; i<3; i++) {
                        for(j=0; j<3; j++) {
                                variance += (mean - data[i][j])*(mean - data[i][j]);
                        }
                }
                variance /=size;
                double stdDev = Math.sqrt(variance);
                System.out.println("Mean: " + mean);
                System.out.println("Variance: " + variance);
                System.out.println("Standard Deviation: " + stdDev);
        }
}

Add a comment
Know the answer?
Add Answer to:
[JAVA] I need help creating a method to calculate the Standard Deviation of a 2D Array....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • JAVA: array return types May you please help me fix this coding. Sum is SUPPOSE to...

    JAVA: array return types May you please help me fix this coding. Sum is SUPPOSE to be 1253 import java.util.Scanner; public class Array { public static int sum(int[] arr) { int i; int total = 0; for ( i =0; i < arr.length; ++i) { total = total + arr[i]; } return total; } public static void main(String[] args) { int[] myArray = {45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44,...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • I need help writing the helper function find() and removeAll(). I'm pretty sure i have find...

    I need help writing the helper function find() and removeAll(). I'm pretty sure i have find wrong. please help me! I included the top portion of my code so you get an idea of whats going going. on Java language someone asked structure of list node too? i dont know what they mean public class LinkedIntList public ListNode front; // first value in the list // post: constructs an empty list public LinkedIntList( front null; e public LinkedIntList(int[l arri this);...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • This is java. I need help with my computer science class. Question 11 Assume that you...

    This is java. I need help with my computer science class. Question 11 Assume that you have an array of integers named arr. The following program segment is intended to sum arr [0]through arr[n−1], where n = arr.length:          sum = 0;          i = 0;          n = arr.length;          while (i != n)          {             i++;             sum += arr[i];          } In order for this segment to perform as intended, which of the following modifications, if any, should be made? 1.No modification is necessary...

  • (JAVA) Given an array of unique positive integers, write a function findSums that takes the array...

    (JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called “hashT” and inserts all the elements of the input array in the hashtable. 2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console. 3. Returns another hashtable names “sums” of those sum elements. For example: Input: [1,5,4,6,7,9] Output: [6,5,7,9] Explanation: 6 = 1...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Below I have done a bubble sort on an array. I need to figure out how...

    Below I have done a bubble sort on an array. I need to figure out how to change the --- bubbleSort() method... with a new method called oddEvenSort() method.. I know I have to do two passes one for odd, one for even, can I have someone help me do this please I keep getting a lot of errors when I attempt to do it. Java code to change is below. I have to sort the array using one pass...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT