You already know that angleA is 30 and angleB is 60, and that the sum of all three is 180. Therefore, 180 - (angleA + angleB) is the same as 180 - (30 + 60), or 180 - 90 which gives the result 90.
It then returns angleLeft to angleC in main().
My solution:
[code]#include <stdio.h>
// Function that takes the first two angles
float remainingAngle(angleA, angleB)
{
// calculates what angleLeft actually is.
// We know it is 180 degrees minus the two known angles
float angleLeft = 180.0 - angleA - angleB;
// returns value (angleLeft, which turns out to be 90)
return angleLeft;
}
int main(int argc, const char * argv[])
{
float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA, angleB); // this is where remainingAngle() above kicks in
// The third angle is angleC as set above, with two decimals:
printf("The third angle is %.2f \n", angleC);
return 0;
I don’t see why we would put in a function to begin with?
The program already has what it needs in order to give the printf statement.
So, I simply changed it to:
[code]#include <stdio.h>
int main(int argc, const char * argv[])
{
float angleA = 30.0;
float angleB = 60.0;
float angleC = angleA + angleB;
printf(“the third angle is %.2f\n”, angleC);
return 0;
}[/code]
I guess it’s not the point of the challenge but again, I don’t see the need for a function when the solution is already there. So just wondering why to overcomplicated things
In case you are still looking to know why you are receiving an output of (lldb), it is because you had previously built and run your program with an issue. This inserted a breakpoint at the point where you calculate angleLeft in your function. Look for the breakpoint, right click and delete it and then rebuild. Your output will be as expected.
Can someone explain this following line of code? I don’t recall it being explained in the book anywhere:
I understand printf statements, and i understand that “angleC’s” value is being imported to the printf statement, but i don’t understand the “%.2f\n” bit, specifically. Shouldn’t it read like “%f\n”? Not understanding what the “.2” is for.
Thanks so much! I’d really like to make sure i fully understand this before moving on to chapter 6.
[quote=“mtrieb”]Can someone explain this following line of code? I don’t recall it being explained in the book anywhere:
printf("The third angle is %.2f\n", angleC);
I understand printf statements, and i understand that “angleC’s” value is being imported to the printf statement, but i don’t understand the “%.2f\n” bit, specifically. Shouldn’t it read like “%f\n”? Not understanding what the “.2” is for.
Thanks so much! I’d really like to make sure i fully understand this before moving on to chapter 6.[/quote]
Since this is C code, I will quote K&R and their explanation.
To better grasp this, I highly suggest making a simple program and declaring int, floats, and printing out above formats for visual explanation and understanding.
Your code would not work with other angles, your code only works if angleA and angleB equal 90 degrees returning 90 degrees as the answer. Declare the return type, that is the word “float” in front of the remainingAngle function, if there is no return put “void”. Your variables also need their types.
[code]#include <stdio.h>
float remainingAngle(float a1, float a2) // add types to variables and the return.
{
float a3 = 180 - (a1 + a2); // a triangle is 180 degrees so the remaining angle is 180 - (a1 + a2).
return a3;
}
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;
}
[/code]
Mitch
[quote=“mitchb”]Your code would not work with other angles, your code only works if angleA and angleB equal 90 degrees returning 90 degrees as the answer. Declare the return type, that is the word “float” in front of the remainingAngle function, if there is no return put “void”. Your variables also need their types.
[code]#include <stdio.h>
float remainingAngle(float a1, float a2) // add types to variables and the return.
{
float a3 = 180 - (a1 + a2); // a triangle is 180 degrees so the remaining angle is 180 - (a1 + a2).
return a3;
}
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;
}
[/code]
Mitch[/quote]
I understand about it not working with other angles, but that wasn’t the challenge. “float” for angleA and angleB was already declared inside of the main function and does not need to be declared again. Same thing with the remainingAngle() function, so it neither needs another declaration or void (since I did return a value). Thanks though!
I think there are some concepts here that you have overlooked. Indeed you declared the variables angleA, angleB, and angleC as type floats. The variables a1, a2, and a3 are separate and distinct and also need a type. In the absence of declaring a type many compilers will set the type to integer as a default. The function return is also defaulted to type integer if not declared so void needs to be declared if there is no return. It is always good form to state the type even if the default works so you, the compiler, and others know what your intentions are. Notice even main has integer declared as the return type and the return 0; at the end of main satisfies that. Xcode is looking over your shoulder all the time so if a return type doesn’t match the expected type, Xcode lets you know so you can get a fresh cup of coffee and scratch your head for a few minutes.
The variables angleA, angleB, and angleC will exist as long as main executes, on the other hand a1, a2, and a3 only exist while the function is executing. When the function is called the three variables are created, the values passed, used, and when finished, the function, and it’s variables are destroyed and the memory that they used returned back to the heap.
Here is what worked for me when creating the function the book asks for in the Challenge for Chapter 5 while keeping what was given for int main as it is in the book.
HERE is also my result with some explanation on it!
#include <stdio.h>
//Declare the Function
float remainingAngle (float angleA, float angleB)
{
float result = 180 - angleA - angleB; // it is same as "180 - (angleA+angleB)"
return result; // your calculation gets store in "result" variable
}
int main(int argc, const char * argv[])
{
float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA, angleB); //angleC is calling the "remainingAngle" function and receive that result.
printf("The third angle is %.2f\n", angleC);
return 0;
}