4

Does anyone know how to turn an integer array into a float array?

3
  • -1: Show us what you have done and tell us what difficulty you are having in your approach? Commented Mar 22, 2013 at 0:49
  • Hi Sorry about the wording - I was rushed when I posted my question. I already had the integer array declared and needed to pass it as an argument to a function that required an array of floats. I see now that I need to create a new array. Thanks for the responses! Commented Mar 22, 2013 at 1:16
  • @yasouser +1...humanity restored. Commented Sep 13, 2021 at 19:34

2 Answers 2

4

Your question is not worded well; however, assuming you have already declared your integer array, you could try something like this:

// instantiate float array    
float fArray[sizeOfIntArray];

// step through each element of integer array, and copy into float array as float   
for(int i = 0; i < sizeOfIntArray; ++i) {
    fArray[i] = (float)iArray[i];
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Armin, i dont understand? what do you mean?
my mistake. i noticed them about as soon as i posted the answer. thanks for the heads up.
1

You cannot convert an already existing array. But, you can do this to get a similar result:

int int_array[10];
float float_array[10];

int I = 0;

for (I=0; I<10; I++) {
      float_array[I] = (float)int_array[I];
}

This copies the current int array to another float array. I've considered the size of the integer array to be 10 You can replace it by the size of your array.

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.