c++ - Insert item to array -
i have 3 arrays contains 5 elements each (max size 5). wanted insert item, e.g, position 7. end result item should placed in 2nd array @ index 2 , 4th array created 1 element (from last item of 3rd array).
result array1 array1 - item1 (position 0) - item1 (position 0) - item2 (position 1) - item2 (position 1) - item3 (position 2) - item3 (position 2) - item4 (position 3) - item4 (position 3) - item5 (position 4) - item5 (position 4) array2 array2 - item1 (position 5) - item1 (position 5) - item2 (position 6) - item2 (position 6) - item3 (position 7) - item3 (position 7) -> new_item - item4 (position 8) - item4 (position 8) - item5 (position 9) - item5 (position 9) array3 array3 - item1 (position 10) - item1 (position 10) - item2 (position 11) - item2 (position 11) - item3 (position 12) - item3 (position 12) - item4 (position 13) - item4 (position 13) - item5 (position 14) - item5 (position 14) array4 - item1 (position 15)
and, if wanted item @ position 12, result should item3 of array3.
how can in c++?
you haven't told real problem, here's outline of general solution. access position 13:
unsigned int n = 13; // zero-based index find array. unsigned int = n/5; // zero-based index of element within array. unsigned int j = n%5;
for insertion, function insert item x @ location k , return last item (which had removed make room):
int insert(item *a, item x, unsigned int k) { item ret = a[4]; for(unsigned int j=4; j>k; --j) a[j]=a[j-1]; a[k] = x; return(ret); }
so if have 3 arrays, insert item x @ position 7:
item y; y = insert(array2, x, 2); y = insert(array3, y, 0);
now create new array4 whatever method like, , set array4[0]=y.
Comments
Post a Comment