strange result

H

happytoday

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);
}
 
K

Kojak

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>
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.
 
G

Guest

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


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>
 
G

Guest

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>
 
G

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 *);

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!
 
H

happytoday

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>

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
 
B

Ben Bacarisse

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.
 
G

Guest

[yes, richard I *did* use a debugger]
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))

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.

:-(
 
G

Guest

(e-mail address removed) said:



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
IMYM fflush(stdout);

I'm really not having a good day...
 
D

Dik T. Winter

> 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.
 
J

JosephKK

On Sat, 21 Feb 2009 08:00:44 -0800 (PST),
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Error with cc sunstudio compiler 5
float pointers 20
error when rename pointer names 3
I'm new 14
My Status, Ciphertext 2
Problem of the code? 3
SENTINEL CONTROL LOOP WHEN DEALING WITH TWO ARRAYS 1
Newbie question 5

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top