Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strange result

Reply
Thread Tools

strange result

 
 
happytoday
Guest
Posts: n/a
 
      02-21-2009
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);
}

 
Reply With Quote
 
 
 
 
Kojak
Guest
Posts: n/a
 
      02-21-2009
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.

 
Reply With Quote
 
 
 
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      02-21-2009
On 21 Feb, 14:56, Kojak <(E-Mail Removed)> 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>

 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      02-21-2009
On 21 Feb, 14:07, happytoday <(E-Mail Removed)> 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
 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      02-21-2009
On 21 Feb, 14:07, happytoday <(E-Mail Removed)> 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!



 
Reply With Quote
 
happytoday
Guest
Posts: n/a
 
      02-21-2009
On Feb 21, 6:18*pm, (E-Mail Removed) wrote:
> On 21 Feb, 14:07, happytoday <(E-Mail Removed)> 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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-21-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> On 21 Feb, 14:07, happytoday <(E-Mail Removed)> 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.
 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      02-21-2009
On 21 Feb, 17:27, happytoday <(E-Mail Removed)> wrote:
> On Feb 21, 6:18*pm, (E-Mail Removed) wrote:
>
>
>
>
>
> > On 21 Feb, 14:07, happytoday <(E-Mail Removed)> 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.


 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      02-21-2009
On 21 Feb, 18:09, Richard Heathfield <(E-Mail Removed)> wrote:
> (E-Mail Removed) said:
> > On 21 Feb, 14:07, happytoday <(E-Mail Removed)> 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...
 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      02-23-2009
In article <(E-Mail Removed)> happytoday <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i = 10; result = ++i - --i; How result become ZERO Lakshmi Sreekanth C Programming 52 09-23-2010 07:41 AM
Re: i = 10; result = ++i - --i; How result become ZERO Mr. Buffoon C Programming 4 09-23-2010 03:01 AM
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM



Advertisments