Results 1 to 11 of 11
Thread: Histogram made with array
- 09-06-2011, 05:28 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
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:
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.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("-------------------"); } }
-
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.
- 09-06-2011, 05:49 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Histogram made with array
Would I have do the math for every grade range, or just once? (sorry newbie programmer).
- 09-06-2011, 05:57 AM #4
Re: Histogram made with array
You need to test each grade to see which range it falls into.
- 09-06-2011, 06:09 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Histogram made with array
Is there any snippet of code you could give me without giving it away at all?
-
Re: Histogram made with array
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.
-
Re: Histogram made with array
- 09-06-2011, 06:16 AM #8
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.
- 09-06-2011, 06:18 AM #9
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.
- 09-06-2011, 06:31 AM #10
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Histogram made with array
Alright thanks, gonna work on it again before class tomorrow and tell you how it goes.
- 09-07-2011, 03:12 AM #11
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Histogram made with array
So I just ended up doing the histogram the long and tedious way:
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.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("");
Similar Threads
-
need help with name histogram
By cachico12 in forum New To JavaReplies: 1Last Post: 05-09-2011, 08:27 AM -
histogram
By little_man in forum New To JavaReplies: 4Last Post: 11-13-2010, 01:38 AM -
Home-Made GUI Object
By billycro in forum AWT / SwingReplies: 2Last Post: 09-21-2010, 01:06 AM -
histogram equalization
By syarizma in forum New To JavaReplies: 2Last Post: 08-14-2009, 03:03 AM -
No class made
By ChuckLS in forum New To JavaReplies: 1Last Post: 04-28-2009, 04:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks