Triangle Challenge Cheat?


#1

Like many of you, I am very new to programming. I love this book, but sometimes I wonder if I missed a paragraph or something. It could just be the teaching technique because when I finally figure out the answer, I feel smart and accomplished. Haha.

Anyway, I thought the triangle challenge had a trick in it- specifically the line: "float angleC = remainingAngle(angleA, angleB);
I though that (angleA, angleB) was supposed to be changed to (angleA + angleB), but in the solutions some of you posted, you didn’t do that. Maybe one of you more experienced programmers can take a look at my solution and tell me what you think. In particular if my solution would cause problem down the line with more complex code or something like that.

Thanks!

Here’s what I did:

[code]#include <stdio.h>

float remainingAngle(float twoAngles)
{

float angle = 180 - twoAngles;
return angle;

}

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]


#2

There are no tricks in the challenge.

You missed the important clue in the challenge, but don’t worry this happens when you are learning new things especially programming.

The challenge already provides an important clue about the abstract signature of the function: “… a function that takes the first two angles and returns the third …”

So the function’s abstract signature (aka its prototype) should look like this:
(, );

That is, the function takes two arguments as input and returns a value as its output.

Therefore we can write its concrete signature like this:
float remainingAngle (float, float);

Even better, like this:
typedef float angle_type;
angle_type remainingAngle (angle_type, angle_type);

Therefore:

typedef float angle_type;

angle_type remainingAngle (angle_type A, angle_type B)
{
   return 180 - (A + B);
}

Note: the typedef keyword is covered in Chapter 10. We use typedef declarations to assign new names to types that are already defined. We do this mainly to make our code more readable and easier to maintain.

Have fun with learning to program in Objective-C!


#3

Thanks, ibex.