The following Java Code generates random numbers. Put them in an array and prints the number of times each one occurs in the Array.
Only generated numbers will be listed.
import java.util.Random;
public class Histograms {
public static void main(String[] args) {
final int size = 100;
int[] numbers = new int[size];
int[] distribution = new int[size];
Random rand = new Random(System.currentTimeMillis());
for (int i = 0; i < size; ++i) {
numbers[i] = rand.nextInt(100 ) ;
}
for (int i = 0; i < size; ++i) {
System.out.print(numbers[i] + "\t");
if ((i + 1) % 10 == 0) {
System.out.println();
}
}
System.out.println();
for (int i = 0; i < size; ++i) {
++distribution[numbers[i]];
}
/* for (int i = 1; i < size; ++i) {
System.out.println("Number " + i + " occurs " + distribution[i] + " times. ");
}*/
showHistogram(distribution, size);
}
static void showHistogram(int[] arr, int size) {
String stars = "";
int totStars = 0;
for (int i = 0; i < size; ++i) {
totStars = arr[i];
for (int j = 1; j <= totStars; ++j) {
stars += "*";
}
if (totStars > 0)
System.out.println(i + ": " + stars);
stars = "";
totStars = 0;
}
}
}
Be the first to comment