I'm trying to define the function below in the main function:
1 2 3 4 5 6 7
float distance(float x, float y, float pitch)
{
float r; //local variable
r = sqrt(x * x + y * y);
Bangle = arctan2(pitch*(y/r) + x , pitch*(x/r) -y);
return (cos(Bangle),sin(Bangle));
}
I'm getting an error : a function-definition is not allowed here before ‘{’ token
{
^
I'm not rely sure if I should write it in this way or not?!
any suggestion.
There's a problem in your return
From what I understand you've already got a cos() and sin() function, so there's no problem with that. But in your return you have
return (cos(Bagle), sin(Bagle));
But you only return 2 arguments, whereas distance() needs 3. It should be like this:
Wait, what are you trying to return there? You've defined your main function to return an int (as all main functions must do). So why are you trying to return a float?
Also, why are you using the comma operator on line 8 there? Do you understand what the comma operator actually does in C++?
The comma operator means each term in the expression is evaluated. Then the final value of the entire expression is simply the part after the last comma (the rest is ignored).
I'm trying to understand the meaning of 'pitch' in the function float distance(float x, float y, float pitch)
In terms of geometry I assume x and y represent a 2-D coordinate. But what would pitch mean?
You code has the definition of the function inside main(). It should be inside the body of the function itself, as in the example given by Thomas1965 above. Note the name of the arctangent function is atan2() as previously mentioned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
float distance(float x, float y); // function prototype declaration
int main()
{
float d = distance(3, 4); // example function call
}
float distance(float x, float y) // function definition
{
float r = sqrt(x * x + y * y);
double Bangle = atan2((y/r) + x ,(x/r) -y);
double c = cos(Bangle);
double s = sin(Bangle);
return ???? // I don't know what goes here.
}
Please feel free to ask any question in the forum. My responses are no better and no worse whether in public or private. But in public, the whole forum community both benefits and is able to contribute.
@chervil
Okay , the point is if you still remember you helped me to rewrite python code to c++ last month. I want to add the function above into it (the form above I wrote it similar to python syntax). I don't understand all the part of the code you have written
T=100
N=10000
dt=float(T)/N
D= 1
DII=10
def Bfield(x,y,pitch): #pitch value in for loop is const.
r = np.sqrt(x**2. + y**2.)
Bangle =np.arctan2 (pitch*(y/r) + x, pitch*(x/r) -y)
return [np.cos(Bangle), np.sin(Bangle)]
n= 1000
seed(2)
finalpositions=[]
for number in range(0, 100):
finalpositions.append([])
x=[]
y=[]
x.append(10.)
y.append(0.)#0 or 20 for example , to check if the diffusion is isotropic or anisotropic
for i in range(n):
#localB = Bfield(x[-1],y[-1])
localB = Bfield(x[-1],y[-1],0.8)
s = np.random.normal(0, 1, 2)
deltaX= localB[0]*np.sqrt(2*DII*dt)*s[0] - localB[1]*np.sqrt(2*D*dt)*s[1]
deltaY= localB[1]*np.sqrt(2*DII*dt)*s[0] + localB[0]*np.sqrt(2*D*dt)*s[1]
x.append(x[-1] + deltaX)
y.append(y[-1] + deltaY)
finalpositions[-1].append(x)
finalpositions[-1].append(y)
allxes = []
allyes = []
for p in finalpositions:
allxes.append(p[0][-1])
allyes.append(p[1][-1])#
p1, = plt.plot(allxes, allyes, 'o') '