2

Guys I am trying to understand why we have incremented frequency and divided grade by 10 could somebody please help explain.

public void getBarChart(){

    System.out.println("\nGrade Distribution: ");

    int[] frequency = new int[11];

    for (int grade : grades)
        ++frequency[grade / 10];

    for (int counter =0 ; counter < frequency.length; counter++){


        if(counter == 10){

            System.out.printf("%5d:  ",100);
        }

        else{

            System.out.printf("%02d-%02d: "
                    ,counter * 10, counter * 10 + 9 );
        }

        for (int stars= 0; stars < frequency[counter] ; stars++)
            System.out.print("*");

        System.out.println();
    }
}
3
  • so what line(what exactly do you don't understand)? Commented Oct 24, 2017 at 8:48
  • It is storing the amount of grades that it finds in a certain range. Imagine the value of grade is 78, then it will increment to number in the seventh element of the frequency array Commented Oct 24, 2017 at 8:51
  • @inoxy probably the line where he "... incremented frequency and divided grade by 10 ..." like it says in the question. Commented Oct 24, 2017 at 8:51

1 Answer 1

3

Assuming grade can be between 0 and 100, the frequency array counts how many grades fall in the groups 0-9, 10-19, ..., 90-99, 100.

That's the reason you divide grade by 10 to locate the array index.

++frequency[grade / 10] increments the count of grades that fall in the group of that grade.

So, for example, the grade 75 will be counted in the array element frequency[75 / 10] which is frequency[7]. frequency[7] will contain the number of grades in the range 70 to 79.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.