### Don't understand why this BarChart works

This is one of the functions in a larger class, most of which I understand very clearly. I understand most of this, and actually have it memorized (I memorize everything I learn, but am not happy unless I fundamentally understand what it actually does conceptually), but just can't grasp what is happening in the line with:

frequency[gradesArray[i]/10]++;

Why does that work - I know it's supposed to output the frequency of how many grades you have in a 10 point range (i.e. 80-89), but I just can't visualize it. I understand most of the other lines.

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include using namespace std; int main(){ int gradesArray[10] = {88, 95, 55, 76, 82, 94, 59, 77, 88, 43}; cout << "\n\nGrade Distribution: \n\n"; int frequency[10] = {}; for (int i = 0; i < 10; i++) frequency[gradesArray[i]/10]++; for (int k = 0; k < 10; k++){ if (k == 0) cout << " 0-9: "; else if (k == 10) cout << " 100: "; else cout << k * 10 << "-" << (k*10) + 9 << ": "; for (int z = 0; z < frequency[k]; z++) cout << '*'; cout << endl; } return 0; }``````

Here is what the output looks like:

Grade Distribution:

0-9:
10-19:
20-29:
30-39:
40-49: *
50-59: **
60-69:
70-79: **
80-89: ***
90-99: **

Last edited on
Which part is more difficult to you?

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include using namespace std; int main(){ int gradesArray[10] = {88, 95, 55, 76, 82, 94, 59, 77, 88, 43}; cout << "\n\nGrade Distribution: \n\n"; int frequency[10] = {}; for (int i = 0; i < 10; i++) // for each grade, increment the appropriate //frequency frequency[gradesArray[i]/10]++; for (int k = 0; k < 10; k++){// for each grade frequency, print bar in chart if (k == 0)// output bar labels ("0-9:", ..., "90-99:", "100:" ) cout << " 0-9: "; else if (k == 10) cout << " 100: "; else cout << k * 10 << "-" << (k*10) + 9 << ": "; // print bar of asterisks for (int z = 0; z < frequency[k]; z++) cout << '*'; cout << endl; //start a new line of output } return 0; }``````
@eyenrique...

This line:

frequency[gradesArray[i]/10]++;

I get that gradesArray[i] is equal to 88, 95, 55, etc., and then you divide it by ten, so (8.8, 9.5, 5.5, etc.), but don't get how why you add one, and how exactly that gives you the frequency, which, for this, is: 0, 0, 0, 0, 1, 0, 2, 3, 2.

I'm usually pretty good with Arrays, but I don't get this one, yet.
Integer division gets truncated. So 88/10 is actually `8`, not `8.8`

This is significant because the result is being used as an index in an array.

You can think of each element in the array as a tally of how many times a grade has fallen within that range.

So...
 ``1234567`` ``````int range = grade / 10; // divide the grade by 10 to figure out which range it falls in // IE: grades 0-9 fall into range 0 // grades 10-19 fall into range 1 // grades 20-29 fall into range 2 // etc frequency[range] += 1; // add one to that range's frequency ``````

The above code is a more verbose version... but it does the same thing.

`frequency[gradesArray[i]/10]++;`

Is the same, just more compact.
@Disch explained very well.
I hope this helps
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````//BarChart.cpp //How this bar chart works. #include using std::cout; using std::cin; using std::endl; int main(){ const int GRADES=10; int gradesArray[GRADES]={88, 95, 55, 76, 82, 94, 59, 77, 88, 43}; int frequency[GRADES]={}; cout<<"Grade Distribution:\n\n"; for(int i=0;i

 ``` Eyenrique-MacBook-Pro:Help Eyenrique\$ ./BarChart Grade Distribution: Add 1 at index 8 of frequency array! Add 1 at index 9 of frequency array! Add 1 at index 5 of frequency array! Add 1 at index 7 of frequency array! Add 1 at index 8 of frequency array! Add 1 at index 9 of frequency array! Add 1 at index 5 of frequency array! Add 1 at index 7 of frequency array! Add 1 at index 8 of frequency array! Add 1 at index 4 of frequency array! index 0 0-9: index 1 10-19: index 2 20-29: index 3 30-39: index 4 40-49: * index 5 50-59: ** index 6 60-69: index 7 70-79: ** index 8 80-89: *** index 9 90-99: ** ```
Topic archived. No new replies allowed.