![]() |
float pointers
That program does not yield and respond correctly espcially for the
pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51 cm ---1 inch = 2.54/100 Meter ---1 yard = 3 feet ---1 feet = 12 inch ---inches=meter/0.0254 ---feets =meter/0.3048 (0.0254 * 12) ---yards =meter/0.9144 (0.0254 * 36) ---meter=1/0.0254=39.3700 INCH ---meter=1/0.3048=3.28084 FEET ---meter=1/0.9144=1.09361 YARDS ----------------------------------------------*/ #include "stdio.h" /*Module 2 : using pointers in a function to convert Size Entry in meter to sizes of Yards,feet,inches-----*/ main() { float meter; float yds,ft,ins; char another(); system("cls"); do { input_meter(&meter); divide_meter_into(&meter,&yds,&ft,&ins); print_divide_meter_into(&meter,&yds,&ft,&ins); } while (another()=='y'); return 0; } input_meter(float *m) { printf("\nEnter size in meter :"); scanf("%f",&(*m)); printf("\nYou have Entered % f",*m); } divide_meter_into(float *m,float *y,float *f,float *i) { float temp_m=0; temp_m=(*m)*100.0/2.54; (*y)=(temp_m)/36.0; (temp_m)=(temp_m)-(*y)*36.0; (*f)=(temp_m)/12.0; (*i)=(temp_m)-(*f)*12.0; printf("\n Meter %f is = %f yards %f feet %f inches",(*m),(*y),(*f),(*i)); } print_divide_meter_into(float *m,float *y,float *f,float *i) { printf("\n Meter %f is = %f yards %f feet %f inches",(*m),(*y),(*f),(*i)); } char another() { char answer; printf("\n do ou want to procees (y/n)"); scanf("\n"); scanf("%c",&answer); return (answer); } |
Re: float pointers
On 10 Jan 2006 13:34:36 -0800, in comp.lang.c , ehabaziz2001@gmail.com
wrote: Turn up warninglevels in your compiler, as high as they'll go, and fix all the errors it now shows you. >main() int main(). Since 1989, its been considered incorrect to use implicit int. >{ >float meter; >float yds,ft,ins; FYI, most people advise not to use floats for maths, they're too imprecise. >char another(); don't declare functions inside other functions - this isn't supported in C, and serves no useful purpose. >system("cls"); you don't include the header required for system(), so this won't compile. >do { >input_meter(&meter); Your compiler must have complained about this line.... You must declare or prototype the function before you can use it. If you don't do that, then the arguments are "promoted" according to some rules that will cause you problems - floats are promoted to doubles, but input_meter expects floats, so it will get bad data since floats are not doubles.... >divide_meter_into(&meter,&yds,&ft,&ins); >print_divide_meter_into(&meter,&yds,&ft,&ins); same problem for these two... >} while (another()=='y'); >return 0; >} > >input_meter(float *m) again, inplicit int. Don't do that - either return void or return an int. In this case, void is good. void input_meter(float * m) >{ >printf("\nEnter size in meter :"); >scanf("%f",&(*m)); scanf("%f", m); & and * cancel out. So just "m" will work fine (though if you use doubles, you'll want %lf). Mind you, scanf is not a safe function. Try typing in "foo" or just hitting enter. Read the FAQ for further comments on that. >printf("\nYou have Entered % f",*m); >} > >divide_meter_into(float *m,float *y,float *f,float *i) >{ >float temp_m=0; >temp_m=(*m)*100.0/2.54; 100.0 is a double, and your compiler will complain about this since you're converting a double into a float and possibly losing data. Another reason its easier to use doubles. Also the brackets round *m are not needed. Use whitespace if you want to make it clearer >(*y)=(temp_m)/36.0; the brackets.... >(temp_m)=(temp_m)-(*y)*36.0; and more brackets... Oh, and your logic is wrong here - for any y, temp_m==0, not what you want. Work it out on paper. >(*f)=(temp_m)/12.0; brackets... >(*i)=(temp_m)-(*f)*12.0; etc >printf("\n Meter %f is = %f yards %f feet %f inches",(*m),(*y),(*f),(*i)); yet more unneeded brackets.... >print_divide_meter_into(float *m,float *y,float *f,float *i) no need to pass pointers here, you're not modifying what they point to. >{ >printf("\n Meter %f is = %f yards %f feet %f inches",(*m),(*y),(*f),(*i)); again unneccessary brackets Mark McIntyre -- ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =---- |
Re: float pointers
ehabaziz2001@gmail.com wrote:
> That program does not yield and respond correctly espcially for the > pointers (*f),(*i) What does this mean? What did you expect it to do? What did it actually do? We aren't mind readers. Brian -- Please quote enough of the previous message for context. To do so from Google, click "show options" and use the Reply shown in the expanded header. |
Re: float pointers
ehabaziz2001@gmail.com wrote:
> That program does not yield and respond correctly espcially for the > pointers (*f),(*i) > in > print_divide_meter_into(&meter,&yds,&ft,&ins); Please post code that compiles. Yours did not. If I fix that and all warnings: #include "stdio.h" /*Module 2 : using pointers in a function to convert Size Entry in meter to sizes of Yards,feet,inches-----*/ void input_meter(float *m); void divide_meter_into(float *m,float *y,float *f,float *i); void print_divide_meter_into(float *m, float *y, float *f, float *i); char another (void); int main (void) { float meter; float yds,ft,ins; char another(); /* system("cls"); */ do { input_meter(&meter); divide_meter_into(&meter,&yds,&ft,&ins); print_divide_meter_into(&meter,&yds,&ft,&ins); } while (another() == 'y'); return 0; } void input_meter(float *m) { printf("\nEnter size in meter :"); scanf("%f",&(*m)); printf("\nYou have Entered % f",*m); } void divide_meter_into(float *m,float *y,float *f,float *i) { float temp_m=0; temp_m=(*m)*100.0/2.54; (*y)=(temp_m)/36.0; (temp_m)=(temp_m)-(*y)*36.0; (*f)=(temp_m)/12.0; (*i)=(temp_m)-(*f)*12.0; } void print_divide_meter_into(float *m, float *y, float *f, float *i) { printf("\n Meter %f is = %f yards %f feet" " %f inches",(*m),(*y),(*f),(*i)); } char another (void) { char answer; printf("\n do ou want to procees (y/n)"); scanf("\n"); scanf("%c",&answer); return (answer); } Back to the original problem: <snip!> > divide_meter_into(float *m,float *y,float *f,float *i) > { > float temp_m=0; > temp_m=(*m)*100.0/2.54; > (*y)=(temp_m)/36.0; > (temp_m)=(temp_m)-(*y)*36.0; Here, you try to calculate temp_m = temp_m - (temp_m/36.0)*36.0; Guess what. > (*f)=(temp_m)/12.0; > (*i)=(temp_m)-(*f)*12.0; > printf("\n Meter %f is = %f yards %f feet %f > inches",(*m),(*y),(*f),(*i)); > } <snip!> What are you trying to do? If you want integer division, use integers, "/" and "%". The version I think you want: #include "stdio.h" #include "ctype.h" #include "limits.h" #define METER_PER_INCH 0.0254 typedef struct { double meter; struct { double inch; long int yard; unsigned char foot; } imperial; } metric_imp; int input_meter (double *meters); void cleanup_stdin (void); void calculate_yfi (metric_imp *in); void print_metric_imp (metric_imp *in); int do_another (void); int main (void) { metric_imp input = {0}; do { if (input_meter(&input.meter)) { calculate_yfi(&input); print_metric_imp(&input); } cleanup_stdin(); } while (do_another()); return 0; } int input_meter(double *m) { printf("\nEnter size in meter: "); fflush(stdout); if (1 == scanf("%lf", m)) { return 1; } else { fprintf(stderr, "Input error\n"); return 0; } } void cleanup_stdin (void) { int c; while (EOF != (c=getchar())) if (c=='\n') break; } void calculate_yfi (metric_imp *in) { in->imperial.inch = in->meter / METER_PER_INCH; if (in->imperial.inch >= 0 && in->imperial.inch/36 < LONG_MAX) { in->imperial.yard = in->imperial.inch/36; in->imperial.inch -= in->imperial.yard*36.0; in->imperial.foot = in->imperial.inch/12; in->imperial.inch -= in->imperial.foot*12.0; } else { in->imperial.yard = in->imperial.foot = 0; } } void print_metric_imp (metric_imp *in) { printf("\n Meter %g is = %ld yards %d feet" " %g inches\n", in->meter, in->imperial.yard, (int)in->imperial.foot, in->imperial.inch); } int do_another (void) { int answer; printf("\n Do you want to proceed (y/n)? "); fflush(stdout); answer = getchar(); cleanup_stdin(); return EOF != answer && 'y' == tolower(answer); } -- E-Mail: Mine is an /at/ gmx /dot/ de address. |
Re: float pointers
Mark McIntyre wrote On 01/10/06 17:34,: > On 10 Jan 2006 13:34:36 -0800, in comp.lang.c , ehabaziz2001@gmail.com > wrote: > > Turn up warninglevels in your compiler, as high as they'll go, and fix > all the errors it now shows you. > > >>main() > > > int main(). Since 1989, its been considered incorrect to use implicit > int. s/8/9/ or s/correct/advisable/. > >>{ >>float meter; >>float yds,ft,ins; > > > FYI, most people advise not to use floats for maths, they're too > imprecise. "Most people" don't know what they're talking about. Besides, precision is clearly not the O.P.'s biggest worry (contemplate the table of equivalences at the start of his code, and ponder how NASA lost a Mars probe ...) > >>char another(); > > > don't declare functions inside other functions - this isn't supported > in C, and serves no useful purpose. It's perfectly legal to declare (not define) one function inside another, and it serves the useful purpose of providing a declaration. It's certainly not the best way to do things, but it is "supported by C." > >>system("cls"); > > > you don't include the header required for system(), so this won't > compile. Legal under C89 rules. Not advisable, but legal. > >>do { >>input_meter(&meter); > > > Your compiler must have complained about this line.... You must > declare or prototype the function before you can use it. That's the C99 rule, but C89 didn't (or "doesn't," in what I'm guessing about the O.P.'s case) doesn't require it. It's still a good idea to declare before use, but C89 doesn't make it mandatory. > If you don't > do that, then the arguments are "promoted" according to some rules > that will cause you problems - floats are promoted to doubles, but > input_meter expects floats, so it will get bad data since floats are > not doubles.... I was just going to let the whole message slide by until this piece of misleading misinformation caught my eye. Please note that pointers are not subject to promotion, not ever. Since `meter' is a float, `&meter' is a float*, and that's exactly what input_meter() expects to receive. There's no promotion, no bad data, and no error in this line. > >>divide_meter_into(&meter,&yds,&ft,&ins); >>print_divide_meter_into(&meter,&yds,&ft,&ins); > > > same problem for these two... Same non-problem for these two. > >>} while (another()=='y'); >>return 0; >>} >> >>input_meter(float *m) > > > again, inplicit int. Don't do that - either return void or return an > int. In this case, void is good. > > void input_meter(float * m) > > >>{ >>printf("\nEnter size in meter :"); >>scanf("%f",&(*m)); > > > scanf("%f", m); > > & and * cancel out. So just "m" will work fine (though if you use > doubles, you'll want %lf). > > Mind you, scanf is not a safe function. Try typing in "foo" or just > hitting enter. Read the FAQ for further comments on that. > > >>printf("\nYou have Entered % f",*m); >>} >> >>divide_meter_into(float *m,float *y,float *f,float *i) >>{ >>float temp_m=0; >>temp_m=(*m)*100.0/2.54; > > > 100.0 is a double, and your compiler will complain about this since > you're converting a double into a float and possibly losing data. > Another reason its easier to use doubles. Also the brackets round *m > are not needed. Use whitespace if you want to make it clearer Some compilers may complain about converting a double to a float (one even complained about `float f = 0.0;'), but no complaint is required and the language permits the conversion. > [remainder snipped] -- Eric.Sosman@sun.com |
Re: float pointers
Michael Mair wrote:
> #include "stdio.h" > #include "ctype.h" > #include "limits.h" Causes UB. Should be <stdio.h> etc. > typedef struct { > double meter; > struct { > double inch; > long int yard; > unsigned char foot; > } imperial; > } metric_imp; > void calculate_yfi (metric_imp *in); I can't agree with this design choice. In general, you don't want to maintain a value in both metric form and imperial form. You have a value of one or the other, and convert it as need be. Also using only one byte for foot is perhaps questionable (due to overflow risk). I would far prefer: struct imperial { double inch; int foot; long yard; }; void calculate_yfi( double metric, struct imperial *imp ); This also has the advantage that 'metric' cannot be modified by the function. In fact I would personally go for: struct imperial calculate_yfi( double metric) but I could understand if you don't like passing structures by value. > while (EOF != (c=getchar())) > if (c=='\n') > break; Strange that one of the tests is in the while condition and one of them is in the body -- I'd prefer to see either both in the condition or both in the body. > return EOF != answer && 'y' == tolower(answer); tolower(EOF) is defined as returning EOF. So simply: return 'y' == tolower(answer); |
Re: float pointers
Old Wolf wrote:
> Michael Mair wrote: > >>#include "stdio.h" >>#include "ctype.h" >>#include "limits.h" > > Causes UB. Should be <stdio.h> etc. AFAIK, this is implementation defined at best, as after not finding things in implementation defined places, #include "foo" is processed as #include <foo>. However, you are right; I just took the OP's program and modified it. >>typedef struct { >> double meter; >> struct { >> double inch; >> long int yard; >> unsigned char foot; >> } imperial; >>} metric_imp; > > >>void calculate_yfi (metric_imp *in); > > > I can't agree with this design choice. In general, you don't want to > maintain a value in both metric form and imperial form. You have > a value of one or the other, and convert it as need be. You are right. I just typed ahead and changed the OP's programme as I went without giving appropriate consideration to it. I just wanted to show the OP a slightly different way. > Also using only one byte for foot is perhaps questionable (due to > overflow risk). Well, the way I proceed, inch is my base unit and I have at most 2 ft. > I would far prefer: > > struct imperial > { > double inch; > int foot; > long yard; > }; > > void calculate_yfi( double metric, struct imperial *imp ); This is a much better interface. > This also has the advantage that 'metric' cannot be modified > by the function. > > In fact I would personally go for: > > struct imperial calculate_yfi( double metric) > > but I could understand if you don't like passing structures > by value. Hmmm, I am a friend of being able to return the error status. >> while (EOF != (c=getchar())) >> if (c=='\n') >> break; > > Strange that one of the tests is in the while condition and one of > them is in the body -- I'd prefer to see either both in the condition > or both in the body. This is a matter of taste, so I won't discuss it :-) >> return EOF != answer && 'y' == tolower(answer); > > tolower(EOF) is defined as returning EOF. So simply: > > return 'y' == tolower(answer); True; I did not look it up. Thank you for the corrections! Cheers Michael -- E-Mail: Mine is an /at/ gmx /dot/ de address. |
Re: float pointers
On 10 Jan 2006 16:21:00 -0800, in comp.lang.c , "Old Wolf"
<oldwolf@inspire.net.nz> wrote: >Michael Mair wrote: > >> #include "stdio.h" > >Causes UB. Should be <stdio.h> etc. No, the "" notation causes the compiler to search for headers in an implementation defined manner, or if the compiler chooses, identically to <> (6.10.2 p3). Mark McIntyre -- ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =---- |
Re: float pointers
On Tue, 10 Jan 2006 18:02:31 -0500, in comp.lang.c , Eric Sosman
<eric.sosman@sun.com> wrote: (okay, I'll bite) >Mark McIntyre wrote On 01/10/06 17:34,: >> >> int main(). Since 1989, its been considered incorrect to use implicit >> int. > > s/8/9/ or s/correct/advisable/. I disagree. Even though C89 allowed implicit int, its been considered a naughty thing to rely on for decades. Sure, you can do it. You can also toboggan in the nude. >> FYI, most people advise not to use floats for maths, they're too >> imprecise. > > "Most people" don't know what they're talking about. I'm sure I've no need to point you at Goldberg. However if the above is truly your opinion, I can only assume you do very very little maths. However I'm not here to educate you, you're a big boy. :-) >Besides, precision is clearly not the O.P.'s biggest worry >(contemplate the table of equivalences at the start of >his code, and ponder how NASA lost a Mars probe ...) Oh, sure, whch is why it was an FYI. I wanted to save him some pain later on, when he tried some *real* maths. >> don't declare functions inside other functions - this isn't supported >> in C, and serves no useful purpose. > > It's perfectly legal to declare (not define) one function >inside another, my understanding was that it was a GCC extension, but if you can provide me a refrence in the standard, I'm happy to be corrected. But I am also astonished that you encourage the practice by your remarks below. I thought you a better programmer than that. >and it serves the useful purpose of providing >a declaration. It's certainly not the best way to do things, >but it is "supported by C." >> Your compiler must have complained about this line.... You must >> declare or prototype the function before you can use it. > > That's the C99 rule, but C89 didn't (or "doesn't," in >what I'm guessing about the O.P.'s case) doesn't require it. >It's still a good idea to declare before use, but C89 doesn't >make it mandatory. But no C89 compiler will "let it slide" so my point is entirely valid. >this piece of misleading misinformation caught my eye. Please >note that pointers are not subject to promotion, not ever. Yes, my mistake. Stupid of me. >> 100.0 is a double, and your compiler will complain about this >but no complaint is required and the language permits the >conversion. But with a potential loss of data. Which is why compilers complain, no? If you have one that doesn't , I suggest you get your wallet out. Mark McIntyre -- ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =---- |
Re: float pointers
Mark McIntyre wrote:
> "Old Wolf" <oldwolf@inspire.net.nz> wrote: >>Michael Mair wrote: >> >>> #include "stdio.h" >> >>Causes UB. Should be <stdio.h> etc. > > No, the "" notation causes the compiler to search for headers in an > implementation defined manner, or if the compiler chooses, identically > to <> (6.10.2 p3). You're right (as is Michael Mair). In fact, N869 6.10.2 says that if the "" notation does not find a file, then it MUST be reprocessed as if it were the <> notation. (Did this change in the final standard?) However, I suppose it would be legal for someone to create their own "stdio.h" somewhere on the "" search path, though.. |
| All times are GMT. The time now is 06:54 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.