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.
#include <iostream>
usingnamespace 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: ";
elseif (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;
}
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...
1 2 3 4 5 6 7
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.
//BarChart.cpp
//How this bar chart works.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main(){
constint 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<GRADES;i++){ // for each grade, increment the appropriate
//frequency
frequency[gradesArray[i]/10]++;
cout<<"Add 1 at index "<<gradesArray[i]/10
<<"\nof frequency array!\n"<<endl;
}//end for
cout<<'\n'<<endl;
for(int i=0;i<GRADES;i++){
cout<<"index "<<i;
if (i==0)// output bar labels ("0-9:", ..., "90-99:", "100:" )
cout <<" 0-9: ";
elseif (i==10)
cout << " 100: ";
else
cout <<' '<< i * 10 << "-" << (i*10) + 9 << ": ";
// print bar of asterisks
for(int j=0;j<frequency[i];j++)
cout<<'*';
cout<<endl; //start a new line of output
}//end outer for
return 0; //indicates success
}//end main
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: **