C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-08-2007, 12:41 PM   #1
Niz
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
Niz is offline   Reply With Quote
Old 01-08-2007, 12:52 PM   #2
...
 
kermit's Avatar
 
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.
__________________
Got Ed?

sys-sizes
kermit is offline   Reply With Quote
Old 01-08-2007, 01:16 PM   #3
Frequently Quite Prolix
 
dwks's Avatar
 
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   Reply With Quote
Old 01-08-2007, 02:39 PM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 01-08-2007, 03:39 PM   #5
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Quote:
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.
dwks is offline   Reply With Quote
Old 01-08-2007, 04:46 PM   #6
Niz
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   Reply With Quote
Old 01-08-2007, 05:09 PM   #7
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
http://www.cprogramming.com/tutorial/c/lesson4.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   Reply With Quote
Old 01-08-2007, 05:20 PM   #8
and the hat of Jobseeking
 
Salem's Avatar
 
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));
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.

Salem is offline   Reply With Quote
Old 01-08-2007, 05:41 PM   #9
Niz
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   Reply With Quote
Old 01-08-2007, 06:32 PM   #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
crypto_quixote is offline   Reply With Quote
Old 01-08-2007, 07:04 PM   #11
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 01-09-2007, 10:01 AM   #12
Niz
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   Reply With Quote
Old 01-09-2007, 05:55 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:14 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22