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.
This is a discussion on convert float array to int array within the C Programming forums, part of the General Programming Boards category; Hello, I'm new in c programming and cant figure out how, after adding some fractions into one array A, to ...
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.
How would you get the integer part of any of those fractions?
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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 .
Okay. One approach is to consider what happens if you say, cast one of these floating point numbers to int.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
If you have a double x with the value 9.876 and you want the integer part (9.0), you have two basic choices:
orCode:y = (int)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.Code:z = floor(x);
Thank you. I came up with this solution and it seems to work fine.
It would be nice if you could suggest improvementsCode: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(); }
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Thank you. I will work on that.
You can add #include <limits.h> and write:
And then try to input too large float values, e.g. 123456789000.0Code: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]); }
Last edited by Shurik; 12-03-2012 at 12:29 PM.
Shurik, corrected check is if (A[i] < (double)INT_MIN || A[i] > (double)INT_MAX)
Yes, it's wrong code.
It's must be:
<> are changed to <= >=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]); }
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:
with same functionality.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]); }