This program is for calculating students marks frequency
float value[50];
int group[11] = {0};
for (i = 0; i < 50; i++) {
scanf("%f",&value[i]); /* reading of values */
++group[(int)(value[i])/10)]; /* what does this line do.? */
}
This line:
++group[(int)(value[i]) / 10)];
Is the same as:
int flooredValue = (int)value[i]; //e.g. 3.5 becomes 3; 12.6 becomes 12
int groupIndex = flooredValue / 10; //division discarding the remainder
group[groupIndex] += 1;
What does it mean, though? Well here's how it ends up converting the values:
value[i] | groupIndex
----------+------------
3.5 | 0
12.6 | 1
18.6 | 1
23.1 | 2
57 | 5
94.6 | 9
100 | 10
So it ends up counting how many marks fall into each bucket of 10. Scores from 0 to 9.9999 fall in the "0" bucket, from 10 to 19.99999 in the "1" bucket, etc. Note the following, though:
value[i] | groupIndex
-----------+------------
-3.5 | -3
155.6 | 15
As it is now, your input could certainly be less than zero or over 100. If that happens, you're going to write into memory outside of the bounds of group. This will lead to very unpleasant behavior. Check either that the input value is in bounds or that the group index is in bounds before indexing into the group array.
++group[(int)(value[i])/10)];
lets divide this
1. (int)(value[i])/10)
for example value[i] is 51 it will result 5 as you are using casting to int.
2. ++group[5]; // is same as group[5] += 1;
pre incrementing value of group[5]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With