473,394 Members | 1,811 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,394 software developers and data experts.

Array(s)

Hello ..
A begginers question I presume, but it still needs to be asked . .

How can I remove ( or exclude or whatever ) an element from an 1-dimensional
array ?

Let say I have something like this: int myarray [4],
which is then populated with 5 numeric values.

My question is, how can I delete, let's say .. second element of this array
?

Thank you ..

Oct 10 '06 #1
5 1515
"Nickoteen" <Ni*******@Inet.Hrwrote in message
news:eg**********@garrison.globalnet.hr...
Hello ..
A begginers question I presume, but it still needs to be asked . .

How can I remove ( or exclude or whatever ) an element from an
1-dimensional array ?

Let say I have something like this: int myarray [4],
which is then populated with 5 numeric values.
You can't fit 5 numeric values into an array that's only 4 big.
My question is, how can I delete, let's say .. second element of this
array
Simplest answer is, you don't. Next answer is, you shift the remaining
elements left one.

int myarray[4];

myarray[0] = 0;
myarray[1] = 1;
myarray[2] = 2;
myarray[3] = 3;

So how to delete element 1 (the second element)?

for (int i = 2; i <= 3; ++i )
myarray[i - 1] = myarray[i];
myarray[3] = 0;

This would make the array 0, 1, 3, 0

But, if you need to do this, by all means use std::vector instead. Then you
can simply erase an element out of the middle with a single call.
Oct 10 '06 #2

Nickoteen wrote:
Hello ..
A begginers question I presume, but it still needs to be asked . .

How can I remove ( or exclude or whatever ) an element from an 1-dimensional
array ?

Let say I have something like this: int myarray [4],
which is then populated with 5 numeric values.

My question is, how can I delete, let's say .. second element of this array
?

Thank you ..
Hi,
A begginers question I presume, but it still needs to be asked . .
You have presumed correctly :)

Well the way to delete an element 'i' from an array of 'n' elements
would be to shift all the elements after i (from i+1 till n) up one
position, and update n to be n - 1, i.e change the total number of
elements.

You can use stl::vectors which will make your life easier with such
things :)

Regards,
Abhishek Srivastava

Oct 10 '06 #3
Simplest answer is, you don't. Next answer is, you shift the remaining
elements left one.

int myarray[4];

myarray[0] = 0;
myarray[1] = 1;
myarray[2] = 2;
myarray[3] = 3;

So how to delete element 1 (the second element)?

for (int i = 2; i <= 3; ++i )
myarray[i - 1] = myarray[i];
myarray[3] = 0;

This would make the array 0, 1, 3, 0
I see. Well, thanks mate .. I'll give it a try ..
Oct 10 '06 #4
Nickoteen posted:
How can I remove ( or exclude or whatever ) an element from an
1-dimensional array ?

You got a few replies over on comp.lang.learn.c-c++.

--

Frederick Gotham
Oct 10 '06 #5

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:FZ*******************@news.indigo.ie...
Nickoteen posted:
>How can I remove ( or exclude or whatever ) an element from an
1-dimensional array ?


You got a few replies over on comp.lang.learn.c-c++.
Ok .. thnx every1 ..
Oct 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.