Thread: convert float array to int array

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How would you get the integer part of any of those fractions?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay. One approach is to consider what happens if you say, cast one of these floating point numbers to int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    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. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Thank you. I will work on that.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    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
    Last edited by Shurik; 12-03-2012 at 12:29 PM.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Shurik, corrected check is if (A[i] < (double)INT_MIN || A[i] > (double)INT_MAX)

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert char array to int array
    By redruby147 in forum C Programming
    Replies: 3
    Last Post: 03-25-2009, 11:09 AM
  2. initialisng an array of floats (float* array???)
    By Taper in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 12:45 PM
  3. Replies: 24
    Last Post: 11-11-2008, 01:39 PM
  4. convert char array to int array
    By skeme in forum C Programming
    Replies: 10
    Last Post: 09-07-2008, 10:37 PM
  5. Replies: 8
    Last Post: 07-08-2005, 09:12 AM