Asking slice_array for elements
`cake' represents a 5-by-5 matrix. How would you do a row-wise addition on `cake'? I tried slicing `cake' and adding the slices, but things like state = cake[l1] + ... + cake[l5];
are illegal, and now I'm stuck.
Code:
1 0 0 0 0 }
0 1 0 0 0 }
1 0 0 0 0 } `cake'
0 0 0 1 0 }
+0 0 0 0 1 }
-------------
2 1 0 1 1 `state'
Code:
void cycle(int balls) {
valarray<int> state(0, 5); // State of the machine
valarray<int> cake(0, 25); // A "cake" with five layers (slices)
state[0] = balls;
// FORTRAN-style indexing is used: elements are read top to bottom,
// and left to right.
slice_array<int> l1 = cake[slice(0, 5, 1)];
slice_array<int> l2 = cake[slice(5, 5, 1)];
slice_array<int> l3 = cake[slice(10, 5, 1)];
slice_array<int> l4 = cake[slice(15, 5, 1)];
slice_array<int> l5 = cake[slice(20, 5, 1)];
for(int i = 0; i < 5; i++) {
state[i] = cake[0+i] + cake[5+i] + cake[10+i] + cake[15+i] + cake[20+i];
}
}
Also, how would you make this function more compact?
Thanks!