My Solution to Challenge (No global / static variables)


#1

Hey guys,

Iā€™m re-reading this book and this is my second attempt at this challenge and so I may have posted an older solution. (I think last time I used global variables - will do a search)

Anyway - here is my reworked solution which I think works quite well. :smiley:

[code]#include <stdio.h>
#include <stdlib.h>

float remainingAngle (float angleA, float angleB)
{

float remainingAngle = 180 - angleA - angleB;

return remainingAngle;

}

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", angleC); 


EXIT_SUCCESS;

}

[/code]

Output is: The third angle is: 90.00

Cheers, Tander


#2

Here is my old topic - I now understand why there was no need for global variables.
viewtopic.php?f=137&t=5079


#3

To simplify the code even further you do not need to assign the expression to a variable. The function only has a single purpose and that is to return the last value of the angle. Just a thought both work but remember it is best to be as lazy as possible when writing code.

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