(ii) shift the elements of the array to the right so that used space is available at the left end
(iii) fill the unused spaces by 0 (zero)
(For example , the array 10 | 20 | 15 | 4 | 20 |2 | 20 after execution of the program for given integer 20 should become
0 | 0 | 0 | 10 | 15 | 4 | 2)
======================================
#include <iostream.h>
#include <conio.h>
void main()
{
int A[100], B[100], i , j, n;
cout << "\n Enter the elements in an array:";
for( i = 0; i<=99; i++)
cin>>A[i];
cout<< "\n Enter an element to be removed from array :";
cin>>n;
//remove all occurrences of a given integer.
j = 0;
for(i = n-1; i>0; i--)
{
if(n != A[i])
B[j++] = A[i];
}
//right shift and store zeros at the beginning of the list
for (i = j; i>0; i --)
B[i] =0;
// Display Original
for(i = 0; i< n ; i++)
cout<< A[i] << " ";
// Change the Source Array
for(i = 0; i< n; i++)
A[i] = B[i];
//Display the changed array after right-shift
for(i = 0; i< n ; i++)
cout<< A[i] << " ";
getch();
}
No comments:
Post a Comment