### Convolution of dice

I have a biased dice with probability of throwing 1,2,3,4,5,6 as 0.35 , 0.10, 0.05, 0.05, 0.15, 0.30 respectively. now I want to calculate the expected value of the sum of thrown values, if i throw 10 such dice. I wrote a code in python it worked perfectly and now i have to translate it to c++ and am still new to c++
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` `````` #include #include using namespace std; vector convolution (vector prob1, vector prob2) { int n = prob1.size(); int m = prob2.size(); vector Sum((n+m-1), 0.); for(int i=0; i recursive (int n){ vector prob1={0.35,0.10, 0.05, 0.05,0.15,0.30}; vector prob2={0.35,0.10, 0.05, 0.05,0.15,0.30}; vector a= convolution(prob1,prob2); vector> TOTAL ={a}; if (n==1){ return prob1; } else{ for(int i =2; i r =convolution(TOTAL[i-2], prob1); TOTAL.push_back(r); } vector a= TOTAL[-1]; return a; } } int main(){ vector R= recursive(10); float a; vector ExpectedValue; for(int i=0; i< R.size();i++){ ExpectedValue.push_back(R[i]*(i+10)); a +=R[i]*(i+10); } cout<
 ``12345`` ``````for(int i=0; i

I see `n*m` assignments in that loop, yet you assign to only `n+m-1` elements. In other words, you would get the same result with n+m-1 assignments. Not an error though.

There are two obvious points of error:

Line 29: `TOTAL[-1]`
Q: Which element is at index -1?
A: -1 is not a valid index to vector.
http://www.cplusplus.com/reference/vector/vector/operator[]/
 Portable programs should never call this function with an argument n that is out of range, since this causes undefined behavior.

Line 39:
 ``12`` ``````float a; // uninitialized a +=R[i]*(i+10); // unknown + something is still unknown ``````
thank you
`Sum[i+j] += prob1[i]*prob2[j];`
Topic archived. No new replies allowed.