delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/12/05/22:38:39

Message-ID: <002101bf3f99$c6049b00$0100a8c0@pcpablo>
From: "Pablo M. Dotro" <pyd AT sion DOT com>
To: <djgpp AT delorie DOT com>
References: <82ctj1$i7b$1 AT imsp026 DOT netvigator DOT com>
Subject: Re: Why the value of "c" is always "-17.78"??
Date: Mon, 6 Dec 1999 00:27:03 -0300
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 5.00.2919.6600
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600
Reply-To: djgpp AT delorie DOT com

Hi Jason!

Let's trace the program step by step....
Execution starts in main, where you declare f and c as floats, and i as
integer.
Then you start a loop, controlled by i<=100 (executes 101 times, I think).
Inside it, you call ftc(i), and we now jump to the ftc() function...
ftc takes an integer as parameter, n
after declarin local variables c and f as float and i as int, ypou enter a
loop, to be executed o to n times, with i in increments of one.

Now, inside the loop there are a couple of problems:
1.- even as you will calculate the conversion 0 to n times, you return only
the last value (because c gets re used in each iteration).
If you wanted to store all the values, you would have had to use an array.

2.- c is always 17.78, because f is always 0, so 5.0/9.0*(0-32) is 17.78

I propose the following modification to the program:

1.- make the ftc function to do only one conversion, so it should look like:

float ftc(float f){
    float c;
    c = (5.0/9.0) * (f-32);

    return(c);
}

2.- If your intention is to show a temperature conversion table, from 0 to
100 °F, you could re-write main() as

int main(){

    for(i=0; i<=100; i++);
        printf("\t\tF= %.2f\t\t C=%.2f  \n",(float) i, ftc((float) i));

    return(0);
}

Remember to include stdio.h, and anothe good trick would be (if you need a
couple of extra decimal digits of accurancy) to change all the float data
types by doubles...
I hope this had helped you!
Contact me if you need more help!
Best regards from Argentina,

Pablo M. Dotro
pyd AT sion DOT com                pdotro AT USSEnterprise DOT com
pdotro AT labs DOT df DOT uba DOT ar     ICQ#: 18144918
http://www.usuarios.sion.com/abismo

----- BEGIN GEEK CODE BLOCK -----
Version 3.12
GCS/S/IT/CM d- s: a23 c++$ UL P+ E W++ N++ o K- w
O? M V? PS PE- Y+ PGP>+ t++ 5+ X+ R+>+++ tv b++ DI? D++
G++ e h! r- y+
-----END GEEK CODE BLOCK -----
(para decodificar el bloque, visite www.geekcode.com)



----- Original Message -----
From: "Jason Yip" <manman AT netteens DOT net>
Newsgroups: comp.os.msdos.djgpp
To: <djgpp AT delorie DOT com>
Sent: Sunday, December 05, 1999 2:33 AM
Subject: Why the value of "c" is always "-17.78"??


> Can anyone tells me why the value of "c" is always -17.78??
> Thank a lot!!
>
> #include <stdio.h>
> float ftc(n)
> int n;
> {
> float c, f; int i;
>  for (i=0;i<=n;i++){
>  c=5.0/9.0*(f-32);
>         }
> return c;
> }
> main ()
> {
> float c, f=0; int i=0;
> do
> {
> c=ftc(i);
> printf("\t\t F=%.2f \t\t C=%.2f\n",f,c);
> f++;
> i++;
> } while (i<=100);
> }
>
>
>


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019