 # Chapter 7 challenge solution

I hardly know any math, but I put this together by looking at other suggestions. It feels a bit roundabout, although I can’t put my finger on it. At least it gets the desired result.

[code]#include <stdio.h>

// Including math library
#include <math.h>

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

``````// Declaring x to be 1.0

double x = (1.0);

// Declaring variable "result" to be sin(x).
double result = sin(x);

// Prints the variable "result" with 3 decimals.
printf("The sine of 1 radian is %.3f.\n", result);
return 0;
``````

}[/code]

Turns out it can be done even shorter:

[code]#include <stdio.h>

// Including math library
#include <math.h>

int main (int argc, const char * argv[])
{
// Declaring variable “result” to be sin(1.0).
double result = sin(1.0);

``````// Prints the variable "result" with 3 decimals.
printf("The sine of 1 radian is %.3f.\n", result);
return 0;
``````

}
[/code]

My solution. Hopefully it’s valid because they mentioned about declaring a function like this: double sin(double x);

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

int main(int argc, const char * argv[])
{
printf("Challenge 7, sine of 1 radian:\n");
printf("Answer is %.3f\n", sin(1));

return 0;
}``````

Output:

Challenge 7, sine of 1 radian:
Answer is 0.841
Program ended with exit code: 0

[code]#include <math.h>

int main(int argc, const char * argv[])
{
double sin(double x);
printf("%.3f\n",sin(1));

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

}
[/code]

This is what i came up with and it does get the result but its too bad there is no way to check the correct answer…

however i guess with coding there are several ways of doing the same thing. I just hope I’m not doing the assignment as intended Could anyone tell me what a radian is?

Iromao linked the definitive answer. To try and answer more simply, it’s an alternative way to measure an angle instead of degrees.
Where a degree is what you get when you slice a circle 360 slices, a radian is what you get when you slice a circle up into slices the same length as the circle’s radius.

Since we know the circumference of a circle is 2 times (pi) times the radius [ C=2(pi)r ]
If we say that the radius of the circle is one “unit” then [ C=2(pi)(1) ] or [ C=2(pi) ]
So there are 2(pi) radians in any circle (or approximately 6.28)

This is my solution for Challenge
I’ve some explanation in commentary

[code]#include <stdio.h>
#include <math.h> // include the library for your calculation
int main(int argc, const char * argv[])
{

``````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

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

return 0;
``````

}
[/code]
The result is as you would expected
Your answer is: 0.841
Program ended with exit code: 0

Code:

[code]/* Use the math library!
Add code to main.c that displays the sine of 1 radian.
Show the number rounded to three decimal points. It should be 0.841.
The sine function is declared like this: double sin(double x);
*/

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

double calculatedSin(double x){

``````double sine = sin(x);
return sine;
``````

}

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

``````double x = 1.0; // 1 radian
printf("Sine of 1 radian is %0.3f \n",calculatedSin(x));

return 0;
``````

}[/code]

result:

[quote]Sine of 1 radian is 0.841
Program ended with exit code: 0[/quote]

I got the code to work but, I don’t know what the question meant by using double sin(double x) ??

Since the challenge said:

I added the following code:

``````    double value = 1.0;
double result = sin(value);
printf("the sine of 1 is %.3f\n", round(result*1000)/1000);``````

I guess my challenge answer works for brevity…