Challenge Question?


#1

I completed the challenge this way, and got the correct answer to output. Did I do it wrong?

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

float angleA = 60.0;
float angleB = 30.0;

//New Function here

float remainingAngle (float angleC){
float totalTwoSides = angleA + angleB;
angleC = 180 - totalTwoSides;
return angleC;
}

int main(int argc, const char * argv[])
{

// insert code here...
float totalTwoSides = angleA + angleB;
float angleC = remainingAngle(totalTwoSides);
printf("The remaining angle is %.2f degrees. \n", angleC);

return 0;

}

As someone who is wanting to learn development and programming for the iphone/ipad, this books is really giving me the fundamentals. Let me know how I could make my program better. Thanks

Stan


#2

Hi Stan,

I’m a newbie too and went through your code. It seems like you’re doing the same things with angleA and angleB inside AND outside your function.

I just finished this challenge too and came up with something different. Don’t know if it’s right or wrong but I got the answer as well. See if it helps you:

#include <stdio.h>

float remainingAngle (float firstAngle, float secondAngle)
{
float thirdAngle = 180 - (firstAngle + secondAngle);
return thirdAngle;
}

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 %f\n", angleC);

return 0;

}

Best.

Amit


#3

Yours does make better sense. And yes I am putting it inside and outside the function. I got the answer, but it was extra coding. I’m still learning and it’s difficult trying to remember some of this when you only get a few hours each night. I need to do better in making time.

Stan


#4

Hi guys,
[color=#BF0000]Update: :ugeek: I reread and reviewed functions again and now I understand what I asked below.[/color]
angleA, angleB are passed to the function which then runs and returns the result as the return of the function and thus the value of angleC.

I am having a brain freeze and have reread the Functions chapter over and over.
Please help:

float angleC = remainingAngle(angleA, angleB);

I understand that remainingAngle is the function, but I am not understanding the (angleA, angleB) part.

Thanks in advance.

zeighty


#5

@Zeighty

I am also new to this, but if you haven’t found resolution to your question I believe I can answer:

float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA, angleB);

The (angleA, angleB); portion of code is inputing the values that you declared (30.0, 60.0) into the function remainingAngle(angleA,angleB); essentially, if you replace (angleA, angleB); with (30.0,60.0); you should receive the same output of 90.

Hope that helps, and if it does, I’m sure you (or somebody) will be returning the favor soon for me :wink:


#6

Hi,

I just did the challenge and solved it this way:

float remainingAngle(angleA, angleB)
{
float angleC = 180.00-(angleA+angleB);
return angleC;
}

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 %.f\n”, angleC);
return 0;
}

One thing I don’t understand up to now. It’s the line “float angleC = 180.00-(angleA+angleB);” If I use the brackets the result is 90, but when I left them out, the result was 210. :confused:

In my opinion there should be no difference if I calculate 180-30+60 or 180-(30+60). In both cases it is 90.

Any suggestions? :wink:

Thanx
Marco


#7

If an argument’s type is not specified, it defaults to int.


#8

@ibex10: Thanx, I just changed my code. :slight_smile:

And the problem is no problem anymore. I was a little confused. :smiley:


#9

[quote=“MarcoR”]

One thing I don’t understand up to now. It’s the line “float angleC = 180.00-(angleA+angleB);” If I use the brackets the result is 90, but when I left them out, the result was 210. :confused:

In my opinion there should be no difference if I calculate 180-30+60 or 180-(30+60). In both cases it is 90.

Any suggestions? :wink:

Thanx
Marco[/quote]

This is my first attempt at any programming language so here’s fingers crossed for the future. What I’ve learned so far I think I can help you with. In programming Multiplication * and Division / take priority over Addition + and Subtraction - If you want the math format you add parenthesis ( ) so 180 - 30 + 60 = 210 whereas 30 + 60 = 90 now 180 - 90 = 90 We did the maths in the ( ) first. I hope this makes sense.


#10

Operator precedence does come into play here a little. Addition(+) and subtraction(-) have the same precedence, therefore, the math operation is performed from left-to-right.

Thus, if you perform the operation 180 - 30 + 60 without parentheses, the 180 - 30 operation occurs first, as in: 180 - 30 + 60 = 150 + 60 = 210.

The operation 180 - ( 30 + 60 ) with parentheses, the 30 + 60 operation occurs first, as in: 180 - 90 = 90.