Printing a Histogram Help - Arrays

• June 3rd, 2009, 07:24 PM
Mock26
Printing a Histogram Help - Arrays
Hello All,

I am currently working on a assignment where we have to input a number of students, generate (either randomly with a gaussean distribution or via user input) a test score of 1-100 for each student, and then print a histogram that represents the scores. The scores are broken down in to ranges of 10 (1-10, 11-20, 21-30, etc.) and for each grade within each range the program prints out an asterix ("*"). I have completed my assignment where it prints out all of the test scores and prints the histogram horizontally (the first row is range 1-10: *****, the second row is range 11-20: **, etc.

Now that my assignment is over I am trying to mess around with the program and figure out how prinit it vertically, so that there will be a row at the bottom of the graph that displays the grade ranges, and the asterixes are printed vertically above each group. Unfortunately, I just cannot figure this out. I would greatly appreciate any help and advice that anyone can give.

Take care and have a great day....

ciao,
john.

Here is my code:
Code :

```import javax.swing.*; import java.util.*; import java.text.*;     public class assignment6 { public static void main (String[] args) {   String continueOption;   do { int n = 1; int pupils = 1;   int [] scores = new int [10];   String msg1 = "How many students are there in the class?"; String msg2 = "Please input a positive number."; String msg3 = "Would you like to input numbers from the Console" + "or have the computer Randomly generate for you?" + "\n" + "(Enter \"C\" for Console Input or \"R\" for Random Generation)"; String msg4 = "Would you like to repeat? (Y/N)"; String msg5 = "Thank you for using this Teaching Aid." + "\n" + "We hope all your students were paying attention and passed." + "\n" + "Have a great summer, Professor Gordon!";     do { pupils = Integer.parseInt(JOptionPane.showInputDialog(msg1)); if (pupils <= 0) { JOptionPane.showMessageDialog(null,msg2); } }   while (pupils <= 0); String choice = JOptionPane.showInputDialog(msg3);   if ((choice.charAt(0)=='C') || (choice.charAt(0)=='c')) scores = userNum(pupils);   else { if(( choice.charAt(0)=='R') || (choice.charAt(0)=='r')) scores = randomNum(pupils); }   histogram(scores);   continueOption = JOptionPane.showInputDialog(msg4);   if (continueOption.equals("n") || continueOption.equals("N")) { JOptionPane.showMessageDialog(null, msg5, "Thank you message", JOptionPane.INFORMATION_MESSAGE); System.exit(0); }   System.out.println("\n");   } while(continueOption.equals("y") || continueOption.equals("Y")); }               public static int[] userNum(int n) { int i, num; int [] list = new int [10];   String msg6 = "Please enter the score between 1-100."; String msg7 = "The input is wrong, Please enter the score between 1-100.";   System.out.print("Test Scores:");   for (i = 0; i < n; i++) { num = Integer.parseInt( JOptionPane.showInputDialog(msg6));   if ((num < 1) || (num > 100)) { do { num = Integer.parseInt( JOptionPane.showInputDialog(msg7)); }   while ((num < 1) || (num > 100)); }   if (i%10 == 0) {System.out.println();}   System.out.print(num + " ");   int value = (((int)num)-1)/10;   list [value]++; }   return list; }               public static int [] randomNum(int n) {   int [] list = new int[10];   Random generator = new Random();   int i; double num = 0;   System.out.print("Test Scores:");   for(i = 0; i < n; i++) {   do { num = generator.nextGaussian();   num *= 15; num += 60;     }   while ((num < 1) || (num > 100));   int conversion = (int)num;     if (i%10 == 0) {System.out.println();}   System.out.print( " " + conversion);   int value = (conversion-1)/10;   list [value]++; }   return list; }               public static void histogram(int [] scores) { int i = 0; int index; double max; int result, key;     System.out.println("\n" + "\n" + "Histogram:");   for(i = 0; i < 10; i++) { int asterix = scores[i];   System.out.print(String.format("%2d", ((i*10)+1)) + " - " + String.format("%3d", ((i+1)*10)) + ": ");   { for(int k = 0; k < asterix ; k++) System.out.print("*"); }     System.out.println();   } System.out.println();   /** //************************************************************ //Finding the largest group of scores   result = findLargest(scores,0,scores.length); System.out.println(result); System.out.println(scores[result]);     //Largest group found! //************************************************************ **/ }           /** //************************************************************ //Finding the largest group of scores   public static int findLargest(int [ ] list,int start, int n) { int largest = start;   for(int i = start+1;i<n;++i) if (list[i] > list[largest]) largest = i;   return largest; }   //Largest group found! //************************************************************ **/     }```
• June 4th, 2009, 04:49 AM
Dalisra
Re: Printing a Histogram Help - Arrays
I would suggest to use tab to align them in a nice row.. Another option is to count how big strings are and try to compensate with spaces if string is too short..

using tabs:
Code :

```/** * Haven't tested this code, wrote it from the head, * so use it more as a pseudo code than java code */ String firstLine = "first Studen \t first score \t first info"; String secondLine = "second Student \t second Score \t second info";```
If values differ by length greatly, you might need to insert several tabs.