In: Computer Science
In Statistics, a histogram is a graphical representation of data using bars of different heights. In a histogram, each bar groups numbers into ranges. Taller bars show that more data falls in that range. A histogram displays the shape and spread of continuous or discrete sample data.
In this project, you will write a program that creates a histogram that allows users to visually inspect the frequency distribution of a set of values.
REQUIREMENTS:
* Read from the data file the values in the range of 1-100 inclusive.
* Produce a histogram as shown in the expected output below to indicate how many input values fell in the range 1 to 10, 11 to 20, and so on.
EXPECTED OUTPUT:
run:
1-10 I*****
11-20 I******
21-30 I**
31-40 I*
41-50 I***********
51-60 I******
61-70 I********
71-80 I***
81-90 I****
91-100 I****
DATA FILE
51 68 42 61 23 50 79 9 12 9 67 85 64 97 41 13 66 47 54 2 46 48 11 52 79 48 73 48 51 15 44 97 19 48 53 13 58 47 95 2 1 65 84 28 89 99 70 67 84 36
import java.io.File;
import java.util.Scanner;
public class Histogram {
public static void main(String[] args) throws
Exception{
int arr[]=new int[10];
Scanner sc = new Scanner(new
File("histogram.txt"));
int n;
while(sc.hasNext()) {
n=sc.nextInt();
if(n==100)
arr[9]++;
else
arr[n/10]++;
}
String l[]=
{"1-10|","11-20|","21-30|","31-40|","41-50|","51-60|","61-70|","71-80|","81-90|","91-100|"};
for(int i=0;i<arr.length;i++)
{
System.out.print(l[i]+" ");
for(int
j=0;j<arr[i];j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me