//*****************************************************************
//Histogrm1.Java
//
//Java program that takes numerical inputs between 1-100 and prints
//them in to categories of 10 with a * to represent each 5 number
//values
//*****************************************************************
import java.io.*;
public class Histogram1 {
// Main method
public static void main(java.lang.String[] args) throws IOException {
//Storage of int
DataInputStream stdin = new DataInputStream (System.in);
final int MAXRANGE = 10;
final int MINRANGE = 1;
final int RANGE = 10;
int[] list = new int[MAXRANGE];
for (int i=0; i<list.length; i++) {
list[i] = 0;
}
// Enter Integers of range
System.out.println ("Enter some numbers between 1 and 100.");
System.out.println ("Signal the end by entering a number outside of that range.");
// Entering the actual number
System.out.print ("Enter Integer: ");
int value = Integer.parseInt (stdin.readLine());
// Will keep adding integers within range
while (value >= MINRANGE && value <= (MAXRANGE*RANGE)) {
// Range
list[(value-1)/RANGE] = list[(value-1)/RANGE] + 1;
// Enter next integer
System.out.print ("Enter Integer: ");
value = Integer.parseInt (stdin.readLine());
}
// Print histogram
System.out.println ("\nThis is the histogram:");
for (int i=0; i<list.length; i++) {
System.out.print (" " + (i*RANGE+1) + " - " + (i+1)*RANGE + "\t| ");
// Print *
for (int j=0 ; j<list[i] ; j++) {
System.out.print ("*");
}
System.out.println ();
}
}
}
Histogram java question
Page 1 of 11 Replies - 222 Views - Last Post: 07 October 2012 - 05:15 PM
#1
Histogram java question
Posted 07 October 2012 - 03:33 PM
Hey guys, my question was just i understand how the program works and well this program works fine but where question comes in at is the second part of this program. For example the second part asks to print an asterisk for every 5 values instead of 1 asterisk for every value. Ive been trying to declare variables where i would replace the print action of "*" from one value to printing 5 values but each time i keep getting errors. Well anyway if anyone could help going in the right direction that would be awesome. Thanks in advance
Replies To: Histogram java question
#2
Re: Histogram java question
Posted 07 October 2012 - 05:15 PM
I'm not sure how I should put this so I'm not giving you the answer directly.
So let's say someone picked the number 30 5 times. If your key is to print one asterisk PER one time putting the number 30 in.
You can display this ratio as 1:1
If you need 1 asterisk per 5 inputs. You can display this ratio as 1:5.
So the first number is how many asterisks you need per value, the second number is how many values you need to have a asterisk.
Plus remember ratios are also fractions, and fractions are just division problems.
So let's say someone picked the number 30 5 times. If your key is to print one asterisk PER one time putting the number 30 in.
You can display this ratio as 1:1
If you need 1 asterisk per 5 inputs. You can display this ratio as 1:5.
So the first number is how many asterisks you need per value, the second number is how many values you need to have a asterisk.
Plus remember ratios are also fractions, and fractions are just division problems.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|