Thread: Loop and Array Program

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    18

    Loop and Array Program

    I am having difficulty determining what is wrong with my code. This code is calculated the sin function without actually using the function.
    Any help or advice?
    Code:
    # include <stdio.h>
    # include <math.h>
    
    
    double mysin(double* x, int* n){
        int j, i;
    	int count=0;
    	int factorial=1; 
    	int addorsub=0;
    	double sin=0;
    	    for(j=1; j<= *n; j+=1){
    		    if(j==1){
    			   i=1;
    			}
    			else{
    			   i=j*2-1;
    			}
    			factorial=1;
    		if(addorsub==0){
    		    for(count=1; count<=i; count+=1){
    			    factorial*=count;
    			}
    		sin+=pow(*x,i)/factorial;
    		addorsub+=1;
    		}
    		else{
    		    for(count=1; count<=i; count+=1){
    			    factorial*=count;
    			}
    			sin-=pow(*x,i)/factorial;
    			addorsub-=1;
    		}
    		}
    		return sin;
    }
    
    
    
    
    int main(){
        int x=0;
    	double n=0;
    	double computedsin;
    	double actualsin;
    	
    	printf("Please input x and n:\n");
    	scanf("%d %lf, &x, &n");
    	
        computedsin=mysin(&n,&x);
    	actualsin=sin(n);
    	
    	printf("mysin=%lf\n",computedsin);
    	printf("Real Sin=%lf\n", actualsin);
    	
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    sin() is defined in the "math.h" library, thus you should not use 'sin' as a variable name

    ....

    Also check line 47.

    Compile with warnings enabled, this will sort out a couple of the problems you have
    Last edited by africanwizz; 03-20-2014 at 07:20 PM. Reason: meh..

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    I changed the name of sin and the program will still not run. Good catch though, I would have never thought of that.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    THANK YOU! I see it!

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array loop
    By CASHOUT in forum C Programming
    Replies: 1
    Last Post: 03-28-2013, 06:59 PM
  2. Array with for loop
    By InfinityHacker in forum C Programming
    Replies: 16
    Last Post: 07-14-2012, 02:46 AM
  3. array and loop help
    By hockey1 in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 09:47 PM
  4. Help Array and Loop
    By Mike1982 in forum C Programming
    Replies: 5
    Last Post: 10-17-2007, 02:41 PM
  5. Need help on for loop and array
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2003, 10:17 AM

Tags for this Thread