Challenge at the end of Functions chatter. Help!

#1

Okay I am brand new at programming, and I’m understanding things pretty well but at this challenge I am stuck. It’s probably a very simple answer but help would be greatly appreciated to help me get on my way!

Challenge
The interior angles of a triangle must add up to 180 degrees. Create a new C Command Line Tool named Triangle.
In main.c, write a function that takes the first two angles and returns the third.
Here’s what it will look like when you call it:
#include <stdio.h>
// Add your new function here
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;
}

The output should be: The third angle is 90.00

Aaron, Hillegass (2011). Objective-C Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides) (Kindle Locations 1024-1032). Big Nerd Ranch Guides. Kindle Edition.

#2

I’m certain you can do this, but if you are stumped, just skip the challenge for now. Come back and do it in a few days.

#3

Okay so I have been messing around and came up with this:

#include <stdio.h>
float remainingAngle (float angle, float number)
{
double realAnswer = 180.0 - angle - number;
printf(“Angle C will be %f.\n”, realAnswer);
}
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;
``````

}

and the output is:

Angle C will be 90.000000.
The third angle is 0.00.

So I know I’m on the right track, but I know I need to do it without the printf() in the remainingAngle function.
advice?

#4

I meant:
float realAnswer

#5

Can you guess, why the printf function in the main loop prints 0?
to give you a hint: try returning another value then 0 in you function (return returns a value, it can be something else then 0).

#6

#include <stdio.h>
float remainingAngle (float angle, float number)
{
float realAnswer = 180.0 - angle - number;
printf(“Angle C will be %f.\n”, realAnswer);
return 90;
}
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;
``````

}

and the output is:

Angle C will be 90.000000.
The third angle is 90.00.

I added a return 90 to the remaining angle function and the output became 90.

is this the correct way?

Thanks for the hint by the way

#7

You are very close, so let me give you the nudge over the edge: return realAnswer.

Nice job.

#8

Just a couple small notes.

Programming is very detailed, so its good to be specific. I don’t mean this in a bad way, I just wanted to point it out because it may be a “gotcha” in the future.

Your function is receiving 2 angles, but you’re calling them “angle” and “number” (technically, they’re both angles and they’re both numbers, so I would stick with the same kind of name for both - angle1/angle2, num1/num2). It would be easier/best to just use the same names that are used in the call. Also, I’m a fan of parenthesis when doing math so that everything happens as it should (plus it makes it easier to read).

``````float remainingAngle(float angleA, float angleB)
{
float realAnswer = 180.0 - (angleA + angleB);
return realAnswer;
}``````

#9

I think I’m still missing something conceptually. Here’s what I got:

``````
float remainingAngle(float angleA, angleB)
{
float angleC = 180 - 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 %.2f\n", angleC);
return 0;
}``````

I get “The third angle is 90.00” message in the debugger. There just seems to be something redundant in this code. The main is calling on the remainingAngle function, which is defining angleC as 90. But when the stack goes back to main (is that the right terminology?) wouldn’t it just read angleC=angleC?

Thanks!

#10

Ah, read the other posts and I think I understand. This would only be a problem if I established angleA, …B, and …C as global or static floats. As it stands, their life as a variable begins and ends with the function they’re in, but the value (parameter?) they pass lives on in main.

#11

Correct, although even a static float would not be visible outside the function in which it’s declared. It would be retained between function calls, but not visible externally.

#12

Okay so a quick question. I have been seeing a lot of

``````float remainingAngle(float angleA, float angleB)
{
float realAnswer = 180.0 - (angleA + angleB);
return realAnswer;
}``````

And is it necessary to have the float in front of angleA and angleB. Because when i do it without it, i still get the right answer but will this cause problems for me in the future if i don’t include the float.

i.e.

``````float remainingAngle(angleA, angleB)
{
float realAnswer = 180 - (angleA + angleB);
return realAnswer;
}``````

#13

It is necessary to give the type. In particular, if you don’t give the type, the arguments of C functions are assumed to be ints. So, your floats are being rounded off before they are used for the calculations. If you use something other than a whole number as the argument, you will get the wrong answer.

#14

I was able to solve this problem using this code (which I came up with having used the hints on this page, and from looking at my previous lessons [It took me awhile because I had used a void function which didn’t seem to work very well and I ended up using a float function after seeing this which brought my whole code back into line)

``````#include <stdio.h>
float remainingAngle(float angleA, float angleB)
{
float angleC = 180 - (angleA + angleB);
return angleC; //return angleC to the Main function after solving
}

int main (int argc, const char * argv[])
{
float angleA = 30.0;
float angleB = 60.5;
float angleC = remainingAngle(angleA, angleB); //call remainingAngle function to solve"
printf("The remaining angle is %f\n", angleC);

return 0;
}``````

However the %f is throwing me for a loop. In the actual lessons most of our examples use a “%” followed by a letter (i’m pretty sure it doesn’t matter what letter is being used?) But in this challenge the printf is shown as using '%.2f" which confuses me, does the .2 have a purpose? My code up top originally used %.2f but I changed it after verifying that it worked. At first I thought maybe it had something to do with decimals but I tried returning a decimal even using just %f and it still worked. . .

Edit: I see now having gotten to the next chapter that the “%” does depend on specific usages (like %d for a base 10 integer and %s for a string), but, the %.2f still puzzles me.

#15

To unpuzzle yourself, open the Terminal Application and enter man -S3 printf and hit and keep reading about the printf function. (Because all this %f-format-descriptor stuff has its origin in the printf function (and its cousins.))

You can also get the same information from Xcode, by entering printf somewhere in your code, option clicking the word printf and then clicking the tiny document icon.

You should see something like this:

If you patiently keep reading and reach the end, you are guaranteed to be an expert

You can even run some experiments on %.f to see how it affects the output:

``````//
//  main.m
//

int main (int argc, const char * argv[])
{
// Print PI
NSLog (@"%.0f", M_PI);
NSLog (@"%.1f", M_PI);
NSLog (@"%.2f", M_PI);
NSLog (@"%.3f", M_PI);
NSLog (@"%.5f", M_PI);
NSLog (@"%f", M_PI);    // Just %f

return 0;
}``````

#16

Here’s what i came up with and i was pretty stoked that it worked the first time.

``````
#include <stdio.h>

//add new function>>
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 %.2f\n", angleC);

return 0;
}[color=#00FF00][/color]``````