• 09-06-2011, 05:28 AM
Bowsan22
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:

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.
• 09-06-2011, 05:35 AM
Fubarable
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
Bowsan22
Would I have do the math for every grade range, or just once? (sorry newbie programmer).
• 09-06-2011, 05:57 AM
Junky
You need to test each grade to see which range it falls into.
• 09-06-2011, 06:09 AM
Bowsan22
Is there any snippet of code you could give me without giving it away at all?
• 09-06-2011, 06:10 AM
Fubarable
Quote:

Originally Posted by Bowsan22
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.
• 09-06-2011, 06:11 AM
Fubarable
Quote:

Originally Posted by Bowsan22
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.
• 09-06-2011, 06:16 AM
Junky
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
Junky
```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("");```