### Python to C++ code?

I'm not sure if this appropriate post but I found no place to post it except here. I would like to translate the following code to C++

 ``12345678910111213141516171819202122232425`` ``````p[0, 1, 0, 0, 0] world = ['green', 'red', 'red', 'green', 'green'] measurements = ['red', 'red'] pHit = 0.6 pMiss = 0.2 pExact = 0.8 pOvershoot = 0.1 pUndershoot = 0.1 def sense(p, Z) q=[] for i in range(len(p)) hit = (Z == world[i]) q.append(p[i]*(hit*pHit + (1-hit)*pMiss)) q[i] = q[i] /s return q def move(p, U) q = [] for i in range(len(p)) s = pExact * p[(i-U)% len(p)] s = s + pOvershoot * p[(i-U-1)%len(p)] s = s + pUndershoot * p[(i-U+1)%len(p)] q.append(s) return q``````

the problem I'm facing with move function. I took the code from the following video
Note: i - U + {0 | -1 | 1} could be a negative number. In C++ the result of x%y is implementation-defined for negative values of x. You should make sure that your program gives back values that match your expectations.
 ``123456789`` ``````void move(std::vector &q, const std::vector &p, int U){ q.clear(); for (size_t i = 0; i < p.size(); i++){ double s = pExact * p[(i - U) % p.size()]; s += pOvershoot * p[(i - U - 1) % p.size()]; s += pUndeshoot * p[(i - U + 1) % p.size()]; q.push_back(s); } }``````
> In C++ the result of x%y is implementation-defined for negative values of x.

In C++ the result of x%y used to be implementation-defined for negative values of x.
Now, the IS mandates that the result must have the same sign as the dividend.

 if the quotient a/b is representable in the type of the result, (a/b)*b + a%b is equal to a.
@helios,
thank you so much for this code. However, I'm still not getting the expected results. I've altered the function to return a vector instead of passing a vector that holds the result.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````void print(std::vector v); std::vector move_v(const std::vector &p, int U) { std::vector q; q.reserve(p.size()); const float pExact(0.8), pOvershoot(0.1), pUndershoot(0.1); for (int i = 0; i < p.size(); ++i) { double s = pExact * p[(i - U) % p.size()]; s += pOvershoot * p[(i - U - 1) % p.size()]; s += pUndershoot * p[(i - U + 1) % p.size()]; q.push_back(s); } return q; } int main () { std::vector p, temp_v; p.reserve(5); temp_v.reserve(5); p.push_back(0); p.push_back(.1); p.push_back(.8); p.push_back(.1); p.push_back(0); print(p); temp_v = move_v(p, 1); print(temp_v); temp_v = move_v(temp_v, 1); print(temp_v); return 0 } void print(std::vector v) { std::cout << "[ "; for ( unsigned int i = 0; i < v.size(); ++i) { std::cout << v[i]; if ( i < v.size() - 1) std::cout << " , "; if ( i == v.size() - 1 ) std::cout << " ] " << std::endl; } }``````

Results:
[ 0 , 1 , 0 , 0 , 0 ]
[ 0 , 0.1 , 0.8 , 0.1 , 0 ] <--- Correct
[ 0 , 0.01 , 0.16 , 0.66 , 0.16 ] <--- Not correct ( correction: [0.01 , 0.01, 0.16, 0.66, 0.16])

I did implement a function but it is not elegant one. It is only for U = 1. Any suggestions?
Topic archived. No new replies allowed.