C program for a power function

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 23
Reputation: wangatang126 is an unknown quantity at this point 
Solved Threads: 0
wangatang126 wangatang126 is offline Offline
Newbie Poster

C program for a power function

 
0
  #1
Oct 27th, 2009
Can someone help me edit this program. I am a begginer in C and suck at it somewhat. I can't figure out a way to make the function work since i have to initialize s to equal u and if i initialize it in the function it will always make s=u. However if i initialize it in the program s still equals u. I'm lost can someone help me. Here is my code
# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# include "strlib.h"


int power(int num){
int u,v,r,s;
s=s*v;
}


main()
{
int u,v,r,s,pow;
printf("whats the base");
u=GetInteger();
printf("whats the power");
v=GetInteger();
r=v;
while(r!=0)
{
pow=power(s);
r=r-1;
}
if(u==0&&v<0)
printf("undefined");
else
printf("the result is %d",s);
getchar();
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 466
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #2
Oct 27th, 2009
If you declare the function as int then you must return an int value in the function definition.
By the way, I'm not sure if you are aware but there is a built in power function in the math library.
See this for more info
http://www.thinkage.ca/english/gcos/expl/c/lib/pow.html
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: wangatang126 is an unknown quantity at this point 
Solved Threads: 0
wangatang126 wangatang126 is offline Offline
Newbie Poster
 
0
  #3
Oct 27th, 2009
I tried that but it still dosent work.
Here is my new code
# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# include "strlib.h"


int power(int num){
int u,v,r,s;
s=s*v;
return(s);
}


main()
{
int u,v,r,s,pow;
printf("whats the base");
u=GetInteger();
printf("whats the power");
v=GetInteger();
r=v;
s=u;
while(r!=0)
{
pow=power(s);
r=r-1;
}
if(u==0&&v<0)
printf("undefined");
else
printf("the result is %d",s);
getchar();
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 466
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #4
Oct 27th, 2009
As the built in function is declared, the power function should contain 2 parameters, the first being the base and the second being the exponent. Try writing the function with a declaration like this:
  1. double power(double x, int n)
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: wangatang126 is an unknown quantity at this point 
Solved Threads: 0
wangatang126 wangatang126 is offline Offline
Newbie Poster
 
0
  #5
Oct 27th, 2009
i put in the line but it still dosent work. Here is my new code
# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# include "strlib.h"
double power(double x, int num)

double power(double x, int num){
int u,v,r,s;
s=s*v;
return(s);
}


main()
{
int u,v,r,s,pow;
printf("whats the base");
u=GetInteger();
printf("whats the power");
v=GetInteger();
r=v;

while(r!=0)
{
pow=power(s);
r=r-1;
}
if(u==0&&v<0)
printf("undefined");
else
printf("the result is %d",s);
getchar();
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 466
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #6
Oct 28th, 2009
If you have two parameters in your function definition then you need to have two parameters in the function call. Make sure the parameters in the function call are of the same type data type as those in the definitiion.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Chilton is an unknown quantity at this point 
Solved Threads: 2
Chilton Chilton is offline Offline
Newbie Poster
 
0
  #7
Oct 28th, 2009
Why are there unused local variables in the function power?

Also, bear in mind that your function is designed to take two arguments, yet you only supplied one in the function call.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: wangatang126 is an unknown quantity at this point 
Solved Threads: 0
wangatang126 wangatang126 is offline Offline
Newbie Poster
 
0
  #8
Oct 28th, 2009
how do i supply a second one?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 466
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #9
Oct 28th, 2009
When you call the function in main(), the two parameters should be the base and the exponent. In you last post these would refer to u and v.
  1. power(u,v);
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: wangatang126 is an unknown quantity at this point 
Solved Threads: 0
wangatang126 wangatang126 is offline Offline
Newbie Poster
 
0
  #10
Oct 28th, 2009
I tried that yet it still dosent work. it always says "the result is 45" no matter what number i enter. here is what i have now.

  1. # include <stdio.h>
  2. # include "simpio.h"
  3. # include "genlib.h"
  4. # include "strlib.h"
  5.  
  6.  
  7. double power(double x, int num){
  8. int u,v,r,s;
  9. s=s*v;
  10. return(s);
  11. }
  12.  
  13.  
  14. main()
  15. {
  16. int u,v,r,s,pow;
  17. printf("whats the base");
  18. u=GetInteger();
  19. printf("whats the power");
  20. v=GetInteger();
  21. r=v;
  22.  
  23. while(r!=0)
  24. {
  25. pow=power(u,v);
  26. r=r-1;
  27. }
  28. if(u==0&&v<0)
  29. printf("undefined");
  30. else
  31. printf("the result is %d",s);
  32. getchar();
  33. }
Reply With Quote Quick reply to this message  
Reply

Tags
power

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC