Write a C program which removes a specific element V from an
Write a C++ program which removes a specific element, V, from an integer array, whose memory is dynamically allocated. The size of the array, its elements and the number V are input by the user. The program should remove the first instance of V from the array, shift the following elements to the left and add a 0 to the last element. If V is not found in the array then no change should be made to it. Finally, display all elements of the array. Use top-down design and break the program\'s task into subtasks. The output of the program should be exactly the same as shown in the examples below.
Example 1
Array size: 5
Integer number 1: 4
Integer number 2: 3
Integer number 3: 7
Integer number 4: 2
Integer number 5: 1
Value to remove: 7
4
3
2
1
0
Example 2
Array size: 3
Input integer number 1: 6
Input integer number 2: 2
Input integer number 3: 5
Input the value to remove: 4
6
2
5
Solution
#include <iostream>
#include <new>
using namespace std;
int main ()
{
int i,n;
int * p;
int rem;
cout << \"Array size: \";
cin >> i;
p= new (nothrow) int[i];
if (p == NULL)
cout << \"Error: memory could not be allocated\";
else
{
for (n=0; n<i; n++)
{
cout << \"Input integer number \"<<(n+1)<<\": \";
cin >> p[n];
}
cout<<\"Value to remove: \";
cin>>rem;
int flag=0,pos;
for (n=0; n<i; n++)
{
if(p[n]==rem)
{
flag=1;
pos=n;
break;
}
else
{
flag=0;
}
}
if(flag)
{
for (n=0; n<i; n++)
{
if(n<pos)
{
p[n];
}
else if(n>=pos)
{
p[n]=p[n+1];
}
}
p[i-1]=0;
}
for (n=0; n<i; n++)
cout << p[n] << endl;
delete[] p;
}
return 0;
}
OUTPUT 1:
Array size: 5
Input integer number 1: 6
Input integer number 2: 5
Input integer number 3: 7
Input integer number 4: 4
Input integer number 5: 8
Value to remove: 2
6
5
7
4
8
OUTPUT 2:
Array size: 5
Input integer number 1: 3
Input integer number 2: 4
Input integer number 3: 2
Input integer number 4: 7
Input integer number 5: 6
Value to remove: 2
3
4
7
6
0


