Thread: Help creating function of sin cos and tan

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    14

    Help creating function of sin cos and tan

    i need to create a function that will convert an user input angle into all three sin cos and tan and display, i can do this without pointers and creating separate functions for each sin cos tan, but i want to know how to do this with pointers and in 1 function. Any websites or people to help? books ive used dont help much. ill explain more if necessary, thanks

    Note: im new to C

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    How about you post some code showing what you have tried so far. What you need to know how to do this is in your textbook, if it's a book worth anything, that is. It may not have a specific example of what you want to do, but it will give you details on how to pass a pointer to a function, as well as calling functions from functions.

    As for bundling everything up into one function, its maybe a matter of personal taste, but I prefer separating out different jobs into different functions. Make your function do one thing, and do that one thing well.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Combining the three values into one function isn't hard, you just need to figure out the pointers part.

    Also see http://www.cprogramming.com/tutorial/c/lesson6.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but i want to know how to do this with pointers and in 1 function
    Pointers to what - where to store the results, which functions to call.
    Post an attempt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    i need to create a function that will convert an user input angle into all three sin cos and tan and display
    IMO it sounds like a pointer to a number is passed; that number is passed to sin, cos, and tan; and the return values of those functions are printed.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    maybe i just need to understand pointers better but i just cant understand the point of pointers, they examples given are so simple there's no need for them, here's what i have done without pointers, and yes dwks thats exactly what i want to do. Also how would i do what i have currently done without using global variables, if i put them into the main i get errors, unless that is why pointers would be needed?

    Code:
    #include<stdio.h>
    #include<math.h>
    
    
    float angle, my_func1(void), my_func2(void), my_func3(void), my_func4(void);
    
    
    main()
    
    {
    
      
      printf("Enter Value in Degrees: ");
      scanf("%f", &angle);
    
      printf("Degrees: %f \n", angle);
      printf("Radians: %f \n", my_func1());
      printf("sin: %f \n", my_func2());
      printf("cos: %f \n", my_func3());
      printf("tan: %f \n", my_func4());
    
    return(0);
    }
    
    
    float my_func1(void)
    {
      
    	return angle*M_PI/180;
    }
    
    
    float my_func2(void)
    {
      return sin (angle) ;
    }
     
    
    
    float my_func3(void)
    {
      return cos (angle) ;
    }
    
    
    float my_func4(void)
    {
    
      return tan (angle) ;
    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What about say
    Code:
      angle = my_func1(angle);
      printf("Radians: %f \n", angle);
      printf("sin: %f \n", my_func2(angle));
    With say
    Code:
    float my_func1(float angle)
    {
    	return angle*M_PI/180;
    }
    
    
    float my_func2(float angle)
    {
      return sin (angle) ;
    }
    With appropriate prototypes of course.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Salem i don't understand what problem you are solving? but im guessing its, not using global variables, i tried it and the compiler says 'my_func' requires no argument, i take it its to do with the prototypes.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    19
    Howdy,

    Just so happens, I'm working on getting a better understanding of pointers right now myself- so your question
    inspired me to hack the following:
    Code:
    /*code to pass a pointer to a function which will calculate the */
    /*sin, cos, and tan of an angle*/
    #include<stdio.h>
    
    #include<math.h>
    
    
    void three_values(void *angle);
    
    int main()
    {  
    	/*declare a variable to hold the angle value*/
    	float ang;
    
    	/*declare and initialize a pointer*/
    	void *angle = &ang;
    
    	printf("Enter the angle:\n");
    
    	scanf("%f", &ang);
    
    	three_values(angle);
    
    	return 0;
    }
    
    void three_values(void *angle)
    {
            int i;
    
    	for(i = 1; i < 4; i++){
    
     	if(i == 1){
    		printf("cos = %f\n", cos(*(float *)angle));}
    
    	else if(i == 2){
    		printf("sin = %f\n", sin(*(float *)angle));}
    
    	else if(i == 3){
    		printf("tan = %f\n", tan(*(float *)angle));}
    
    	
    
    	}
    }
    There are some interesting things you can do with void pointers, functions, and typecasting pointers.
    The * dereferencing operator seems to be the key.

    Hope this helps.

    cq

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's no need to use a void pointer. You don't gain anything above using just a pointer to a float here.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    ok no worries ive sorted it out, for future browsers heres the solution simple short and sweet.

    Code:
    #include<stdio.h>
    #include<math.h>
    
    void my_func(float ang,float* r, float* s, float* c, float* t)
    {
        *r=ang*M_PI/180;
        *s=sin(*r);
        *c=cos(*r);
        *t=tan(*r);
    }
    
    main()
    
    {
      float angle,radians,sinang,cosang,tanang;
      printf("Enter Value in Degrees: ");
      scanf("%f", &angle);
      my_func(angle,&radians,&sinang,&cosang,&tanang);
      printf("\nDegrees: %f \n", angle);
      printf("Radians: %f \n", radians);
      printf("Sin: %f \n", sinang);
      printf("Cos: %f \n", cosang);
      printf("Tan: %f \n", tanang);
    
    return(0);
    }

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    19
    Howdy Quzah,

    Yep, I see your *point. A void pointer is useless overkill.

    The whole thing was an exercise of my own to use void pointers, void functions, and typecasting.

    Thanks again for your critique.

    cq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculating sin, cos, and tangent function
    By rcastel2 in forum C++ Programming
    Replies: 12
    Last Post: 02-23-2006, 01:32 PM
  2. need help to logically lay out program
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 04-07-2002, 10:11 PM