delete a value in an array

Hi ! I need help here .
I have to write a function that delete a value of one element in the array , and shift the elemnts that follows it , for example :
the values in the array are : 54 25 99 10
and if i want to delete the value 25 it has to become : 54 99 10
also i have to include a counter to count the values that i deleted it

so i did this function , but sth is missing and idk what !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void deleteByValue(int a[], int &size)
{
	int value, count=0;
	cout <<"Eneter value of element to be deleted: ";
	cin >> value;
	for( int i=0; i<=size-1; i++ )
		if (value == a[i])
		{
			count++;
			a[i]=a[i+1];
			size--;

		}
		
		cout << count << " numbers were deleted. " << endl << "Done!!" <<endl;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void deleteByValue(int a[], int &size)
{
	int value, count=0;
	cout <<"Eneter value of element to be deleted: ";
	cin >> value;
	for( int i=0; i<=size-1; i++ )
		if (value == a[i])
		{
			count++;
                        for (int j=i;j<size-1;j++)
			      a[j]=a[j+1];
			size--;
                        i--;

		}
		
		cout << count << " numbers were deleted. " << endl << "Done!!" <<endl;

}
Last edited on
By the way there is standard algorithm std::remove in C++ that does exactly what you want.:)
Thank you so muck , it worked :D
You may want to check an insure that your code will remove a duplicate that is next to each other in the array. Try your code with "54 25 25 99" as your array.

I modifyed the code inserting a i-- to fix this error... Thanks @JLB... :)

i didn't notice that problem in the beginning , if i wanted to delete two values that follows each other.

thanks all for help =)
Topic archived. No new replies allowed.