 # Challenge

can i see what the answer looks like for this one?

I’ll give you a hint.

or

```double x; x = sin(1);```
let me know if you need any more help.

Ok so that first line of code is how I came about my answer to the challenge.
My question is, in the function `double sin(double x);` what does x represent? I effectively ran the code by removing the x.

To the best of my limited knowledge x isn’t being declared unless you opt for the second option given, which I wrote as `double x = sin(1);`

Would someone be kind to help me out here. I guess I just don’t understand the reason for the x in the function.

Thanks,
Chris

[quote=“AtomicSpaceTone”]Ok so that first line of code is how I came about my answer to the challenge.
My question is, in the function `double sin(double x);` what does x represent? I effectively ran the code by removing the x.

To the best of my limited knowledge x isn’t being declared unless you opt for the second option given, which I wrote as `double x = sin(1);`

Would someone be kind to help me out here. I guess I just don’t understand the reason for the x in the function.

Thanks,
Chris[/quote]

Ok…I will try to answer…
double sin (double x) -> This is a function, first part is telling us that will return a double, second part “(double x)” -> how many arguments it takes, in this case 1. so “x” can be whatever you want"y,d,h," etc "x " is there to let you know that you function expect you to enter an argument , in this case a double which happens to be named “x”, but you will replace this “x” with a real number.
for i.e.

[code]double sin(double x);// This is a function that takes one argument "(double x) "

``````double radiantOf1 = sin(1); // Create a variable that use the "sin function" to calculate and store the result -> You can see that "x" is replace with number 1.

printf("Your answer is: %.3f\n" ,radiantOf1); //Print out the result with 3 numbers after "."

return 0;[/code]``````

x is what they call a local variable. This means its only purpose to hold a value. The value of the local variable changes based upon the function its used in.

//Does this look alright to you guys?

#include <stdio.h>
#include <math.h>

double sin(double x);

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

``````// insert code here...
``````

double x = sin(1);

``````printf("The answer is %.3f\n", sin(x));

return 0;
``````

}

I have a question. I try to reuse the calcul from the challenge and turning it for a calcul I need to do. Sorry if it is of topic.

I would like to calculate the cosinus of a number and use this number with other simple calculation.
That is what I want to calculate.
I need the cos of the angle between my track and the wind. the answer has to be multiply by the wind speed and add the actual speed of my plane to the answer.
cos (30)= 0.86 *20= 17kts

17-100=117kts

how in “double x = cos(1)” can I replace 1 by 30?

thank you for your help

[quote]how in “double x = cos(1)” can I replace 1 by 30?
[/quote]
cos and sin functions in the math library want their arguments to be angles in radians. Therefore, you need to convert an angle in degrees to an angle in radians.

Look at the following example:

``````#include <math.h>

// Cosine of an angle in degrees
double MyCos (double degrees)
{
return cos ((M_PI/180) * degrees);
}

// Sine of an angle in degrees
double MySin (double degrees)
{
return sin ((M_PI/180) * degrees);
}

int main (int argc, const char * argv[])
{
double angle = 45; // degrees
printf ("cos %.3f = %.3f\n", angle, MyCos (angle));
printf ("sin %.3f = %.3f\n", angle, MySin (angle));

return 0;
}``````

[Accelerate your learning and become a competent programmer: pretty-function.org]

Thank you!

Hi there,

I wanted to know if the code

really is necessary is this case, and what its purpose is.

I just wrote

```double radiantOf1 = sin(1); printf("The sine of 1, in radians, is %.3f\n\n", radiantOf1);```
which works…

Thanks in advance.

N.C.

[quote] wanted to know if the code

really is necessary is this case, and what its purpose is.
[/quote]
Yes, it is. It is called a function declaration, which tells the compiler that sin is a function which takes an argument of double and returns a value of double.

Without that declaration, you will receive at least a warning from the compiler.

[quote] I just wrote

```double radianOf1 = sin(1); printf ("The sine of 1, in radians, is %.3f\n\n", radianOf1); ```
which works…[/quote]
That’s because either you have included a header file which includes the declaration of sin or Xcode is including the declaration for you behind the scenes.

If you are curious, create the following program:

[code]
// main.m

int main (int argc, const char * argv[])
{
double radianOf1 = sin(1);
printf (“The sine of 1, in radians, is %.3f\n\n”, radianOf1);

``````return 0;
``````

}
[/code]and compile after commenting out the contents of the *-Prefix.pch header file in your project to see the compilation warnings you will get.

Oh I understand! Thanks!

I had indeed imported math.h, which is why the sin function was working then, I guess.

Cheers,

NC

I solved this but all of the other solutions approached it differently and wanted to know if I am doing it right?

``````
#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
float x = 1;
double sin(double x);
printf("The sin of 1 radians is %.3f\n", sin(x));
return 0;
}
``````

The following is logged to the console:

``````The sin of 1 radians is 0.841
Program ended with exit code: 0``````

However I just would like to know if there is a reason I should be doing it like the other examples?

Thank You!

Perfect.

Try writing a function that takes an angle in degrees and returns its sine.

What I don’t understand is how float x = 1 yields the correct answer whereas assigning the integer 57.2958, which is the value of ONE radian, to float x does NOT yield the correct result.

Do you mean to say you are expecting to see the same result for both x = 1 and x = 57.2958?

``````double sin (double x);

float x = 1;
printf ("The sin of 1 radians is %.3f\n", sin (x));

float x = 57.2958;
printf ("The sin of %.4f radians is %.3f\n", x, sin (x));``````

1 radian equals 57.2958 degrees - correct.

But sin function expects its argument to be specified in radians not in degrees!

Do you mean to say you are expecting to see the same result for both x = 1 and x = 57.2958?

``````double sin (double x);

float x = 1;
printf ("The sin of 1 radians is %.3f\n", sin (x));

float x = 57.2958;
printf ("The sin of %.4f radians is %.3f\n", x, sin (x));``````

1 radian equals 57.2958 degrees - correct.

But sin function expects its argument to be specified in radians not in degrees![/quote]

Excellent! Thanks for the reply. My next question is, how does the function know that it’s dealing with radians? My code is nearly exactly what you posted.

It doesn’t know; it just assumes that its input is in radians.

Can the challenge be done like this :-

int main(int argc, const char * argv[])
{
double x = 0.841;
printf(" sine of radian 1 is %.3f\n", x);
}

if not. why not ?

What is the book asking you to do?