The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. |
Home Vectors A Histogram | ||
A Histogram
A better solution is to use a vector with length 10. That way we can create all ten storage locations at once and we can access them using indices, rather than ten different names. Here's how: int numValues = 100000;int upperBound = 10; pvector<int> vector = randomVector (numValues, upperBound); pvector<int> histogram (upperBound); for (int i = 0; i<upperBound; i++) { int count = howMany (vector, i); histogram[i] = count; } I called the vector histogram because that's a statistical term for a vector of numbers that counts the number of appearances of a range of values. The tricky thing here is that I am using the loop variable in two different ways. First, it is an argument to howMany, specifying which value I am interested in. Second, it is an index into the histogram, specifying which location I should store the result in.
|
||
Home Vectors A Histogram |