strange result

Discussion in 'C Programming' started by happytoday, Feb 21, 2009.

  1. happytoday

    happytoday Guest

    Here it is a primitive conversion program from meter to
    yards,inches,feets. Should I get the result of 1 inches when I enter
    0.0245 meter.


    #include "stdio.h"
    #include "stdlib.h"

    void input(float *);
    void convert(float *,int *,int *,int *);
    char another(void);

    float main ()
    {
    int yards,feet,inches;
    float meter;
    do {
    //system("clear");
    input(&meter);
    convert(&meter,&yards,&feet,&inches);
    printf("\n %.4f meter is = %d yards %d feet %d inches
    ",meter,yards,feet,inches);
    } while (another()=='y');
    return 0;
    }


    void input(float *m)
    {
    printf("\nEnter size in meter :");
    scanf("%f",m);
    printf("\nYou have entered size in meter %.4f\n",*m);

    }


    void convert(float *m,int *y,int *f,int *i)
    {
    float remainder=0.0;
    (remainder)=((*m) * 100.0) / 2.54;
    *y=(remainder)/36.0;
    (remainder)=(remainder)-(*y)*36.0;
    *f=(remainder)/12.0;
    *i=(remainder)-(*f)*12.0;
    printf("\n %.4f meter is = %d yards %d feet %d inches ",*m,*y,*f,*i);
    }
    happytoday, Feb 21, 2009
    #1
    1. Advertising

  2. happytoday

    Kojak Guest

    Le Sat, 21 Feb 2009 06:07:43 -0800 (PST),
    happytoday a écrit :

    > Here it is a primitive conversion program from meter to
    > yards,inches,feets. Should I get the result of 1 inches when I enter
    > 0.0245 meter.


    Ouch !

    > #include "stdio.h"
    > #include "stdlib.h"


    Better to write:
    #include <stdio.h>
    #include <stdlib.h>


    > void input(float *);
    > void convert(float *,int *,int *,int *);
    > char another(void);


    Where's defined another?


    > float main ()


    Oops !

    int main (void)

    look better.


    > {
    > int yards,feet,inches;
    > float meter;


    Why float meter and int the others?
    That is, it's your choice...


    > do {
    > //system("clear");
    > input(&meter);
    > convert(&meter,&yards,&feet,&inches);
    > printf("\n %.4f meter is = %d yards %d feet %d inches
    > ",meter,yards,feet,inches);
    > } while (another()=='y');


    Same as previous, what's 'another'
    Some indentation and spacing will help reading code.


    > return 0;


    Hmmm, remember 'float main ()' :)


    > }
    >
    >
    > void input(float *m)
    > {
    > printf("\nEnter size in meter :");


    A flush here in case of...


    > scanf("%f",m);


    It is advisable to check scanf...


    > printf("\nYou have entered size in meter %.4f\n",*m);
    >
    > }
    >
    >
    > void convert(float *m,int *y,int *f,int *i)


    I think it's better to separate calculations here.
    One by function, by example, and in case you want
    int for inch and float for meter:

    int meter2inch(float *);
    float inch2meter(int *);

    and so on...

    > {
    > float remainder=0.0;
    > (remainder)=((*m) * 100.0) / 2.54;
    > *y=(remainder)/36.0;
    > (remainder)=(remainder)-(*y)*36.0;
    > *f=(remainder)/12.0;
    > *i=(remainder)-(*f)*12.0;


    Strange method of conversion ?

    inch = meter / 0,0254 (simply)

    and ditto for the rest.


    > printf("\n %.4f meter is = %d yards %d feet %d inches ",*m,*y,*f,*i);
    > }
    >


    Well, tries to resolve these issues, the rest we will see later
    if necessary...

    That's all for now.

    --
    Jacques.
    Kojak, Feb 21, 2009
    #2
    1. Advertising

  3. happytoday

    Guest

    On 21 Feb, 14:56, Kojak <> wrote:
    > Le Sat, 21 Feb 2009 06:07:43 -0800 (PST),
    > happytoday a écrit :


    <snip>

    > > {
    > > int  yards,feet,inches;
    > > float  meter;

    >
    > Why float meter and int the others?
    > That is, it's your choice...


    because he wants to enter fractions of meter and return whole numbers
    of yards, feet and inches

    <snip>
    , Feb 21, 2009
    #3
  4. happytoday

    Guest

    On 21 Feb, 14:07, happytoday <> wrote:

    <snip>

    > void convert(float *m,int *y,int *f,int *i)
    > {
    > float remainder=0.0;
    > (remainder)=((*m) * 100.0) / 2.54;

    [yes, richard I *did* use a debugger]

    > *y=(remainder)/36.0;
    > (remainder)=(remainder)-(*y)*36.0;
    > *f=(remainder)/12.0;
    > *i=(remainder)-(*f)*12.0;


    you get problems converting from int to float. At this point remainder
    is 0.9646... which when it is assigned to an int yeilds 0. All your
    conversions have a similar problem.

    *i = ROUND (remainer - *f * 12.0)

    where ROUND is
    #define ROUND(X) (ceiling ((X) + 0.5))

    > printf("\n %.4f meter is = %d yards %d feet %d inches ",*m,*y,*f,*i);
    > }


    <snip>

    --
    Nick Keighley
    , Feb 21, 2009
    #4
  5. happytoday

    Guest

    On 21 Feb, 14:07, happytoday <> wrote:

    > Here it is a primitive conversion program from meter to
    > yards,inches,feets. Should I get the result of 1 inches when I enter
    > 0.0245 meter.
    >
    > #include "stdio.h"
    > #include "stdlib.h"
    >
    > void input(float *);
    > void convert(float *,int *,int *,int *);


    if you rearrange your code then you won't need these

    > char another(void);
    >
    > float main ()
    > {
    > int  yards,feet,inches;


    please put some spaces in your code! Also please indent your code
    it makes it far easier to follow.

    > float  meter;


    usually pick double over float. Most calculations are done in double
    anyway
    (my compiler gave loads of warnings because of this) double guarantees
    far
    more accuracy. Modern hardware has no problem with double. floats are
    slightly smaller (in RAM) but this rarely matters.

    > do {
    > //system("clear");
    > input(&meter);
    > convert(&meter,&yards,&feet,&inches);


    why did you pass a pointer to meter?

    > printf("\n %.4f meter is = %d yards %d feet %d inches
    > ",meter,yards,feet,inches);
    >
    > } while (another()=='y');
    > return 0;
    > }
    >
    > void input(float *m)
    > {
    > printf("\nEnter size in meter :");


    fflush(stdin) gurantees your output appears

    > scanf("%f",m);


    you should check the return value of scanf(). scanf() can be
    tricky to use, consider fgets() followed by sscanf()

    > printf("\nYou have entered size in meter %.4f\n",*m);
    >
    > }
    >
    > void convert(float *m,int *y,int *f,int *i)
    > {
    > float remainder=0.0;
    > (remainder)=((*m) * 100.0) / 2.54;


    eek! you do like brackets!

    why not:
    remainder = (*m * 100.0) / 2.54;

    > *y=(remainder)/36.0;
    > (remainder)=(remainder)-(*y)*36.0;
    > *f=(remainder)/12.0;
    > *i=(remainder)-(*f)*12.0;
    > printf("\n %.4f meter is = %d yards %d feet %d inches ",*m,*y,*f,*i);
    > }


    a simpler version might be

    #include <math.h>
    #define ROUND(X) (floor((X) + 0.5))

    void convert (float m, int *y, int *f, int *i)
    {
    *i = ROUND (m * 100 / 2.54);
    *f = *i / 12.0;
    *y = *f / 3.0;
    *f %= 3;
    *i %= 36;
    }

    note my previous version of ROUND was wrong!
    , Feb 21, 2009
    #5
  6. happytoday

    happytoday Guest

    On Feb 21, 6:18 pm, wrote:
    > On 21 Feb, 14:07, happytoday <> wrote:
    >
    > <snip>
    >
    > > void convert(float *m,int *y,int *f,int *i)
    > > {
    > > float remainder=0.0;
    > > (remainder)=((*m) * 100.0) / 2.54;

    >
    > [yes, richard I *did* use a debugger]
    >
    > > *y=(remainder)/36.0;
    > > (remainder)=(remainder)-(*y)*36.0;
    > > *f=(remainder)/12.0;
    > > *i=(remainder)-(*f)*12.0;

    >
    > you get problems converting from int to float. At this point remainder
    > is 0.9646... which when it is assigned to an int yeilds 0. All your
    > conversions have a similar problem.
    >
    > *i = ROUND (remainer - *f * 12.0)
    >
    > where ROUND is
    > #define ROUND(X)  (ceiling ((X) + 0.5))
    >
    > > printf("\n %.4f meter is = %d yards %d feet %d inches ",*m,*y,*f,*i);
    > > }

    >
    > <snip>
    >
    > --
    > Nick Keighley


    I got an error with ceiling function . I compile using sun sudio of
    Sun Solaris 10 . I added math.h but with the same error ?
    Thanks
    happytoday, Feb 21, 2009
    #6
  7. writes:

    > On 21 Feb, 14:07, happytoday <> wrote:
    >
    > <snip>
    >
    >> void convert(float *m,int *y,int *f,int *i)
    >> {
    >> float remainder=0.0;
    >> (remainder)=((*m) * 100.0) / 2.54;

    > [yes, richard I *did* use a debugger]
    >
    >> *y=(remainder)/36.0;
    >> (remainder)=(remainder)-(*y)*36.0;
    >> *f=(remainder)/12.0;
    >> *i=(remainder)-(*f)*12.0;

    >
    > you get problems converting from int to float. At this point remainder
    > is 0.9646... which when it is assigned to an int yeilds 0. All your
    > conversions have a similar problem.


    Well they all have the same property but it is only a problem with the
    inches. Yes, I noticed that you don't suggest using ROUND for the
    others, but I thought it worth making this point.

    > *i = ROUND (remainer - *f * 12.0)
    >
    > where ROUND is
    > #define ROUND(X) (ceiling ((X) + 0.5))


    There is no function ceiling. There is a ceil, but that is not the
    one you want. Having added 0.5 you need floor (or a plain conversion
    to int by assignment). Of course, the OP could just use the round
    function if C99 (or close) is available.

    >> printf("\n %.4f meter is = %d yards %d feet %d inches ",*m,*y,*f,*i);
    >> }

    >
    > <snip>


    --
    Ben.
    Ben Bacarisse, Feb 21, 2009
    #7
  8. happytoday

    Guest

    On 21 Feb, 17:27, happytoday <> wrote:
    > On Feb 21, 6:18 pm, wrote:
    >
    >
    >
    >
    >
    > > On 21 Feb, 14:07, happytoday <> wrote:

    >
    > > <snip>

    >
    > > > void convert(float *m,int *y,int *f,int *i)
    > > > {
    > > > float remainder=0.0;
    > > > (remainder)=((*m) * 100.0) / 2.54;

    >
    > > [yes, richard I *did* use a debugger]

    >
    > > > *y=(remainder)/36.0;
    > > > (remainder)=(remainder)-(*y)*36.0;
    > > > *f=(remainder)/12.0;
    > > > *i=(remainder)-(*f)*12.0;

    >
    > > you get problems converting from int to float. At this point remainder
    > > is 0.9646... which when it is assigned to an int yeilds 0. All your
    > > conversions have a similar problem.

    >
    > > *i = ROUND (remainer - *f * 12.0)

    >
    > > where ROUND is
    > > #define ROUND(X)  (ceiling ((X) + 0.5))

    >
    > > > printf("\n %.4f meter is = %d yards %d feet %d inches ",*m,*y,*f,*i);
    > > > }



    > I got an error with ceiling function . I compile using sun sudio of
    > Sun Solaris 10 . I added math.h but with the same error ?
    > Thanks


    Rule 1: never post untested code!

    ceiling should actually be spelt ceil and it should be floor anyway.

    :-(
    , Feb 21, 2009
    #8
  9. happytoday

    Guest

    On 21 Feb, 18:09, Richard Heathfield <> wrote:
    > said:
    > > On 21 Feb, 14:07, happytoday <> wrote:


    > >> float main ()
    > >> {
    > >> int  yards,feet,inches;

    >
    > > please put some spaces in your code! Also please indent your code
    > > it makes it far easier to follow.

    >
    > >> float  meter;

    >
    > > usually pick double over float.

    >
    > But not when returning a value from main! The main function should
    > return an int, not a float (or double).


    someone else had already noted that

    > >> {
    > >> printf("\nEnter size in meter :");

    >
    > > fflush(stdin) gurantees your output appears

    >
    > IMYM fflush(stdout);


    I'm really not having a good day...
    , Feb 21, 2009
    #9
  10. In article <> happytoday <> writes:
    > Here it is a primitive conversion program from meter to
    > yards,inches,feets. Should I get the result of 1 inches when I enter
    > 0.0245 meter.

    ....
    > void convert(float *m,int *y,int *f,int *i)
    > {
    > float remainder=0.0;
    > (remainder)=((*m) * 100.0) / 2.54;
    > *y=(remainder)/36.0;


    OK, here you use that conversion to integer rounds down.
    y is now equal to 0 and remainder remains the same.

    > (remainder)=(remainder)-(*y)*36.0;
    > *f=(remainder)/12.0;


    Similar comments here.

    > *i=(remainder)-(*f)*12.0;


    And now you suddenly expect that the conversion rounds to nearest?
    If it rounds down in the first two cases it will also here, and the
    result is that i equals 0.
    --
    dik t. winter, cwi, science park 123, 1098 xg amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Feb 23, 2009
    #10
  11. happytoday

    JosephKK Guest

    On Sat, 21 Feb 2009 08:00:44 -0800 (PST),
    wrote:

    >On 21 Feb, 14:56, Kojak <> wrote:
    >> Le Sat, 21 Feb 2009 06:07:43 -0800 (PST),
    >> happytoday a écrit :

    >
    ><snip>
    >
    >> > {
    >> > int  yards,feet,inches;
    >> > float  meter;

    >>
    >> Why float meter and int the others?
    >> That is, it's your choice...

    >
    >because he wants to enter fractions of meter and return whole numbers
    >of yards, feet and inches
    >
    ><snip>


    Figured as much. But it breaks at any fractions of an inch.
    JosephKK, Mar 2, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.Ram
    Replies:
    7
    Views:
    639
  2. Pavel
    Replies:
    7
    Views:
    511
    Pavel
    Sep 19, 2010
  3. Lakshmi Sreekanth

    i = 10; result = ++i - --i; How result become ZERO

    Lakshmi Sreekanth, Sep 21, 2010, in forum: C Programming
    Replies:
    52
    Views:
    1,144
    Nick Keighley
    Sep 23, 2010
  4. Mr. Buffoon
    Replies:
    4
    Views:
    282
    Eric Sosman
    Sep 23, 2010
  5. Michael Tan
    Replies:
    32
    Views:
    932
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page