2

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.

3
  • You need to use parentheses to cast, you can't use a float as an array index, and your function is defined as returning int but never returns anything. Commented Apr 1, 2018 at 3:46
  • 1
    int index = input_array[i] will implicitly convert the double to an int. No need for casting, or the floor function. Commented Apr 1, 2018 at 3:48
  • Do not forget to accept one of the answers Commented Apr 25, 2021 at 15:24

2 Answers 2

4

Unless you actually want to modify input_array, you would be best off saving the rounded off double in an intermediate variable to then access your integer array. And no need to use floor() casting the double to int will do that.

int create_hist(double input_array[], int count, int output_array[17]) {


    for (int i = 0; i < count; i++) {
        int index = (int)input_array[i];

        if ((index > 16) || (index < 0)) {
            return -1;
        }

        output_array[index]++;
    }

    return 0;
}

Of course, you should really pass in the size of output_array as a variable as well, instead of hard-coding it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that is similar to what I got in the end but I forgot to return 0. It seems to be working now.
It wouldn't hurt to check whether the double values was greater or lesser than INT_MAX and INT_MIN before assigning to the int array. May not be an issue with the value ranges you are working with, but a double can easily overflow and int value.
3

So let get cracking:

First error is due to the fact that you are kind of declaring a function.

input_array[i] = int floor(input_array[i]);

notice int in front of floor, that is not necessary. It should be

input_array[i] = floor(input_array[i]);

Second error is due to the fact that you are accessing array element using double in

output_array[input_array[i]]++;

either you should do it some other way or do following:

output_array[(int) input_array[i]]++;

and the third error is unbalanced parenthesizes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.