dimanche 1 mars 2015

Java: Creating a frequency table for integers randomly generated and placed in an array


Vote count:

0




The coding is slightly incomplete since I've tried a few ways in order to create the frequency table. I'm aware that it is more common to use map and other formats, but this code is for my online programming class and must use two arrays. For the frequencies, I repeatedly get 0. I use Dr.Java to write and compile my programs.


This is the code I have so far:



public class Arrays {
public static void main(String args[]) {
//Assigning the array length according to the user's input
int N = Integer.parseInt(args[0]);
int randoms[] = new int[N];
//Filling the array with random integer values
for (int i=0; i<randoms.length; i++) {
randoms[i] = (int) (Math.random()*10 + 1);
System.out.println(randoms[i]);
}
double sum = 0.0;
for (int i=0; i<randoms.length; i++) {
sum += randoms[i];
}
System.out.println("The average of the values is " + (sum/N));
int freq[] = new int[10];
for (int i=0; i<randoms.length; i++){
if (randoms[i] == 1) {
freq[1] = freq[1]+1;
}
}
}
}


asked 39 secs ago







Java: Creating a frequency table for integers randomly generated and placed in an array

Aucun commentaire:

Enregistrer un commentaire