Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Printing a Histogram Help - Arrays

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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!
    //************************************************************     
    **/
     
     
    }


  2. #2
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default 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:
    /**
     * 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.

Similar Threads

  1. Problem in implementing mortgage calculator
    By American Raptor in forum AWT / Java Swing
    Replies: 1
    Last Post: April 1st, 2009, 02:09 PM
  2. Elegant and short way to implement my program on 2d Array
    By TheForumLord in forum Collections and Generics
    Replies: 1
    Last Post: December 8th, 2008, 04:56 PM
  3. Java error in using Arrays
    By AnithaBabu1 in forum Collections and Generics
    Replies: 4
    Last Post: November 4th, 2008, 07:50 AM