Challenge problem


#1

Hi, after much fiddling I got this to work. I’m still not sure I fully understand why

My version is as follows;

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

//this function calls the function 'remainingAngle’
float remainingAngle(angleA, angleB){

float diff = 180 - angleA - angleB;
return diff;

}

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

// insert code here...

float angleA = 40.0;
float angleB = 60.0;

//this function passes the variables angleA and angleB

float angleC = remainingAngle (angleA, angleB);

   
printf("The third angle is %.2f\n", angleC);


return 0;

This version also works - my question - is there anything wrong with this?

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

//this function calls the function 'remainingAngle’
float remainingAngle(angleA, angleB){

return 180 - angleA - angleB;

}

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

// insert code here...

float angleA = 40.0;
float angleB = 60.0;

//this function passes the variables angleA and angleB

float angleC = remainingAngle (angleA, angleB);

   
printf("The third angle is %.2f\n", angleC);


return 0;

#2

To johnh123abc:

Yes, there are a couple of things wrong with that code that I was able to identify:

1) Add the type “float” before each parameter inside the function i.e: float remainingAngle(float angleA, float angleB)
2) Put angleA and angleB in between parenthesis and replace the minus with a plus sign i.e: return 180 – ( angleA + angleB);
3) The value of angleA is 30.0 not 40.0 i:e: float angleA =30.0;
4) } // Missing the close brace (i.e: squirly bracket) at the end of main.

The correct code should look like this:

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

//this function calls the function 'remainingAngle’
float remainingAngle(float angleA, float angleB)
{

return 180 - (angleA + angleB);

}

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

// insert code here…

float angleA = 30.0;
float angleB = 60.0;

//this function passes the variables angleA and angleB

float angleC = remainingAngle (angleA, angleB);

printf(“The third angle is %.2f\n”, angleC);

return 0;
}[/code]


#3

In the function above, the types of the parameters angleA and angleB are missing.

A parameter without an explicit type is given the default type of int by the language: For example, float remainingAngle (angleA, angleB) is the same as float remainingAngle (int angleA, int angleB).

If you don’t specify the types of the arguments explicitly, when you invoke the function with arguments of type float, information will be lost (that is, 3.143 will become 3.)

Specify the types of its parameters by defining the function like this:

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