By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Doing Shifts In An Array

P: 4
Note: I'm using jGrasp and writing C++
I'm supposed to create a "remove" function based off of the following postcondition:

"The current item has been removed from the sequence, and the item after this (if there is one) is now the new current item."

I'm aware that one cannot simply remove an element from an array, leaving the position unoccupied, but is it possible to shift the elements in the array to the left in such a way that the succeeding element 'overwrites' the preceding element?

For example, |1|2|3|4|... --> |1|2|4|..., where '4' replaces '3'
Oct 1 '10 #1
Share this Question
Share on Google+
6 Replies

Expert Mod 5K+
P: 8,916
Yes but it's extremely inefficient because of all the data copying.

Any this is C++ you should be using STL containers not arrays. Normally I would say use a std::vector instead of an array but if you want to delete (or insert) in the middle of a sequence then you should use std::list.
Oct 1 '10 #2

P: 4
My instructor only wants us to use arrays though.
Oct 2 '10 #3

Expert Mod 5K+
P: 8,916
Well then I would use memcpy but I would guess your instructor wants you to use a for loop and just copy the entries down the array.
Oct 2 '10 #4

P: 4
I never used the memcpy function. :(

There are three main functions of this assignment: insert, attach, and remove. I used while loops for both insert and attach but I'm not sure how I would use a loop in general for the remove function. I have the following code:

Oct 2 '10 #5

P: 4
This is what I have for the remove function:

Oct 2 '10 #6

Expert Mod 5K+
P: 8,916
the line setting data[used-1] to 0 needs to be outside and after the loop.
Oct 2 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.