Results 1 to 11 of 11
  1. #1
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Histogram made with array

    Hi all, so I basically need help writing the code to make a histogram out of an array I have. It takes the grades inputted and stores them in the array:


    Java Code:
    public class Grades {
     
    	public static void main (String[] args)
    	{
    		float result = 0;
    		Scanner scan = new Scanner(System.in);
    		int count = 0;
     
    		float[] grades = new float[100];
     
    		for (int i = 0; i < grades.length; i++) {
    			System.out.print("Enter a grade between 0.0 - 100.0 (enter -1 to quit): ");
    			grades[i] = scan.nextFloat();
    			if(grades[i] == -1)break;
    			result = result + grades[i];
    			count++;
     
    		}
    		System.out.println("Average of Grades: " + result / count);
     
    		System.out.println("Histogram Of Grades");
    		System.out.println("-------------------");
     
    }
    }
    That's the current code I have, the problem I now have is that I need to add a * if the grades fall under that number range (ex.: 0-59: **, 60-69: ****) and so on for the grades up to 100. Im having trouble on how to know that the numbers fit then adding a star. Any help would be greatly appreciated, even just a start in the right direction.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Histogram made with array

    Break the big problem down into smaller steps and solve each one. For instance consider:

    • doing some basic math on the grade number to "massage" it so that it can correspond to the number of stars. I'm thinking of one division followed by one subtraction. I believe you're smart enough to figure out how to do this.
    • Use a loop to draw the stars. Since you'll know how many stars you'll want at the start of the loop, I'd use a for loop, and print one Star inside the loop.
    • After the loop ends, and the stars have been drawn, println() to move to the next line.


    Luck.

  3. #3
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Histogram made with array

    Would I have do the math for every grade range, or just once? (sorry newbie programmer).

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Histogram made with array

    You need to test each grade to see which range it falls into.

  5. #5
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Histogram made with array

    Is there any snippet of code you could give me without giving it away at all?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Histogram made with array

    Quote Originally Posted by Bowsan22 View Post
    Would I have do the math for every grade range, or just once? (sorry newbie programmer).
    Each grade has a different value and each corresponding line has a number of stars that corresponds to the grade's value, so yes, the calculation needs to be done for each grade. Another for loop and/or a method would work well for this so that you only have to write the code for the calculation once.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Histogram made with array

    Quote Originally Posted by Bowsan22 View Post
    Is there any snippet of code you could give me without giving it away at all?
    My own preference is that you try first on your own, else I'd be cheating you out of a valuable learning experience. My own experience has been that I learn best when I have to force my brain to strain a bit.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Histogram made with array

    You will need an array to keep track of the count of 0-59 scores, 60-69 scores etc. In the same loop as you read in user input, perform the check of which range the score falls into (personally I used a subtraction then a division) and increment a count in the array.

    After the input has completed you will need nested loops. Outer loop does the ranges, inner loop prints n stars for each range.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Histogram made with array

    I just realised you might have to handle scores below 50 differently. Unless Fubar's solution is more elegant than mine.

  10. #10
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Histogram made with array

    Alright thanks, gonna work on it again before class tomorrow and tell you how it goes.

  11. #11
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Histogram made with array

    So I just ended up doing the histogram the long and tedious way:
    Java Code:
    System.out.print("0% - 59% (F)");
    		for (int i = 0; i < grades.length; i++)
    			if (grades[i] > 0 && grades[i] < 60)System.out.print("*");
    		System.out.println("");
    		
    		System.out.print("60% - 69% (D)");
    		for (int i = 0; i < grades.length; i++)
    			if (grades[i] >= 60 && grades[i] < 70)System.out.print("*");
    		System.out.println("");
    		
    		System.out.print("70% - 79% (C)");
    		for (int i = 0; i < grades.length; i++)
    			if (grades[i] >= 70 && grades[i] < 80)System.out.print("*");
    		System.out.println("");
    		
    		System.out.print("80% - 89% (B)");
    		for (int i = 0; i < grades.length; i++)
    			if (grades[i] >= 80 && grades[i] < 90)System.out.print("*");
    		System.out.println("");
    		
    		System.out.print("90% - 100% (A)");
    		for (int i = 0; i < grades.length; i++)
    			if (grades[i] >= 90 && grades[i] <= 100)System.out.print("*");
    		System.out.println("");
    I couldn't figure out what to do using the extra method and / or another for loop. The program works this way, but I would still like to know the other, less tedious way of doing it.

Similar Threads

  1. need help with name histogram
    By cachico12 in forum New To Java
    Replies: 1
    Last Post: 05-09-2011, 08:27 AM
  2. histogram
    By little_man in forum New To Java
    Replies: 4
    Last Post: 11-13-2010, 01:38 AM
  3. Home-Made GUI Object
    By billycro in forum AWT / Swing
    Replies: 2
    Last Post: 09-21-2010, 01:06 AM
  4. histogram equalization
    By syarizma in forum New To Java
    Replies: 2
    Last Post: 08-14-2009, 03:03 AM
  5. No class made
    By ChuckLS in forum New To Java
    Replies: 1
    Last Post: 04-28-2009, 04:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •