java - calculating frequency in arrayList -


the below code works want add method calculate frequency of integers in arraylist. example: 90-99 freq: 3.... 80-89 freq 6

what best way go about? should make if statement counts++ each variable?

import java.io.*; import java.lang.math; import java.util.*; import java.text.decimalformat;  public class gradesorter{  public static void main(string[] args) throws ioexception { { decimalformat fmt = new decimalformat("0.000"); scanner scanner = new scanner(new file("grades.dat")); double average; double deviation; double sum = 0; int number = 0; int newnumber = 0; arraylist<integer> element = new arraylist<integer>();    while (scanner.hasnextint()) { element.add(scanner.nextint());   } (int item : element){         sum += item;         system.out.println(item); }  average = sum / element.size();  (int = 0; < element.size(); i++) {  newnumber += math.pow((element.get(i) - average),2);  }   deviation = math.sqrt(newnumber / (element.size()));     system.out.println("the average of these grades : " + fmt.format(average));  system.out.println("the standard deviation of these grades is: " + fmt.format(deviation));     } } } 

----jgrasp exec: java gradesorter

51 52 55 57 58 61 62 63 66 66 66 70 72 73 74 75 75 77 77 78 79 81 82 84 86 87 88 91 94 97 average of these grades : 73.233 standard deviation of these grades is: 12.288   ----jgrasp: operation complete. 

create array of size 10, holds frequency of numbers in each range:

int[] rangefrequency = new int[10]; 

now, numbers in range - [0,9], frequency go in index 0. range [10, 19], frequency go in index 1, , on...

now point how index range? here's how:

pseudocode:

for each element in list     bucket = element / 10   // range [0-9], give index = 0                            // range [10-19], give index = 1     rangefrequence[bucket] += 1; 

code:

int[] rangefrequency = new int[10];  (int elem: element) {     int bucket = elem / 10;     rangefrequency[bucket] += 1; }  // print frequency array: (int = 0; < rangefrequency.length; ++i) {     system.out.print("frequency in range : [" + * 10 + ", " +                                                   (i * 10 + 9) + ") --> ");     system.out.println(rangefrequency[i]); } 

note need take care of arrayindexoutofbounds, when grade < 0 || grade >= 100, above program crash.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -