Funtions and the Degrees


#1

Its amazing how long it can take you to find a error in your programming I have been going thru the code all day since this morning this will probably tell you how new I am to this LOL… but maybe someone else made the same error as I did so I figure Id better post it because I never seen it…

I’ve been getting the wrong output all day;

0.000000 Celsius is 32.000000 Fahrenheit
Water freezes at 0.0000000 degrees Fahrenheit
The Last temperature converted was 0.000000

all because of I repeated a variable wrong and I kept getting a <!> unused variable but I read the code over and over and same and read the forum went back to reading the code and didn’t see the mistake all because I used wrong variable

[code]#include <stdio.h>
#include <stdlib.h>

//Declare a static varible
static float lastTemperature = 50.0;

float fahrenheitFromCelsius(float cel)
{
lastTemperature = cel;
float fahr = cel * 1.8 + 32.0;
printf("%f Celsius is %f Fahrenheit\n", cel, fahr);
return fahr;
}

int main(int argc, const char * argv[])
{
float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(freezeInC);
printf(“Water freezes at %f degrees Fahrenheit\n”, freezeInC); //<----------------The ERROR was here Changed it to freezInF
// and all issues went away really hard debugging when
// everything looked right just he wrong variable
printf(“The Last temperature converted was %f\n”, lastTemperature);

return EXIT_SUCCESS;

}
/code]


#2

I tried putting this code in, but I still did not get it to show as 50.0 instead of 0.00000000 anyone have a suggestion?l

thank you!


#3

It worked when I removed, “lastTemperature = cel;

The issue might be reassigning a variable within the function. I may be totally off base here, but I believe the way the function is put together in the book, you have a global (or static… whichever you prefer), but you also are reassigning the variable within the function.

This is the function I put together and it appears to be working. I could be way off on my understanding with this…

[code]#include <stdio.h>

//Declare a global varible
float lastTemperature = 50.0;

float fahrenheitFromCelsius(float cel)
{
//lastTemperature = cel;
float fahr = cel * 1.8 + 32.0;
printf(" %.2f Celsius is %.2f Fahrenheit\n", cel, fahr);
return fahr;
}

int main(int argc, const char * argv[])
{
float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(freezeInC);
printf(“Water freezes at %.2f degrees Fahrenheit.\n”, freezeInF);
printf(“The last temperature converted was %.2f.\n”, lastTemperature);
return 0;
}[/code]


#4

There are two issues here:

...
float fahrenheitFromCelsius (float cel)
{
    // lastTemperature = cel;
    float fahr = cel * 1.8 + 32.0;
    printf ("Water freezes at %.2f Celsius or %.2f Fahrenheit\n", cel, fahr);
    return fahr;
}
..

(1) The lastTemperature variable is commented out;
(2) A confusing message is printed. The message is confusing because the purpose of the function is to only convert from celsius to fahrenheit, no to print the freezing point of water.

This is how the code should look:

#include <stdio.h>

//Declare a global varible
float lastTemperature = 0;

float fahrenheitFromCelsius (float cel)
{
    lastTemperature = cel;
    float fahr = cel * 1.8 + 32.0;
    return fahr;
}

int main(int argc, const char * argv[])
{
    float freezeInC = 0;
    float freezeInF = fahrenheitFromCelsius (freezeInC);
    printf ("Water freezes at %.2f degrees Fahrenheit.\n", freezeInF);  
    printf ("The last temperature converted is %.2f.\n", lastTemperature);
    
    for (float cel = 0; cel <= 100; cel += 10)
    {
        float fahr = fahrenheitFromCelsius (cel);
        printf ("%6.2f Celsius --- %6.2f Fahrenheit\n", cel, fahr);
    }
    printf ("The last temperature converted is %.2f.\n", lastTemperature);
    return 0;    
}

#5

Yes, I know I intentionally commented that variable out. I wanted to see what would happen.

Also, I changed the wording of the printf function to just play with it, but the results/arithmetic are the same (0C is 32F), regardless of me writing whether it is the freezing point or not. Sorry, I should have mentioned that I altered text in my post to see what would happen, but was trying to stay with the main example provided.

I believe we are following the example in the book, which gives the code verbatim. Someone was just wondering why it wasn’t working as written in the given example. Your code may be totally fine and work great, but it definitely deviates from what we were shown in the text.