1. Member
Join Date
Sep 2011
Posts
17
Rep Power
0

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 static void main (String[] args)
{
float result = 0;
Scanner scan = new Scanner(System.in);
int count = 0;

for (int i = 0; i < grades.length; i++) {
System.out.print("Enter a grade between 0.0 - 100.0 (enter -1 to quit): ");
count++;

}
System.out.println("Average of Grades: " + result / count);

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. ## 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. 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).

4. ## Re: Histogram made with array

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

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?

6. ## Re: Histogram made with array

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.

7. ## Re: Histogram made with array

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.

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.

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.

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.

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:
Java Code:
System.out.print("0% - 59% (F)");
for (int i = 0; i < grades.length; i++)
System.out.println("");

System.out.print("60% - 69% (D)");
for (int i = 0; i < grades.length; i++)
System.out.println("");

System.out.print("70% - 79% (C)");
for (int i = 0; i < grades.length; i++)
System.out.println("");

System.out.print("80% - 89% (B)");
for (int i = 0; i < grades.length; i++)
System.out.println("");

System.out.print("90% - 100% (A)");
for (int i = 0; i < grades.length; i++)
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.

#### Posting Permissions

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