Challenge Question, Left float out in function


#1

This was my solution to the challenge and it worked fine. But I later noticed i left float out of the highlighted line. It did not effect the outcome though. Can it be left out here or will it cause some problem I am not seeing?

#include <stdio.h>
#include <stdlib.h>

[color=#0040FF]float remainingAngle(angleA, angleB)[/color]
{
float answer = 180 - (angleA + angleB);
return answer;
}
int main (int argc, const char * argv[])
{
float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA, angleB);
printf(“The third angle is %.2f\n”, angleC);
return 0;
}


#2

Don’t take this the wrong way: I don’t believe you. Try deleting the line – I’d be very surprised if it works.


#3

I think you misunderstood was I was tying to say. My line says:
float remainingAngle(angleA, angleB)

But according to what I have read so far it should say:
float remainingAngle(float angleA, angleB)

But it seems to work either way.


#4

Ah, I’m sorry. I didn’t read your explanation clearly before doubting it veracity. My mistake.

If you leave the type out, C assumes it is an int. Your floats are being coerced into ints before the function is called.

Here is a fun bug that this creates. Note that I’ve changed angleA and angleB. Now angleC should be 89.

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

float remainingAngle(angleA, angleB)
{
float answer = 180 - (angleA + angleB);
return answer;
}
int main (int argc, const char * argv[])
{
float angleA = 30.5;
float angleB = 60.5;
float angleC = remainingAngle(angleA, angleB);
printf(“The third angle is %.2f\n”, angleC);
return 0;
}[/code]

But, if you run this, you’ll find that angleC is still 90.

Once again, I’m sorry for not taking more time to understand your explanation.


#5

No problem Aaron, Thank you for the quick responses.

I also noticed that I was mistaken in thinking this was how it was supposed to be:

Which left me 89.5 when I changed the angles to 30.5 and 60.5.

I now know that both angles need their type defined like this for it to work correctly:

Again thank you for your quick responses. I am new to programming and am really enjoying your book so far. Having this forum to go along with the book for questions and answers is also of great benefit.

Thanx,
Chuck


#6

Firstly Aaron I would like to say I really love the book. This is the best book so far that has explained C/ objective C programming (although I am on chapter 6 so far). I now get the function challenge question. You have to declare your variables first for the programme to work. A little tip, and this might make not sense, once you have found your answer try to make the solution incorrect. This will help with a better understanding of the principle.
Thank you for your help-
P.S I have your iPhone book on order and look forward to reading that in the future. :stuck_out_tongue:


#7

Firstly I would like to thank you fro bringing this up, I did not declare each individual variable as a float and missed the necessity of it. I originally had:

float remainingAngle (float angleA, angleB) not realizing I needed the to declare angleB as a float as well.

Also, I was just looking at your solution and I noticed that you could have changed

float answer = 180 - (angleA + angleB); return answer;
to

same thing, just one less variable.