QUESTION: Design and implement an application that creates a histogram that allo
ID: 639336 • Letter: Q
Question
QUESTION: 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 | *********
_______________________________________________
//I have this so far. I need to fix DataInputStream input = new DataInputStream(System.in);
//so it can read my int value input please help! Please Explain what in the heck is going on....I'm so frustrated.
public class mybrainhurts
{
public static void main(java.lang.String[] args) {
DataInputStream input = new DataInputStream(System.in);
final int MAX = 10;
final int MIN = 1;
final int RANGE = 10;
int[] list = new int[MAX];
// Initialize to 0
for (int i=0; i<list.length; i++) {
list[i] = 0;
}
System.out.println ("ENTER ANY INTEGERS FROM 1 to 100: ");
System.out.println ("TO QUITE AND SEE HISTOGRAM PRESS 999");
System.out.print ("PLEASE ENTER INTEGER: ");
int value = Integer.parseInt (stdin.readLine());
while (value >= MIN && value <= (MAX*RANGE)) {
// Divide by range to determine which range to increment
list[(value-1)/RANGE] = list[(value-1)/RANGE] + 1;
// Enter next integer value
System.out.print ("ENTER INTEGER: ");
value = Integer.parseInt (input.readLine());
}
// Print the histogram
System.out.println ("HISTOGRAM= ");
for (int i=0; i<list.length; i++) {
// Print histogram labels
System.out.print (" " + (i*RANGE+1) + " - " + (i+1)*RANGE + " | ");
// Print as many asterisks as the value in list[i]
for (int j=0 ; j<list[i] ; j++) {
System.out.print ("*");
}
System.out.println (); // starts new histogram line
}
}
}
Explanation / Answer
import java.util.Scanner;
public class mybrainhurts
{
public static void main(java.lang.String[] args) {
Scanner input = new Scanner(System.in);
final int MAX = 10;
final int MIN = 1;
final int RANGE = 10;
int[] list = new int[MAX];
// Initialize to 0
for (int i=0; i<list.length; i++) {
list[i] = 0;
}
System.out.println ("ENTER ANY INTEGERS FROM 1 to 100: ");
System.out.println ("TO QUIT AND SEE HISTOGRAM PRESS 999");
System.out.print ("PLEASE ENTER INTEGER: ");
int value = input.nextInt();
while (value >= MIN && value <= (MAX*RANGE)) {
// Divide by range to determine which range to increment
list[(value-1)/RANGE] = list[(value-1)/RANGE] + 1;
// Enter next integer value
System.out.print ("ENTER INTEGER: ");
value = input.nextInt();
}
// Print the histogram
System.out.println ("HISTOGRAM= ");
for (int i=0; i<list.length; i++) {
// Print histogram labels
System.out.print (" " + (i*RANGE+1) + " - " + (i+1)*RANGE + " | ");
// Print as many asterisks as the value in list[i]
for (int j=0 ; j<list[i] ; j++) {
System.out.print ("*");
}
System.out.println (); // starts new histogram line
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.