Question:
How do I delete an element in an array in C++?
2013-01-31 13:18:49 UTC
I am writing a program that takes two Arrays, A and B, and merges them. But in the merge_array it should also delete any duplicates. What is wrong with my code (particularly the "delete c[j]" part? I'm using pointers.

//This program merges two arrays into and deletes duplicate entries
#include
using namespace std;

void merge_arrays (double[], double[], double *, int);
//Preconditon: Array accepts two full and one empty
//Postcondition: Merges the arrays into the empty one

int main()
{
double *a, *b, *c; // declares three pointer variables of double
int i, size = 0;

a = new double [5]; //declares an array of 5
b = new double [5]; //declares an array of 5

cout << "Enter 5 values for the A array" << endl << endl;

for (i = 0; i < 5; i++) //loops to allow user to input elements
{
cin >> a[i];
size += 1; //increment size by 1
}

cout << endl << "Now enter 5 values for the B array" << endl << endl;

for (i = 0; i < 5; i++) //loops to allow user to input elements
{
cin >> b[i];
size +=1; //increment size by 1
}

c = new double [size]; //declares an array of "size" size

merge_arrays (a, b, c, size); //passes arrays and size to function

cout << endl << "The C Array contains:" << endl << endl;
for (i = 0; i < size; i++) //outputs the new array
{
cout << c[i] << " ";
}

cout << endl;
return 0;
}

void merge_arrays (double a[], double b[], double *c, int size)
{
int i, j;
double key;

for (i = 0; i < 5; i++) //loops for 5 elements to allow A to be input to C
{
c[i] = a[i];
}

for (i = 0; i < 5; i++) //loops for 5 elements to allow B to be input to C
{
c[size-(5-i)] = b[i]; //c[10-(end of last point - position of index)]
}

for (i = 0; i < size; i++) //looks for duplicates in the C array
{
key = c[i];
for (j = i+1; j < size; j++)
{
if (c[j] = key)
{
delete c[j];
}
}
}
}
Three answers:
Mowji
2013-01-31 13:57:19 UTC
Array itself, is a pointer, but its elements are not.They are just doubles. To delete j-th number in array c you can shift left the numbers with indexes larger than j.

Or you can simply check for duplicate numbers when you are copying arrays to array c.



void merge_arrays (double a[], double b[], double *c, int size)

{

int i, j, k;

int exists;



k = 0;



// copying numbers of array a which are not in array b

for (i = 0; i < 5; i++)

{

exists = 0;

for (j = 0; j < 5; j++)

{

if (b[j] == a[i])

{

exists = 1;

break;

}

}

if (!exists)

{

c[k] = a[i];

k++;

}

}



// copying all numbers of array b

for (i = 0; i < 5; i++)

{

c[k + i] = b[i];

}



}
Kaydell
2013-01-31 23:28:48 UTC
Yeah, delete is used to deallocate memory. For example, if you had an array of objects that were allocated using the new operator, then you'd want to delete them. In this case, we have arrays of doubles which don't need to be deallocated individually.



I think that the best way to accomplish what you need to do is to not put duplicates into array c in the first place, but I went ahead and wrote a function called "deleteElement". It just slides everything down in the array, replacing the element to be deleted with the next element in the array.



There's one thing about eliminating duplicates any way that you do it and that is that there are two sizes. 1) the maximum size of the array, and 2) the number of elements of the array that are actually valid. This seems like a good place to talk about having an object of a class that could keep track of both the elements and the number of elements being used.



http://ideone.com/d9fKkM
2013-01-31 23:01:30 UTC
you cant use "delete" that way, you can only use delete on the entire memory at the address passed by "new" . if you come across a duplicate, just ignore it and go to the next element.



You can delete a and b when you are done, if you prefer.



btw, a and b are not technically arrays, they are dynamically allocated during run-time.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...