How to transfer an array of floats to an array of integers?

Asked

Viewed 85 times

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}

  • Do you want to convert in the statement, in the view, create a new array or what? It has many possible interpretations for your question, it would be good to delimit better what you want to get. Remember that you can [Dit] your post to give more details.

  • 1

    Explain better by clicking on [Edit] in your post. Comments are ephemeral (and often unread), what matters to solving the problem should be part of the original question, with as much detail as possible so you can get an answer that really does what you expect.

  • – Implement a Function void Frequencies(float *grades, int n, int *freq) that receives the address of and array grades with the Students' Exam grades (a float value between 0.0 and 20.0), the number of Elements in that array (n), and the address of a Second array (freq) to be filled with the Absolute Frequency of the integer part of the grades stored in the array grades. Use Pointer arithmetic to Solve this Exercise.

  • Example: • the array grades with content {8.23, 12.25, 16.45, 12.45, 10.05, 6.45, 14.45, 0.0, 12.67, 16.23, 18.75} should Produce a freq array with {1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0}

  • 1

    Add this translated information to the question, so that it is clear to everyone the goal

1 answer

4

To convert float to int, just use the 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 integer, i.e., you have a new array of integers.

Exit:

position[0]: 12
  • 1

    Recalling that the cast for int truncate the floating part. In certain cases, it is desirable that the float is rounded up, is the case of 3.68 and 15.55, which in the case were rounded to an integer "farther".

  • @Lacobus Although the desirable/common is in fact round off in the conversion of float for int the output shown in the question suggests truncating. So I would say it is in agreement with the question.

  • @Bacco I agree that the question is not at all clear, and I did not vote to reopen it. I was merely suggesting that the answer gives the output expected according to question, though not clear. Soon I was just countering what Lacobus had said (which should be rounded instead of truncated).

  • @Of course, I think your remark is valid. I only mentioned why the comments in the question sometimes go unnoticed (and change the whole situation), but then I noticed that you commented at the end, suggesting the author to update the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.