Thread: float/int numbers understanding.....

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    20

    float/int numbers understanding.....

    im trying to write a temp conv. program from fahr to cel. by using a function , it works but im trying to make it print a number after the decimal and it gets all messed up...
    Code:
    #include <stdio.h>
    
    #define NEXTSTEP 20
    
    
    int convertingfunction(int, int);
    
    int main(void)
    {
         int c;
         int i;
         
         for(c = 0; c <= 300; c = c + 20)
             printf("%d %3d\n", c, convertingfunction(c,i));
         return 0;
    }
    int convertingfunction(int whzzup, int hehehe)
    {
        int d;
        int c;
        d = 0;
    
    
        d = (5.0/9.0) * (whzzup-32);
        return d;
    }
    this works all right but how do u get it to print numbers after the decimal? i tried changing d into a float as well as c but they all come out messed up.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Try changing more than just d to a float, like perhaps the return value.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Even your printf is displaying and integer.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    20
    no i meant i tried turning everything into float, the printf % things and the variables like c, i, and d.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by i_can_do_this
    no i meant i tried turning everything into float, the printf % things and the variables like c, i, and d.
    funny. if I do that I get this output
    Code:
    0.000000 -17.777779
    20.000000 -6.666667
    40.000000 4.444445
    60.000000 15.555555
    80.000000 26.666666
    100.000000 37.777779
    120.000000 48.888889
    140.000000 60.000000
    160.000000 71.111115
    180.000000 82.222221
    200.000000 93.333336
    220.000000 104.444443
    240.000000 115.555557
    260.000000 126.666664
    280.000000 137.777771
    300.000000 148.888885
    Kurt

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    this works all right but how do u get it to print numbers after the decimal? i tried changing d into a float as well as c but they all come out messed up.
    is this what u wanted. for instance take some floating point number
    37.777779
    u want to get the 37 separate and the .777779 separate, if i understand u well. to do that...

    Code:
    #include<stdio.h>
    
    int main()
    {
        double num=37.777779;
        int beforedecimal;
        double afterdecimal;
        
        beforedecimal = (int) num;
        afterdecimal = num - beforedecimal;
        
        printf("Number before the decimal point ->> %d\n",beforedecimal);
        printf("Number after the decimal point ->> %f\n",afterdecimal);
        
        getchar();
        
        return 0;
    }
    /*My ouptut
    Number before the decimal point ->> 37
    Number after the decimal point ->> 0.777779
    */
    hope this is what u wanted

    ssharish2005
    Last edited by ssharish2005; 07-15-2006 at 09:11 AM.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    20
    sorry but i realized that simply turning all the variables to floats wasnt enough, i need it to turn the function return type and parameters to floats too.... and ssharish nope thats not what i was looking for.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    ssharish2005, there is a function called modf() in <math.h> that does what your code does.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matching numbers
    By kirksson in forum C Programming
    Replies: 7
    Last Post: 07-23-2008, 01:51 PM
  2. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM