# Fill table with values in ranges from another table

I have an one-dimensional table with degrees:

double tabledegrees[10]={0.2,3.4,4.3,1.2,4.6,4.5,3.8,1.5,3.4,3.7};

The degrees are always in the interval [0,5].

I want to count the number of thermometers whose degree belong in each of the intervals [0,1), [1,2),[2,3), [3,4),[4,5] and store these values in an array of integers of size 5, in which cell 0 belongs to degrees belonging to the interval [0,1), cell 1 to degrees belonging to the interval [1,2), and so on.

I want to use floor function and not a sequence of if commands.

The following program:

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(){

double tabledegrees[10]={0.2,3.4,4.3,1.2,5.6,4.5,3.8,1.5,3.4,3.7};
double tabledegreesfloored[10];

for (int i=0;i<10;i++){
tabledegreesfloored[i] = floor(tabledegrees[i]);
}

for (int j=0;j<10;j++){
printf("%.f \n", tabledegreesfloored[j]);
}
}
``````

returns:

0 3 4 1 5 4 3 1 3 3

How to achive this?

### >Solution :

You create an array of thermometers and use the interval to index into the array as you count them:

``````#include <math.h>
#include <stdio.h>

#define LEN(a) sizeof(a) / sizeof(*a)

int main() {
double tabledegrees[10]={0.2,3.4,4.3,1.2,5.6,4.5,3.8,1.5,3.4,3.7};
size_t thermometers[5] = {0};
for (size_t i=0; i < LEN(tabledegrees); i++) {
if(tabledegrees[i] < 0 || tabledegrees[i] >= LEN(thermometers)) {
printf("skip data out of range %lf\n", tabledegrees[i]);
continue;
}
thermometers[(int) floor(tabledegrees[i])]++;
}
for (size_t i=0; i < LEN(thermometers); i++)
printf("%zu: %zu\n", i, thermometers[i]);
}
``````

and here is example output:

``````skip data out of range 5.600000
0: 1
1: 2
2: 0
3: 4
4: 2
``````