Help with function

Task:
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
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<<x<<endl;
	return 0;
}


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
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<<exp(x,y);
	cout<<x<<endl;
	return 0;
}


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
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<<exp(x,y);
	cout<<endl;
	return 0;
}


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.
1
2
3
4
5
6
7
8
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.
@vlad
I didn't learn the unsigned nor insigned.
@CNg
@vlad
I didn't learn the unsigned nor insigned.


No problem. Remove this keyword.:)
Thank you! I traced the code and it works! Thank you for all your help.
Topic archived. No new replies allowed.