Challenge?


#1

Hi!
I put the following solution however the output is not as expected. Build is successful.

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

float remainingAngle(angleA, angleB)
{
float angleLeft = 180.0-(angleA + angleB);
return angleLeft;
}
int main(int argc, const char * argv[])
{
float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA, angleB);
printf(“The 3rd angle is = %.2f \n”, angleC);
return 0;
}

Output is
(lldb)

Debugger Window
angleB int 60 60
angleA int 30 30
angleLeft float 0.000000000000000000000000000000000000000045916347 0.000000000000000000000000000000000000000045916347


#2

I’ve just copied your code and pasted it into Xcode and it’s worked fine, giving

“The 3rd angle is = 90.00
Program ended with exit code: 0”

Sorry, can’t help any more than that, but your code works for me.


#3

Did you get this figured out? Have you checked your breakpoints?


#4

I don’t understand this part:

float remainingAngle(angleA, angleB)

{
float angleLeft = 180 - (angleA + angleB);
return angleLeft;
}

What’s going on with the ‘angleLeft’ part, and how does that translate the correct number to float angleC = remainingAngle(angleA, angleB);


#5

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;

}[/code]


#6

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 :stuck_out_tongue:


#7

mkumar -

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.

I hope this is helpful.


#8

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.


#9

the “.2” part sets the number of decimals. Try it yourself with .3, .4 et cetera.


#10

[quote=“mtrieb”]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]

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.


#11

What I came up with:

[code]#include <stdio.h>

remainingAngle(a1,a2){
int a3 = 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]

The third angle is 90.00
Program ended with exit code: 0

Yay! whew :slight_smile:


#12

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


#13

[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. :slight_smile: “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!


#14

mitchb corrected your code, but you seem to be paying no heed.

What do the following function declarations mean?

float FooBar (float foo, float bar);  // This is easy
FooBar (foo, bar);  // What is the type of return value? And the types of parameters?

#15

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.

Mitch


#16

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.

//Add your new function here

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


#17

Here’s what worked for me:

#include <stdio.h>

// Add your new function here

float remainingAngle(float angA, float angB)
{
    return 180 - (angA + angB);
}


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;
}

#18

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;
}

#19

Instead of ‘angleLeft’, can I just use ‘angleC’ like below?

#include <stdio.h>

int remainingAngle(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;
}

#20

Hello, I input the following and got the correct output.
#include <stdio.h>
#include <unistd.h>

float remainingAngle(angleLeft, angleRight)
{
float formula = 180 - (angleLeft + angleRight);
return formula;

}

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

// insert code here...
float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA, angleB);
printf("The third angle is %.2f\n", angleC);

return 0;

}