Does anyone know how to turn an integer array into a float array?
-
-1: Show us what you have done and tell us what difficulty you are having in your approach?yasouser– yasouser2013-03-22 00:49:39 +00:00Commented 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!JupiterOrange– JupiterOrange2013-03-22 01:16:17 +00:00Commented Mar 22, 2013 at 1:16
-
@yasouser +1...humanity restored.euraad– euraad2021-09-13 19:34:18 +00:00Commented Sep 13, 2021 at 19:34
Add a comment
|
2 Answers
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];
}
2 Comments
jarrodparkes
@Armin, i dont understand? what do you mean?
jarrodparkes
my mistake. i noticed them about as soon as i posted the answer. thanks for the heads up.
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.