How to transform an array of floats to integer array? [closed]

1

How do I convert an array of floats to an array of integers?

Ex:

float grade{12.30, 15.55, 16.25, 0.00, 3.68}

Output: {12, 15, 16, 0, 3}

    
asked by anonymous 07.10.2017 / 16:44

1 answer

4

To convert float to int, just use cast.

would look like this:

 int main() {

    float grade[5] = {12.30, 15.55, 16.25, 0.00, 3.68};
    int gradenew[5], i;


    for (i = 0; i < 5; i++){
       gradenew[i] =  (int)(grade[i]);
    }

    printf("position[0]: %d",gradenew[0]);

    return 0;
}

note that the position I have printed is already in the integer, that is, you have a new array of integers.

Output:

position[0]: 12
    
07.10.2017 / 17:11