Thread: Simple problem, at least I think it should be simple

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    44

    Simple problem, at least I think it should be simple

    It's been a long time since I have written code and need a bit of help.
    If you have two number x and y, and you want x to be the whole number and y to be the decimal, x.y, how would you code that in C?
    Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In general, you should look at floor().

    In the case that your number could be negative, you're going to have to decide what you mean by the whole number: is -3.8 a whole number of -3 and a decimal of -0.8, or a whole number of -4 and a decimal of 0.2? (One of those is consistent with the positive case, and it probably isn't the one you were planning on.)

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    My issue is this:

    Code:
    int convert ( int x, int y)
    {
      int result;
    
     // result = x.y; <-- this is what I need to compute
    
     return result;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    int can only hold an integral value, meaning no decimal. When you say you want x.y does that mean that if x = 3 and y = 25, that the result needs to be 3.25? Since y is an int, it can't hold 0.25 itself.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    Quote Originally Posted by tabstop View Post
    int can only hold an integral value, meaning no decimal. When you say you want x.y does that mean that if x = 3 and y = 25, that the result needs to be 3.25? Since y is an int, it can't hold 0.25 itself.
    Sorry the type is not an issue, and that was my mistake. They call be floats. The basic jist is I need to calculate the result of taking two numbers x and y and making the result x.y.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well if x is a whole number and y is a decimal already, then x.y just means x+y. (I.e. if x is 3 and y .25, then you get 3.25 by doing x+y.)

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    Code:
    #include <stdio.h>
    #include <math.h>
     
    double convert( int x, int y )
    {
        double d = y;
        while( d > 1.0 )
            d /= 10.0;
        return x + d;
    }
     
    double convert2( int x, int y )
    {
        return x + y / pow( 10, floor( log10( y ) + 1 ) );
    }
     
    double convert3( int x, int y )
    {
        char s[ 100 ];
        sprintf( s, "%d.%d", x, y );
        double d = 0.0;
        sscanf( s, "%lf", &d );
        return d;
    }
     
    int main()
    {
        printf( "%f\n", convert( 1, 23 ) );
        printf( "%f\n", convert2( 1, 23 ) );
        printf( "%f\n", convert3( 1, 23 ) );
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    Thank you, that was very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Replies: 1
    Last Post: 07-20-2002, 06:33 PM

Tags for this Thread