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

deleting instance in array

Dan
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks
for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}

Dan

Jul 22 '05 #1
18 2440

"Dan" <Dan@ hotmail.com> wrote in message
news:c4****************@news20.bellglobal.com...
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks
for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}


What does not work? Does it crash?

How do you create each member of the array? If you use something like

array_3[i] = new someobjectyouhavenotshown;

then you're deleting it properly. This assumes that the array holds
pointers. Does it?

Or, are you expecting the ith item to go away, and all items beyond that to
shift up one space?

You'll need to be more specific about what the array contains, how it is
populated (i.e., how are its items created), and what you mean when you say
it doesn't allow you to do a delete.

Perhaps what you want to use is a std::vector, not an array. But without
more info, it's impossible to tell.

-Howard

Jul 22 '05 #2

"Dan" <Dan@ hotmail.com> wrote in message
news:c4****************@news20.bellglobal.com...
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks
for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}


It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.

The easiest way to do this is to learn about vectors, which can be resized.
Get yourself a modern C++ book that teaches you about vectors.

john
Jul 22 '05 #3
Dan wrote:
I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks
What would you expect the result to be? Say, I have an array
to begin with:

int int_array[] = { 1, 2, 3, 4, 3, 2 };

and I want to delete the duplicate '2'. What should the array
be after I finish "deleting" from it?


for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}

Jul 22 '05 #4
Dan

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:2g*************@ord-read.news.verio.net...
Dan wrote:
I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


What would you expect the result to be? Say, I have an array
to begin with:

int int_array[] = { 1, 2, 3, 4, 3, 2 };
and I want to delete the duplicate '2'. What should the array
be after I finish "deleting" from it?


Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.

for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}

Jul 22 '05 #5

"Dan" <Dan@ hotmail.com> wrote in message
news:Iw***************@news20.bellglobal.com...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:2g*************@ord-read.news.verio.net...
Dan wrote:
I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


What would you expect the result to be? Say, I have an array
to begin with:

int int_array[] = { 1, 2, 3, 4, 3, 2 };
and I want to delete the duplicate '2'. What should the array
be after I finish "deleting" from it?


Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.


I would stop uses the term delete, as I'm sure you know delete refer to
something quite different in C++ from what you are trying to do. Lets just
say you are trying to remove duplicate entries. And the only way to do that
is to overwrite entries with other ones. For instance

1, 2, 3, 2, 4, 5

goes to

1, 2, 3, 4, 5, 5

The second 2 has been removed by copying all the higher elements (4 and 5)
down one position. Now of course we have an extra 5 at the end, but that
doesn't matter because we are going to shorten the array by one, so that
extra 5 will be removed when that happens.

Some questions

1) Are you prepared to sort the array, or must the elements stay in the
order they are already? The algorithm you should use is simpler and more
efficient if you are prepared to sort the array (because in a sorted array
the duplicate elements are adjacent).

2) Are you prepared to use a vector? It could literally be one line of code
if you are. In a perfect world there would be no reason at all not to use a
vector for this problem, but I realise that maybe you have never used one
before and you want to stick with what you know. But in truth vectors are
easier than dynamic arrays, vectors are what newbies should use all the time
while dynamic arrays are what experts should use sometimes (most of the time
experts should use vectors as well).

john
Jul 22 '05 #6
Dan wrote:
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks
for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; i++)
{
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}


You can call the destructor on an array element, but you'd be advised to
construct it again -- see below.
#include <new>

template <typename Tz>
Tz * Create( void * i_location )
{
return new ( i_location ) Tz();
}
template <typename T>
void Destroy( T & i_val )
{
i_val.~T();

void * i_addr = static_cast< void * >( & i_val );

Create<T>( i_addr );
}

struct A
{

A()
{
}

~A()
{
}

int z;
};
A a[20];
int i[20];

int main()
{

Destroy( a[3] );

Destroy( i[3] );
}

Jul 22 '05 #7
Dan
Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.

I would stop uses the term delete, as I'm sure you know delete refer to
something quite different in C++ from what you are trying to do. Lets just
say you are trying to remove duplicate entries. And the only way to do

that is to overwrite entries with other ones. For instance

1, 2, 3, 2, 4, 5

goes to

1, 2, 3, 4, 5, 5

The second 2 has been removed by copying all the higher elements (4 and 5)
down one position. Now of course we have an extra 5 at the end, but that
doesn't matter because we are going to shorten the array by one, so that
extra 5 will be removed when that happens.

Some questions

1) Are you prepared to sort the array, or must the elements stay in the
order they are already? The algorithm you should use is simpler and more
efficient if you are prepared to sort the array (because in a sorted array
the duplicate elements are adjacent).
I originally had two on sorted arrays, I combined into a third array, then I
sorted them . Now I have to eliminate the duplicates.
But juste one question when the duplicates are removed, we just cout a
shorter array, we dont really shorten the array right ?


2) Are you prepared to use a vector? It could literally be one line of code if you are. In a perfect world there would be no reason at all not to use a vector for this problem, but I realise that maybe you have never used one
before and you want to stick with what you know. But in truth vectors are
easier than dynamic arrays, vectors are what newbies should use all the time while dynamic arrays are what experts should use sometimes (most of the time experts should use vectors as well). It is something to look into and I will , but my class does not ask for
this at the moment

john

Jul 22 '05 #8

"Dan" <Dan@ hotmail.com> wrote in message
news:Fx****************@news20.bellglobal.com...
Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.

I would stop uses the term delete, as I'm sure you know delete refer to
something quite different in C++ from what you are trying to do. Lets just say you are trying to remove duplicate entries. And the only way to do

that
is to overwrite entries with other ones. For instance

1, 2, 3, 2, 4, 5

goes to

1, 2, 3, 4, 5, 5

The second 2 has been removed by copying all the higher elements (4 and 5) down one position. Now of course we have an extra 5 at the end, but that
doesn't matter because we are going to shorten the array by one, so that
extra 5 will be removed when that happens.

Some questions

1) Are you prepared to sort the array, or must the elements stay in the
order they are already? The algorithm you should use is simpler and more
efficient if you are prepared to sort the array (because in a sorted array the duplicate elements are adjacent).


I originally had two on sorted arrays, I combined into a third array, then

I sorted them . Now I have to eliminate the duplicates.
But juste one question when the duplicates are removed, we just cout a
shorter array, we dont really shorten the array right ?


Yes that's fine, but it wasn't clear from your original post whether that
was a possibility.

Here's some sample code, it only works on an already sorted array.

int remove_dups(int* array, int old_size)
{
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array[i] != array[i - 1])
array[new_size++] = array[i];
return new_size;
}

int main()
{
int a[7] = { 1, 1, 1, 2, 3, 5, 5 };
int size = sizeof a/sizeof a[0];
size = remove_dups(a, size);
for (int i = 0; i < size; ++i)
cout << a[i] << ' ';
cout << '\n';
}

As you can see remove_dups returns the new size of the array, after the
duplicates have been removed.

john
Jul 22 '05 #9
Dan
> Yes that's fine, but it wasn't clear from your original post whether that
was a possibility.

Here's some sample code, it only works on an already sorted array.

int remove_dups(int* array, int old_size)
{
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array[i] != array[i - 1])
array[new_size++] = array[i];
return new_size;
}

int main()
{
int a[7] = { 1, 1, 1, 2, 3, 5, 5 };
int size = sizeof a/sizeof a[0];
size = remove_dups(a, size);
for (int i = 0; i < size; ++i)
cout << a[i] << ' ';
cout << '\n';
}

As you can see remove_dups returns the new size of the array, after the
duplicates have been removed.

john


WOw thats great coding, nice , easy and short
thanks a lot, I am going to study this for a while now.
Jul 22 '05 #10
> >
int remove_dups(int* array, int old_size)
{
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array[i] != array[i - 1])
array[new_size++] = array[i];
return new_size;
}


WOw thats great coding, nice , easy and short
thanks a lot, I am going to study this for a while now.


Thanks, but actually its bugged. It will fail if the old_size is zero, in
that case new_size will equal 1 which is obviously incorrect. Replace with
this slight modification

int remove_dups(int* array, int old_size)
{
if (old_size == 0)
return 0;
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array[i] != array[i - 1])
array[new_size++] = array[i];
return new_size;
}

john
Jul 22 '05 #11
John Harrison wrote:

"Dan" <Dan@ hotmail.com> wrote in message
news:c4****************@news20.bellglobal.com...
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

....
It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.


???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the end,
it has a size of 1. Please explain what I'm missing or why it isn't possible.
Jul 22 '05 #12

"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
John Harrison wrote:

"Dan" <Dan@ hotmail.com> wrote in message
news:c4****************@news20.bellglobal.com...
hello,

I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks
...
It is impossible to change the size of an array in C++, so you are
trying to do something rather difficult.


???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the

end, it has a size of 1. Please explain what I'm missing or why it isn't

possible.

That's a pointer to allocated memory not an array. It was unclear from the
OP post whether he was dealing with pointers or arrays, I said 'that's
impossible' in order to try and provoke a reaction and get him to explain
himself a little more.

john
Jul 22 '05 #13
Julie wrote:
John Harrison wrote:
"Dan" <Dan@ hotmail.com> wrote in message
news:c4****************@news20.bellglobal.com. ..
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

...

It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the end,
it has a size of 1. Please explain what I'm missing or why it isn't possible.


By calling 'realloc' you're not resizing an array, you're getting
another array of a potentially different size with some elements that
have the same values as the source array.

I don't have a copy of C99 (or C90) Standard handy, but AFAICT, the
values of the address you pass to 'realloc' (as the first argument)
and the address you get back from 'realloc' are never the same (unless
it fails to allocate and returns NULL and you passed NULL to begin
with).

V
Jul 22 '05 #14
John Harrison wrote:

"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
John Harrison wrote:

"Dan" <Dan@ hotmail.com> wrote in message
news:c4****************@news20.bellglobal.com...
> hello,
>
> I would to know if it is possible to delete an instance in an array, The > following does not allow me to do a delete.
> I am trying to find and delete the duplicate in an array, thanks


...
It is impossible to change the size of an array in C++, so you are trying to do something rather difficult.


???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the

end,
it has a size of 1. Please explain what I'm missing or why it isn't

possible.

That's a pointer to allocated memory not an array. It was unclear from the
OP post whether he was dealing with pointers or arrays, I said 'that's
impossible' in order to try and provoke a reaction and get him to explain
himself a little more.

john


Ok, we are getting into a pretty gray area here by making that distinction.
Yes, I do agree that there is a distinction conceptually, but implementation,
it is the same, right?
Jul 22 '05 #15
Victor Bazarov wrote:

Julie wrote:
John Harrison wrote:
"Dan" <Dan@ hotmail.com> wrote in message
news:c4****************@news20.bellglobal.com. ..

hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

...

It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the end,
it has a size of 1. Please explain what I'm missing or why it isn't possible.


By calling 'realloc' you're not resizing an array, you're getting
another array of a potentially different size with some elements that
have the same values as the source array.

I don't have a copy of C99 (or C90) Standard handy, but AFAICT, the
values of the address you pass to 'realloc' (as the first argument)
and the address you get back from 'realloc' are never the same (unless
it fails to allocate and returns NULL and you passed NULL to begin
with).

V


I don't have a copy either, but I've never heard that it *must* be a different
address, especially if the initial block is being shrunk. Regardless, I could
write my own sub allocator that would resize (shrink) in place that would then
meet all the conditions of being able to have an allocated array, delete
elements and resize (shrink) in place.

Regardless, I think that this discussion is boiling down to the concept of what
an array is -- JH doesn't consider malloc(sizeof(int) * size) to allocate an
array, per se, but rather a block of memory. So, I guess if you get down to
the specific exact case of an array, such as int array[] = { 1, 2, 3 }, then
yes, you can't _delete_ an element, however I was referring to the more looser
term for array which includes allocated blocks treated as arrays...
Jul 22 '05 #16
> > > > It is impossible to change the size of an array in C++, so you are
trying to
> do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at
the end,
it has a size of 1. Please explain what I'm missing or why it isn't possible.

That's a pointer to allocated memory not an array. It was unclear from the OP post whether he was dealing with pointers or arrays, I said 'that's
impossible' in order to try and provoke a reaction and get him to explain himself a little more.

john


Ok, we are getting into a pretty gray area here by making that

distinction. Yes, I do agree that there is a distinction conceptually, but implementation, it is the same, right?


I was thinking along practical lines. Using realloc on an array is not going
to get you very far

int a[5] ;
realloc(a, 4*sizeof(int)); // crash here

And I didn't want to start explaining about dynamic memory allocation until
I was sure that was what the OP needed (turned out he didn't).

john
Jul 22 '05 #17
>
I don't have a copy of C99 (or C90) Standard handy, but AFAICT, the
values of the address you pass to 'realloc' (as the first argument)
and the address you get back from 'realloc' are never the same (unless
it fails to allocate and returns NULL and you passed NULL to begin
with).


That's not true. The precise words from the C99 standard are "The realloc
function returns a pointer to the new object (which may have the same value
as a pointer to the old object) ..."

john
Jul 22 '05 #18
John Harrison wrote:
> > It is impossible to change the size of an array in C++, so you are
trying to
> > do something rather difficult.
>
> ???
>
> What is happening here then?
>
> size_t size = 2;
> int * array = (int *)malloc(sizeof(int) * size);
> array[0] = 42;
> array[1] = 9000;
> array = (int *)realloc(array, sizeof(int) * (--size));
>
> As near as I can tell, at the start, there is an array of size 2, at the end,
> it has a size of 1. Please explain what I'm missing or why it isn't
possible.

That's a pointer to allocated memory not an array. It was unclear from the OP post whether he was dealing with pointers or arrays, I said 'that's
impossible' in order to try and provoke a reaction and get him to explain himself a little more.

john


Ok, we are getting into a pretty gray area here by making that

distinction.
Yes, I do agree that there is a distinction conceptually, but

implementation,
it is the same, right?


I was thinking along practical lines. Using realloc on an array is not going
to get you very far

int a[5] ;
realloc(a, 4*sizeof(int)); // crash here

And I didn't want to start explaining about dynamic memory allocation until
I was sure that was what the OP needed (turned out he didn't).

john


Agreed and understood.
Jul 22 '05 #19

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

Similar topics

3
by: ms_chika | last post by:
Hi again to all, I just want to know how to delete array or array object in javascript? I have this problem, i have an html table that contains textboxes and delete button for each row, the...
4
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
0
by: Bandit | last post by:
I'm populating a gridview (called docList) with document info from a network folder like so: Sub Show_Files(ByVal inputDir As String) Dim strFileNamePath As String = inputDir Dim dirInfo As...
4
by: Jim Michaels | last post by:
Is it safe to do what is below? deleting from a resultset while you are processing it? I don't know how dynamic the SQL database is. I assume you get a cursor to live dataset. Even if it is a...
1
by: Pat | last post by:
Hi all, I have a really awkward situation that is causing memory leak problems. I'm passing data to a driver, and unfortunately, the driver code is not something I can change, and it is written...
13
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
One more for today.... As I add more and more lines to my RichTextBox the array that holds its strings gets bigger and bigger and the vertical scroll bar gets smaller and smaller until the...
9
by: n00b | last post by:
Hello everyone, I just had a question about deleting objects in C#. For example if I have a string array and I wanted to make it grow by one size adding the string "dd" to it. string array =...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.