![]() |
| | #1 |
| Registered User Join Date: Jan 2007
Posts: 14
| Help creating function of sin cos and tan Note: im new to C |
| Niz is offline | |
| | #2 |
| ... Join Date: Jan 2003
Posts: 1,190
| 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. |
| kermit is offline | |
| | #3 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| 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. |
| dwks is offline | |
| | #4 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,693
| > 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. |
| Salem is offline | |
| | #5 | |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Quote:
__________________ 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. | |
| dwks is offline | |
| | #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) ;
}
|
| Niz is offline | |
| | #7 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
|
__________________ 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. |
| dwks is offline | |
| | #8 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,693
| What about say Code: angle = my_func1(angle);
printf("Radians: %f \n", angle);
printf("sin: %f \n", my_func2(angle));
Code: float my_func1(float angle)
{
return angle*M_PI/180;
}
float my_func2(float angle)
{
return sin (angle) ;
}
|
| Salem is offline | |
| | #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. |
| Niz is offline | |
| | #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 = ∠
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));}
}
}
The * dereferencing operator seems to be the key. Hope this helps. cq |
| crypto_quixote is offline | |
| | #11 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,640
| There's no need to use a void pointer. You don't gain anything above using just a pointer to a float here. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #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);
}
|
| Niz is offline | |
| | #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 |
| crypto_quixote is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| calculating sin, cos, and tangent function | rcastel2 | C++ Programming | 12 | 02-23-2006 01:32 PM |
| need help to logically lay out program | Led Zeppelin | C Programming | 3 | 04-07-2002 10:11 PM |