More efficient way of counting the number of values within an interval? Suppose I have a text file which contains 10,000 random values between 0 and 1, and I want to count the number of values within a specific interval.  An example may explain it more clearly.

Suppose my Datefile.txt contains data like:

0.0102244
0.028072
0.0144578
0.064578
0.08148
0.012749
0.12749.....and 10,000 more values


And I want to count the number of values within the interval 0.05 (i.e. it searches for the values within the range 0.0 to 0.05, then 0.05 to 0.1 and so on). For the example above, the output will be:

In range 0 to 0.05, total values 4
In range 0.05 to 0.1, total values 2
In range 0.1 to 0.15, total values 1...etc.


Here is my code:

int main(){

double area;
double a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0;

ifstream theFile ("Datafile.txt");

  while(theFile &gt;&gt;area){

    if (area&lt;=0.05){
        a++;
    }

    else if (area&lt;=0.1){
        b++;
    }
    else if (area&lt;=0.15){
        c++;
    }
    else if (area&lt;=0.2){
        d++;
    }
    else if (area&lt;=0.25){
        e++;
    }
    else if (area&lt;=0.3){
        f++;
    }
    else if (area&lt;=0.35){
        g++;
    }
    else if (area&lt;=0.4){
        h++;
    }
    else if (area&lt;=0.45){
        i++;
    }
    else if (area&lt;=0.5){
        k++;
    }

    else if (area&lt;=0.55){
        l++;
    }
    else if (area&lt;=0.6){
        m++;
    }
    else if (area&lt;=0.65){
        n++;
    }
    else if (area&lt;=0.7){
        o++;
    }
    else if (area&lt;=0.75){
        p++;
    }
    else if (area&lt;=0.8){
        q++;
    }
    else if (area&lt;=0.85){
        r++;
    }
    else if (area&lt;=0.9){
        s++;
    }
    else if (area&lt;=0.95){
        t++;
    }
    else if (area&lt;=1.0){
        u++;
    }

}


Though my code is working fine, it might not be the most efficient way. So I put my code here for review. Can anyone suggest a better way of doing this?

Updated (based on the answer of ChrisWue)

#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;



using namespace std;

int main(){

const double bucket_size = 0.05;
double area=0;
int number_of_buckets = (int)ceil(1 / bucket_size); 
std::vector&lt;int&gt; histogram(number_of_buckets);
ifstream theFile("Datafile.txt");


while (theFile &gt;&gt; area) {
    int bucket = (int)floor(area / bucket_size);
    histogram[bucket] += 1;
}
for(auto loop = histogram.begin(); loop != histogram.end();++loop)
    {

cout&lt;&lt;bucket_size&lt;&lt;"\t"&lt;&lt;number_of_buckets&lt;&lt;endl;

}

return 0;
}


For the data-

0.0102244
0.028072
0.0144578
0.064578
0.08148
0.012749
0.12749.....


it gives the output like this-

0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20
0.05    20


What am I doing wrong?
What you want is a histogram of your values. If you want it to be flexible in the future with different bucket sizes, then you can use a std::vector and calculate how many buckets you need.

If the values are in range [0, 1] and 0 &lt; bucket_size &lt; 1, then the number of buckets you need obviously is ceil(1 / bucket_size).

So what I'd do would probably look something like this:

const float bucket_size = 0.05;
int number_of_buckets = (int)ceil(1 / bucket_size); // requires &lt;cmath&gt;
std::vector&lt;int&gt; histogram(number_of_buckets);

...

while (theFile &gt;&gt; area) {
    int bucket = (int)floor(area / bucket_size);
    histogram[bucket] += 1;
}


No big if-else cascade and easy to adjust for different bucket sizes.