"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Wednesday, January 13, 2010

Consider a 1-D array 'A' containing 100 integers. Develop a program to do the following:



(i) remove all occurrences of a given integer.
(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

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.