I have an array that is made up doubles which I need to round down and convert to integers so I can use them as indices in an output array. I have just started C programming and am not sure how this works. So far the best I have been able to come up with is:
int create_hist( double input_array[], int count, int output_array[17] ) {
for ( int i = 0; i < count; i++ ) {
input_array[i] = int floor(input_array[i]);
output_array[input_array[i]]++;
However I am getting the following errors which I am having trouble deciphering:
array.c:11:20: error: expected expression before ‘int’
input_array[i] = int floor(input_array[i]);
^
array.c:12:7: error: array subscript is not an integer
hist[input_array[i]]++;
^
array.c:14:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
If someone could let me know where I have gone wrong it would be greatly appreciated.
floatas an array index, and your function is defined as returningintbut never returns anything.int index = input_array[i]will implicitly convert thedoubleto anint. No need for casting, or thefloorfunction.