It looks like you just want to add an element at the front of the array at each timestep, thus moving the already existant array elements one to the right. You could avoid doing O(n**2)
ops like this
int& p_at_time(int index, int time_moment) {
return &p[time_moment-index+1];
}
- and at t=1: p_at_time(1,1) = I[1];
- at t=2: p_at_time(1,2) = I[2], (p_at_time(2,2) is already
== I[1]
) - at t=3: p_at_time(1,3) = I[3], (p_at_time(2,3) and p_at_time(3,3) have the values I[2] and I[1] respectively)