# Function in C++

Dears All , Hi

I have this function written in matlab which takes 5 variables and return one , I would like to convert it in C++ , I think many of the statements in the function are the same in C/C++, i don’t know which statements need to be rewritten to be convenient with C++ form . please help

``````function th =APFPreogram(xc,yc,sf,sl,sr)

qx=xc; qy=yc;

golx=12.8; goly=14;
%
obs1=[0 0];
obs2=[0 0];
obs3=[0 0];

resultant = [0.001;0.001 ];

zeta = 0.1;

dq = sqrt (   (qx-golx)^2 + (qy-goly)^2   );

dstar = 1;

if  dq < dstar

gatt = zeta * ( [qx ; qy] - [golx ; goly]);
else
gatt = dstar * zeta * ( [qx ; qy] - [golx ; goly]) / dq;
end

grep = [ 0;  0];

qstar = 1.5;

n = 0.005;

m=1;
d1 = sf;

if (d1<qstar && d1>0)

obs1(1) = xc + d1;
obs1(2) = yc + d1;

grep = grep + (  n * (   (1/qstar) - (1/d1)   ) * (([qx ; qy]-[obs1(1) ; obs1(2)])/ d1^2) );

end

d2 = sl;

if (d2<qstar && d2>0)

obs2(1) = xc + d2;
obs2(2) = yc + d2;

grep = grep + (  n * (   (1/qstar) - (1/d2)   ) * (([qx ; qy]-[obs2(1) ; obs2(2)])/ d2^2) );

end

d3 = sr;

if (d3<qstar && d3>0)

obs3(1) = xc + d3;
obs3(2) = yc + d3;

grep = grep + (  n * (   (1/qstar) - (1/d3)   ) * (([qx ; qy]-[obs3(1) ; obs3(2)])/ d3^2) );

end

resultant = -1* (gatt + grep);

new = [qx ; qy] + resultant;

dmoved = sqrt (  (qx-new(1))^2 + (qy-new(2))^2  );

xth= new(1) - qx    ;
yth=new(2) - qy    ;

qx = new(1);
qy = new(2);

th= (atan2(yth , xth)); ;

end
``````

You need to lean basic C/C++ syntax. get an elementary textbook.

Fiirst place you need to start, is that you have to declare the variables you are using. What's qx ? What's qy ?

You don't need to declare variables in matlab, you always have to in C/C++

``````(qx-golx)^2
``````

Be careful when you translate this one!