In: Computer Science
java
8.3: Histogram
Design and implement an application that creates a histogram that
allows you to visually inspect the frequency distribution of a set
of values. The program should read in an arbitrary number of
integers that are in the range 1 to 100 inclusive; then produce a
chart similar to the one below that indicates how many input values
fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk
for each value entered.
1 - 10 | *****
11 - 20 | ****
21 - 30 | *********
31 - 40 | **
41 - 50 | **************
51 - 60 | ******
61 - 70 | ****
71 - 80 | ********
81 - 90 | *
91 - 100 | ***
import java.util.*;
public class Sample{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
int[] inner_limit=new int[]{1,11,21,31,41,51,61,71,81,91};
int[] outer_limit=new int[]{10,20,30,40,50,60,70,80,90,100};
int[] frq_cnt=new int[]{0,0,0,0,0,0,0,0,0,0};
System.out.println("Enter -1 for exit:");
int element;
while(true){
element=input.nextInt();
if(element==-1){break;}
for(int i=0;i<10;i++){
if(element>=inner_limit[i] && element<=outer_limit[i]){
frq_cnt[i]=frq_cnt[i]+1;
break;
}
}
}
for(int i=0;i<10;i++){
System.out.print(inner_limit[i]+" - "+outer_limit[i]+" | ");
for(int j=0;j<frq_cnt[i];j++){
System.out.print("*");
}System.out.println("");
}
}
}
#output: