Solution to Chapter 6 Challenge


#1

Ok, I love math, but I had to go back and review a little bit for this one.
First, you need to know that 1 Radian is appx 57.296 degrees.
Second, you need to know the code for pi in C is M_PI
Third, you need to know the formula for finding the Radian = Degrees * PI/180

Got All that? Now here’s my Solution:

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

int main(int argc, const char * argv[])
{
float z = (57.296)*(M_PI/180);
printf(“The sine of 1 radian is %.3f\n”,sinf(z));
}

Hope This Helps!


#2

Thanks allot for the help it was killing me I also noticed that you put parenthesis around the first number you can use one set and it still works is there a reason to use the parenthesis in the first number just curious thanks again.


#3

Ack! You don’t need to know ANY of that stuff to get the Challenge solution. Here is my complete and correct answer (also arrived at by others on this forum) that requires NO real knowledge of math.

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

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

printf( "The sine of 1 radian is %.3f.\n", sin( 1.0 ) );
return 0;

} // main()


#4

I got the solution to the challenge like mentioned above, but it is not really the solution i want, because it’s not the value that is rounded to 3 digits, it’s only the displayed token. I want an actual variable with the 3 decimal digits, but i failed with that one. The round() function from the math library simply turns it into a 1.000000, i was hoping you could give a parameter with the desired number of decimal digits. Isn’t there a function in the math library that works like this for example:

Where 3 represents the number of desired digits? I searched through the library for a bit but couldn’t find anything like that, but my mathematic skills as well as my english language skills are a bit limited, maybe i simply oversaw it?

Thanks in advance!


#5

We can’t change how the numbers are represented internally, but we can change how they are presented externally.

By the way, your english language skills are excellent.


#6

Hehe, thank you, but when it comes to math terms, my vocabulary really lacks precision.I always have to use ‘workarounds’ :wink:

I searched a bit in the web and finally found a function, that does what i thought was the aimed-for solution to this challenge:

float digitRound(float value, int digits) { float divisor = powf (10, digits); return (round(value * divisor)) / divisor; }
I expected some function like that would be in the math lib, but doing it yourself is even more fun :wink:
And if you use the %g token instead of %.3f, the output looks identical too - but the g% token and the powf() function were not yet mentioned in the book, so i went a bit ahead here. Sorry if that was off topic.


#7

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

int main(int argc, const char * argv[])
{
double x = 1.0;
double result = sin(x);
printf("%0.3f\n", result);
return 0;
}