My output is coming different for the last three indices. Can someone explain me why? Thanks!
TRY TO UNDERSTAND MODULUS AND TRY THE PROBLEM AGAIN BEFORE LOOKING AT THIS COMMENT REPLY
So this may no longer be helpful to you but hopefully it will be helpful to others:
- Some hints:
- The for-loop stops at A.size() - 1.
- The first line in the for-loop is B[i] = A[i] you should your first A.size() - 1 indices
- The second line in the for-loop aims to modify the index at the current iteration in the loop plus the size of the input Array. So at the first iteration we will modify B[0 + A.size()]. What we set this index to is the modulus of the input array’s size minus the value of the current iteration we are at and the size of the input array. This prevents us from going out of bounds. So then we have B[0 + A.size()] = A[ (A.size() - 0) % A.size()];
So I will give the first iteration vals and then you may compute it and the rest of the values.
B = A;
B[0 + 4] = A[ (4 - 0) % 4 ];