Skip to main content
gamedev.net
🔒 Locked

c programming question...

Started by FreJa 2 replies 450+ views
Original Post
FreJa
FreJa
Hi, Lets say i have a database with tons of records where one of the fields is age. And i want to know the number of records where age is between 0 and 10 yo, 10 and 20... and so on... Whats the best way to do this? Notice: I dont want to have one int variable for each age interval. For example: Record 1: Age=5 Record 2: Age=12 Record 3: Age=76 Record 4: Age=34 Record 5: Age=23 Record 6: Age=87 Record 7: Age=4 Record 8: Age=98 Record 9: Age=101 Record 10: Age=54 Record 11: Age=67 ... the output would be: 0-10: 2 10-20:1 20-30:1 30-40:1 40-50:0 50-60:1 60-70:1 70-80:1 80-90:1 90-100:1 100-110:1 Thanks
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
Deranged
Deranged
for loop:

int n;for(int r; r < num_of_records; r++){    if(records[r].age <= 10 && records[r].age >= 0)        n++}


I hope this is what you wanted.
Drew_Benton
Drew_Benton
If you were to make a std::vector<int> data, then resize it to the number of intervals, in this case 11, you should be able to just go through the data and take the age/10 and it would put it in the correct location. For example:
Record 1: Age=5Record 2: Age=12Record 3: Age=76Record 4: Age=34Record 5: Age=235/10 = 0 --> Data[0]12/10 = 1 --> Data[1]76/10 = 7 --> Data[7]34/10 = 3 --> Data[3]23/10 = 2 --> Data[2]


If you use that concept, it should do everything you want it to do quite easily and fast, since you are just going to do data[Age/10]++. Just an idea to try, I don't know if it will work how you want it to, but that's the concept. You would have to use the C styled version of this (ie a regular int array).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.