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++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
http://www.youtube.com/watch?v=QCnPJcNprEU&list=PL1EF620FCB11312A6
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.
1
2
3
4
5
6
7
8
9
void move(std::vector<double> &q, const std::vector<double> &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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void print(std::vector<float> v);
std::vector<float> move_v(const std::vector<float> &p, int U)
{
    std::vector<float> 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<float> 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<float> 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.