About the challenge problem


#1

I spent like 50 minutes on this problem and really wasnt even close. I just got the solution from looking on here and didnt really understand a lot of it. What should I do to make sure I grasp it? Should I reread the earlier chapters or something?


#2

I don’t think it would hurt to reread this chapter. But, you don’t need to understand it deeply to keep moving. The ideas will be used many times, and you’ll get them eventually.


#3

I actually ended up getting it eventually, through a combination of this forum and playing around with code. I had trouble understanding the passing of values. I pretty much am 70% with it now. Im going to review later. Would you recommend skipping things in the future if you arent 100% on them then, or just sitting there until you learn?


#4

I actually do have one more question.

[code]float fahrenheitFromCelsius(float cel)
{
float fahr = cel * 10;
return fahr;
}

int main (int argc, const char * argv[])
{
float freezeInC = -1000;
float freezeInF = fahrenheitFromCelsius(-freezeInC);
printf(“Water freezes at %f degrees Fahreheit\n”, freezeInF);

return EXIT_SUCCESS;[/code]

For this code, when you float cel outside main, how does cel know to take the value of freezeInC?


#5

Actually what is been declared is the local variable fahr, not cel.
cel is the parameter of the function, and it is also a local variable.
When the function fahrenheitFromCelsius is called in the main program, the argument used is freezeInC, therefore when the function runs, cel obtains its value from the the argument used when the function was called.
Maybe it will be a good idea for you to go back to page 32 on the book and read it again.
I struggled with the Beer Bottles example and after somedays I went back and read it a couple of times, and checking the forums afterwards helped me understand it.

I was not aware that using a minus sign inside the parenthesis can change the sign the value, thanks for that.


#6

What confuses me is cel. I dont quite understand why it has the value it does. By playing around with the problems I can easily see what affects what, but I do not entirely grasp it.

So is this line in the main program
float freezeInF = fahrenheitFromCelsius(-freezeInC);

Saying that freezeInF is equal to the value of fahrenheitFromCelsius multiplied by the negative value of FreezeInC?
If so, I think I understand what was confusing me.


#7

Nope. Let’s break it down.

First, let’s start with the signature of the function fahrenheitFromCelsius.

float fahrenheitFromCelsius(float cel)

This tells us that it takes one parameter, a floating point number, and will return a floating point number.

Inside the function, the parameter will be referred to by the string cel, but that’s beside the point for the moment.

So we return to the invocation of fahrenheitFromCelsius.

    float freezeInC = -1000;
    float freezeInF = fahrenheitFromCelsius(-freezeInC);

The variable freezeInC has a value of -1000, so let’s turn the above into a single line of code:

    float freezeInF = fahrenheitFromCelsius(-(-1000));

The presence of the extra minus sign confuses things a bit, unfortunately, but we’re taking a negative number and negating its value, resulting in a positive value, so the function call is really this:

    float freezeInF = fahrenheitFromCelsius(1000);

This code now says, in English:

Please invoke the function fahrenheitFromCelsius with one value, the number 1000. Whatever calculations it performs, it will return a number, and that number should be stored in the variable named freezeInF.


#8

Awesome. I saved the explanation in Evernote so I can consult if when (if) I forget. That really helped though.