### Help with function

Write a function:
- exp(x,y):
- where x is positive (possibly a decimal) and y is a positive integer.
- Returns x to the y.
- The definition of x to the y: x multiplied by itself y times.
- Example: 2 to the 3 is 2*2*2.
- Example: 6 to the 1 is 6.
- Example: 12 to the 0 is 1. Anything to the 0 is 1, except 0. But you don't have to worry about 0.

 ``1234567891011121314151617181920`` ``````#include using namespace std; double exp(double&x,int y) { int n=1; for(int=n;n<=y;n++) n=n*x; return n; } int main() { double x; int y; cout<<"Enter two numbers "; cin>>x>>y; exp(x,y); cout<

Error being produced

\$ g++ exp.cpp -o exp
exp.cpp: In function `double exp(double&, int)':
exp.cpp:6: error: expected primary-expression before "int"
exp.cpp:6: error: expected `;' before "int"
exp.cpp:7: warning: converting to `int' from `double'

I am confused with the algorithm of the function. If anyone can help me fix this and explain to me, I would very much appreciated it.
The syntax of the for statement is invalid

for(int=n;n<=y;n++)
Last edited on
Whoops another careless mistake
Edit

 ``123456789101112131415161718`` ``````#include using namespace std; double exp(double&x,int y) { for(int n=1;n<=y;n++) n=n*x; return n; } int main() { double x; int y; cout<<"Enter two numbers "; cin>>x>>y; cout<

Error being produced
\$ g++ exp.cpp -o exp
exp.cpp: In function `double exp(double&, int)':
exp.cpp:6: warning: converting to `int' from `double'
exp.cpp:7: error: name lookup of `n' changed for new ISO `for' scoping
exp.cpp:5: error: using obsolete binding at `n'
Last edited on
Here a double value is converted to int ( n has type int, x has type double)

for(int n=1;n<=y;n++)
n=n*x;

Here used variable n in the return statement was no declared.

return n;
Edit

 ``123456789101112131415161718`` ``````#include using namespace std; double exp(double&x,int y) { for(double n=1;n<=y;n++) return n*x; } int main() { double x; int y; cout<<"Enter two numbers "; cin>>x>>y; cout<

It now compiles but doesnt do the function I desire. Instead it is just printing out the x value (as it seems).
There is an error with whole logic here.
Take for example x = 100 and y = 5
First iteration:
(n = 1)
n = n * x (1 * 100 == 100)
check if n <=y (100 <= 5 == false)
stop looping.
loop counter should be distinct variable.
 ``12345678`` ``````double exp( double x, unsigned int n ) { double result = 1.0; for ( unsigned int i = 1; i <= n; i++ ) result *= x; return result; }``````
Last edited on
@Miinipaa
I am confused, any suggestions? I am in an intro class to c++
You have a return statement inside your for loop. Your method will therefore return on the first iteration of the loop - when n is 1.