0

I'm new here and also new to the language C using GNU. With that being said I'm looking for guidance. I have been given the task to compute the angle of sine using the Taylor series, I have done the calculations and everything. The problem is, that I can't get anything to print out.I'm having problems with understanding how to get the users' input...compute it in another method..then printing it out. I have a feeling its how I pass my functions. After days of surfing the web and having too much pride to ask my classmates, I'm coming to yall for advice. I'll post my code below! Thanks in advance!

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

double sine(float a);

int main() {

    float x;

    printf("Enter angle: \n");
    scanf("%f", x);

    float angleOfSine = sine(x);

    printf("%f", angleOfSine);
}

double sine(float angle) {

    float ans, x;
    int i;

    for(int i =1; i < 10; i++){
    }
    return (i);

    double fact = i *(i -1);
    if(i % 2 == 0){
    }
    else{
        return fact;
    }

    ans = (x - (pow(x,3)/fact)+(pow(x,5)/fact)-(pow(x,7)/fact)+(pow(x,9)/fact));
    return ans;
}
rtayl0a
  • 23
  • 1
  • 8
  • 5
    This code `int i; for(int i =1; i < 10; i++){ } return (i);` loops 9 times and then returns 10 each time. Not what you're after. Your call to `scanf()` is broken too; you need to pass `&x`. You should check that the `scanf()` succeeded. You should print your input value. You need to review more of your `sine()` algorithm than just the initial loop I've already pointed out. – Jonathan Leffler Apr 06 '16 at 01:51
  • Indeed . It never makes it to `...double fact = i *(i -1);...` – Nicolas Miari Apr 06 '16 at 01:53
  • 1
    Also, you have declared `int i` **both** before (i.e., outside) the `for` loop **and** inside it (i.e., control statement). My guess is that you are returning the outer, uninitialized one. – Nicolas Miari Apr 06 '16 at 01:55
  • A few `printf` functions inside `sine` would have quickly shown the problem. First step in debugging an issue (aside from turning on all compiler warning flags and reading the warnings). –  Apr 06 '16 at 01:57
  • It's unclear to me why you pass a `float` value to `sine()`, calculate in a mix of `float` and `double` assigning to a `float`, return a `float` value that's converted to `double` in the `return`, and then reconverted back to `float` in the calling function. All `float` or all `double` would be more sensible. – Jonathan Leffler Apr 06 '16 at 01:58
  • 1
    @NicolasMiari: missed that — well spotted. It means an indeterminate value is returned, not 10 as I assumed. – Jonathan Leffler Apr 06 '16 at 01:59
  • I've closed this as a duplicate of [How does C compute `sin` and other math functions](https://stackoverflow.com/questions/2284860/) because the more up-voted answers cover what happens in current maths libraries — and modern chips — but some of the less up-voted cover Taylor series etc, so all the information you need is there. – Jonathan Leffler Apr 06 '16 at 02:09
  • Thanks for the fast reply!@Jonathan: So I looked at the other page you marked as duplicate. Those are just formulas, but my problem I guess is implementing the formula. My real assignment is that I have to use this as a library. So in reality the formula is in another file as a library. And the method has to return a float while the parameter angle is a double, which addresses the other statement. My main problem is getting the user's input...calculating it in another file/method..then printing out the answer. – rtayl0a Apr 06 '16 at 03:01
  • "angle of sine"? sine takes an angle as its argument (domain) and yields a result (range) that is a unitless real number representing a ratio between the length of the far side of a right triangle and the length of the hypotenuse. If what you actually want is the angle given the sine, that's the arcsin, not sin function. – Jim Balter Apr 06 '16 at 03:58
  • " Those are just formulas" -- no, there at least one answer there that gives explicit C code, and several that link to libraries of source code. – Jim Balter Apr 06 '16 at 04:04
  • You are right @balter. I mean take sine of that angle. Grammar mistake. – rtayl0a Apr 06 '16 at 12:06
  • I rephrased my original question, I want someone to explain to me the process of passing the users input to another method and printing it out. @balter – rtayl0a Apr 06 '16 at 12:08

0 Answers0