Finding And Deleteing The Required Value From Linear Array (Unsorted)

Now many folks out there studying c++ would initially always find difficult to delete values from the unsorted array. The deleting of value from the sorted array in c++ is quit easy and quick through binary search(will be uploading it soon) but searching the value which comes multiple times in the unsorted linear array is a bit tricky for young students who have started learning programming. But here is the solution i am uploading to make the home work look easy.

Starting with the global values which should be placed before the main function.
Remember we are not using a dynamic array so we have to declare the total values of the array to be used later in the program. Muneeb is my friend name so i am using it as a array name muneeb[] you can use any name.Total_Accurance is the number of time the value to be searched comes in the array initially it is declared 0.

int Total_Value=10;
int muneeb[]={100,200,300,400,500,600,400,800,900,1000,400};
int Total_Accurance=0;

Display Array function will be used to display the values of array.

void DisplayArray()
{
        cout<<“The values in the array are :”<<endl;
        for (int q=0;q<=Total_Value;q++)
        cout<<q<<“-)  “<<” ” <<muneeb[q]<<endl;
}

int SearchItem(int Value)
{
 int index=-1;
  for (int r=0;r<=Total_Value;r++)
  {  
      if (muneeb[r]==Value)
        {
            index=r;
            Total_Accurance++;
            
        }
  }
  return index;
}

int DeleteFrom(int Loc)
{
    if(Loc>Total_Value)
        return -1;
    else
    {
        int a=muneeb[Loc];
        for(Loc;Loc<=Total_Value;Loc++)
    {
     muneeb[Loc]=muneeb[Loc+1];
    }
       Total_Value–;
        return a;
    }
}

int DeleteItemByValue(int Item)
{
    SearchItem(Item);
    const int c=Total_Accurance;
    if(c==0)
        return -1;
    else
    {
    for(int a=0;a<c;a++)
    {
         int b=SearchItem(Item);
        {
           DeleteFrom(b);
        }  
    }  
    return c;
    }

As in c++ the methods are declared twice so here they are .

void DisplayArray();
int SearchItem(int Value);
int DeleteFrom(int Loc);
int DeleteItemByValue(int Item);

This is the start of main function,

void main()
{

 cout<<“Enter the Value to be deleted from multiple locations”<<endl;
     cin>>Value;
     cout<<endl;
     int a=DeleteItemByValue(Value);
     if(a==-1)
     {
         cout<<“Value not found in the array”<<endl;
     }
     else
     {
          cout<<“Value “<<Value<<” is deleted.”<<endl;
          cout<<” It came for a total of ” <<a<<” times in the array. “<<endl;
          cout<<endl<<endl<<endl;

     cout<<“******* Finnaly the array is in the following form **************”<<endl;
     DisplayArray();
     }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s