Amadeus Bojiuc
Amadeus Bojiuc

Reputation: 179

How to place numbers into certain groups?

Okay so I will calculate distances. There will be a 20 distances groups that will get points when a match is found. How do I avoid writing conditions for each group?

Is there a shortcut to ---

if (distance>0)and(distance<=10)
{
    distance_group[1]=++1;
}
else
    if (distance>10)and(distance<=20)
    {
        distance_group[2]=++1;
    }

etc..... for all groups all the way to a number like 400

Each group gets points when a close match is found, but I don't want to write out all the groups conditions for getting a point, I might need 5000 groups instead of 20.

Any ideas?

Upvotes: 2

Views: 755

Answers (1)

Vaughn Cato
Vaughn Cato

Reputation: 64308

You can calculate the group number like this:

int group_number = (distance-1)/10+1;

and then increment the count in that group:

++distance_group[group_number];

If distance==1, you have (1-1)/10+1 == 1.

If distance==2, you have (2-1)/10+1 == 1.

. . .

if distance==9, you have (9-1)/10+1 == 1.

if distance==10, you have (10-1)/10+1 == 1.

if distance==11, you have (11-1)/10+1 == 2.

etc.

Upvotes: 6

Related Questions