473,395 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Doing Shifts In An Array

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
6 2311
Banfa
9,065 Expert Mod 8TB
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
My instructor only wants us to use arrays though.
Oct 2 '10 #3
Banfa
9,065 Expert Mod 8TB
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
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
This is what I have for the remove function:

Oct 2 '10 #6
Banfa
9,065 Expert Mod 8TB
the line setting data[used-1] to 0 needs to be outside and after the loop.
Oct 2 '10 #7

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

Similar topics

19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
3
by: Messer | last post by:
hello, is there a way to deserialize the array: a:2:{i:0;s:8:"element0";i:1;s:8:"element1";} in vb. net? greets, messer
13
by: DazedAndConfused | last post by:
Is there a quick way to fill an array with zeros or make all elements empty/nothing?
2
by: Dino | last post by:
I need to create a public multi-dim array that can hold a mixture of string and int values. I will need to search this array for a value and return a value from a subscript. This array will...
13
by: Adam Honek | last post by:
Instead of the ZeroMemory API what would one use in VB.NET to clean an array using a custom structure? The .clear isn't a member of it by default. Any suggestions? Thanks, Adam
3
by: Chris | last post by:
I am clearly missing something simple here.... I have a function (fileTree) that recursively reads all the files in a directory passed to it, and returns an array of the files, I have included...
19
by: Eugeny Myunster | last post by:
I know, only simple one: #include <stdio.h> int main() { int min=0,max=0,i,arr; for(i=0;i<12;i++) arr=rand()%31-10; for(i=0;i<12;i++)
2
by: Damien582 | last post by:
Hi guys, new here. I've used this site for a while researching answers, but this is my first post. I've searched everywhere and I haven't found an answer that works. First, what I am doing: Cubed...
12
by: raylopez99 | last post by:
I have an array that I wish to preserve as "read only". The array holds references to variables myObjects instantiated by new that change all the time. The variables are part of a class that I...
6
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.