Solved programming problem

Array Insert,Delete,Access and Search in C++

Source code

#include <iostream>

#define MAX 100 ///define array size

using namespace std;

int array[MAX],size=0;

void insert_value()

{

    int data;

    if(size>=MAX) ///overflow 

    {

        cout<<"
Your Array is Full"<<endl;

        return ;

    }

    cout<<"
Data: ";

    cin>>data;

    array[size++]=data; ///save the data in size index then increase it

    return;

}

void delete_value()

{

    int data,index=-5;

    cout<<"
Insert The value you want to Delete: ";

    cin>>data;

    for(int i=0;i<size;i++) ///find the value

    {

        if(array[i]==data) ///if found save the index and break

        {

            index=i;

            break;

        }

    }

    if(index==-5) ///if index value is not change 

    {

        cout<<"No Data Found"<<endl;

        return;

    }

    for(int i=0;i<size;i++) 

    {

        if(i>=index) /// replace all the value from index  + 1

        {

          array[i] = array[i+1];

        }

    }

    size--;

    cout<<data<<" Deleted "<<endl;

}

void access_value()

{

    int index,data;

    cout<<"
Insert The index You want to Access: ";

    cin>>index;

    if(index<0 || index>=size) ///check if the index is valid or not

    { 

        cout<<"
Wrong Index"<<endl;

        return;

    }

    cout<<"
Insert the Data: ";

    cin>>data;

    array[index] = data; ///update the index

    cout<<"
Array Updated"<<endl;

}

void search_value()

{

    int data,index=-5;

    cout<<"
Insert The value you want to Search: ";

    cin>>data;

    for(int i=0;i<size;i++)

    {

        if(array[i]==data) //if found save the index

        {

            index=i;

            break;

        }

    }

    if(index==-5) ///if index value is not change the value is not found

    {

        cout<<"No Data Found"<<endl;

        return;

    }

    cout<<"Data Found"<<endl<<"Index: "<<index<<endl;

    return;

}

void print() ///array print

{ 

   if(size<0)

   {

       cout<<"Array is Empty"<<endl;

       return;

   }

   cout<<"Your array: ";

   for(int i=0; i<size;  i++)

   {

       cout<<array[i]<<" ";

   }

   return;

}

int menu() ///menu function

{

    int n;

    while(1)

    {

        cout<<"
Press 1 to Insert
2 to Delete
3 to Access
4 to Search
5 to Print
6 to Exit"<<endl;

        cout<<"Your Choice: ";

        cin>>n;

        if(n==1)

        {

          insert_value();

        }

        else if(n==2)

        {

          delete_value();

        }

        else if(n==3)

        {

         access_value();

        }

        else if(n==4)

        {

          search_value();

        }

        else if(n==5)

        {

            print();

        }

        else if(n==6)

        {

           return 0;

        }

        else

        {

            cout<<"Wrong Choice!!!"<<endl;

        }

    }

}

int main()

{

    menu();

    return 0;

}
Join the discussion

Add your comment