# Thread: convert float array to int array

1. ## convert float array to int array

Hello,

I'm new in c programming and cant figure out how, after adding some fractions into one array A, to copy the integer part of these fractions into another array B. Thanks in advance for every advice you can give.

2. How would you get the integer part of any of those fractions?

3. No idea. Тhe assignment is to add some fractions in array A[11] using scanf ( for example 1.3, 5.325, 23.13... ) , display on the screen an array B[11] with elements that are the whole part of these fractions (respectively to the example 1, 5, 23,..) and calculate the average of these new numbers...that's all .

4. Okay. One approach is to consider what happens if you say, cast one of these floating point numbers to int.

5. If you have a double x with the value 9.876 and you want the integer part (9.0), you have two basic choices:
Code:
`y = (int)x;`
or
Code:
`z = floor(x);`
y and z are of appropriate types to hold the stated value (i.e. y must be at least int, z must be at least double). The floor function is declared in the math.h standard header. If it's an array then do the above for each element of the array, e.g. using a for loop.

6. Thank you. I came up with this solution and it seems to work fine.
Code:
```    double A[11];
int B[11];
int i;
float sum=0;

printf("Please enter a values for array A: ");

for (i = 0; i < 11; ++i)
{
scanf("%lf", &A[i]);
}

printf("Values for array B: ");

for (i = 0; i < 11; ++i)
{
B[i]= A[i];
printf(" %d", B[i]);
}
printf("\n");

for (i = 0; i < 11; ++i)
{
sum=sum+B[i];
}

printf("Avr is: %f\n", sum/11);
getch();
}```
It would be nice if you could suggest improvements

7. You should explicitly cast. Also, since you are summing integers, it probably makes more sense for sum to be int. Later, you can print sum / 11.0 to get the double result (or cast sum to double).

Also, I suggest that you used a named constant instead of repeatedly using the magic number 11.

8. Thank you. I will work on that.

9. You can add #include <limits.h> and write:
Code:
```    for (i = 0; i < YOUR_DEFINED_CONSTANT; ++i)
{
if (A[i] < (double)INT_MIN-1 || A[i] > (double)INT_MAX+1)
{
fprintf(stderr, "Value exceeds integer range: %g", A[i]);
B[i]= 0;
} else
B[i]= (int)trunc(A[i]);
printf(" %d", B[i]);
}```
And then try to input too large float values, e.g. 123456789000.0

10. Shurik, corrected check is if (A[i] < (double)INT_MIN || A[i] > (double)INT_MAX)

11. Yes, it's wrong code.
It's must be:
Code:
```    for (i = 0; i < N; ++i)
{
if (A[i] <= (double)INT_MIN-1 || A[i] >= (double)INT_MAX+1)
{
fprintf(stderr, "Value cannot be truncated to integer range: %g\n", A[i]);
B[i]= 0;
} else
B[i]= (int)trunc(A[i]);
printf(" %d", B[i]);
}```
<> are changed to <= >=

INT_MIN-1 and INT_MAX+1 are used because floats in (INT_MIN-1, INT_MIN] and [INT_MAX, INT_MAX+1) will be truncated to INT_MIN and INT_MAX respectively.
For example, -2147483648.9 and 2147483647.6 are truncating to -2147483648 (==INT_MIN) and 2147483647 (==INT_MAX) (for 32-bit int, two's complement).

But better for understanding is:
Code:
```    for (i = 0; i < N; ++i)
{
double a = trunc(A[i]);
if (a < INT_MIN || a > INT_MAX)
{
fprintf(stderr, "Value cannot be truncated to integer range: %g\n", A[i]);
B[i]= 0;
} else
B[i]= (int)a;
printf(" %d", B[i]);
}```
with same functionality.