| | |
C program for a power function
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 23
Reputation:
Solved Threads: 0
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();
}
# 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();
}
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
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!
URL on facebook!
•
•
Join Date: Oct 2009
Posts: 23
Reputation:
Solved Threads: 0
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();
}
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();
}
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:
c Syntax (Toggle Plain Text)
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!
URL on facebook!
•
•
Join Date: Oct 2009
Posts: 23
Reputation:
Solved Threads: 0
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();
}
# 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();
}
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!
URL on facebook!
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.
c Syntax (Toggle Plain Text)
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!
URL on facebook!
•
•
Join Date: Oct 2009
Posts: 23
Reputation:
Solved Threads: 0
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.
C Syntax (Toggle Plain Text)
# include <stdio.h> # include "simpio.h" # include "genlib.h" # include "strlib.h" 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(u,v); r=r-1; } if(u==0&&v<0) printf("undefined"); else printf("the result is %d",s); getchar(); }
![]() |
Similar Threads
- Square root program without sqrt or pwr (C++)
- power funciton (C)
- Anyone able to help with a part of my program in C++ (recursive function)? (C++)
- how to write a program for power (C)
- How to program speech function for online dictionary (PHP)
- Recursion Function Problem (C++)
- Help with Java program writing (Java)
- PHP hold program execute function (PHP)
- Need help writing a program (C++)
Other Threads in the C Forum
- Previous Thread: C language help required (2 Questions here )
- Next Thread: pass excel sheet in linux C using libxls
| Thread Tools | Search this Thread |
adapter apple battery beep bsod cia commodityhardware cpu crime dead dell electricity energy environment failure green grid hacking iphone laptop licensing linux maxtor microsfot mobile mobo news opinion partition pc phone power ram recovery safety security sony supply terrorism undelete virtualization vista windows






