Program to Insert Update and Delete elements in Array in C++

How it Works:

  • This code is managing a small database in array like insert, update and delete integer values in array.
  • Program has global array declaration of size 10. Global declaration allows it accessible inside any function.
  • A function name “Default values” is initializing all array indexes by default value of -1.
  • A function name “Display Array” displays the array values using for loop.
  • To manage the menu option do while is used and few if else statement within the loop to call a specific function on user selection.
  • To restrict user to enter valid option recursion is used in two functions so if user enter invalid option For example array size is 10 and user select index number 11 then a message will be displayed function will call itself and user has to enter the option again.

Follow is the Program to Insert Update and Delete elements in Array in C++

 

#include <iostream>
#include <stdlib.h>
using namespace std;

//Global Array Declaration
int array[10];
void DisplayArray()
{
    for (int i=0;i<10;i++)
        cout<< "Array [ "<<i<<" ] = "<<array[i]<<endl;
}

void SetDefaultValues()
{
    for(int i=0;i<10;i++)
        array[i]=-1;
}

void InsertValues()
{
    cout<<"Enter 10 Values "<<endl;
    for(int i=0;i<10;i++)
    {
        cin>>array[i];
    }
    cout<<"ntArray Values Inserted...  Successfully "<<endl;
}

void DeleteValues()
{
    cout<<"Enter the Index Number To Delete Value :";
    int index;
    cin>>index;
    if(index>9||index<0)
    {
        cout<<"Invalid Index Entered-> Valid Range(0-9)"<<endl;
        DeleteValues();// Recall The Function it self
    }
    else
    {
        array[index]=-1;
    }
    cout<<"ntArray Value Deleted...  Successfully "<<endl;
}

void UpdateValues()
{
    cout<<"Enter Index Number to Update Value :";
    int index;
    cin>>index;
    if(index>9||index<0)
    {
        cout<<"Invalid Index Entered-> Valid Range(0-9)"<<endl;
        UpdateValues();// Recall The Function it self
    }
    else
    {
        cout<<"Enter the New Value For Index array[ "<<index<<" ] = ";
        cin>>array[index];
        cout<<"ntttArray Updated...  Successfully "<<endl;
    }
}

int main()
{
    char option;
    SetDefaultValues();

    do
    {
        cout<<"Enter 1 to Enter  ValuesnEnter 2 to Update ValuesnEnter 3 to Delete Valuesnn or Enter E to EXITnnEnter Option: ->  ";
        cin>>option;
        if(option=='1')
        {
            cout<<"Insert Function Called"<<endl;
            InsertValues();
            cout<<"Inserted Values :"<<endl;
            DisplayArray();
        }
        else if(option=='2')
        {
            UpdateValues();
            cout<<"Updated Array :"<<endl;
            DisplayArray();
        }
        else if(option=='3')
        {
            DeleteValues();
            cout<<"Array After Deleting Values :"<<endl;
            DisplayArray();
        }
        else if(option!='e'&&option!='E')
        {
            cout<<"Select A Valid Option From Belownn";
        }
    }while(option!='e'&&option!='E');

    system("cls");// To Clear The Screen
    cout<<"Program Ended Press Any Key To Exit Screen.....nnnnnnnnnnnn"<<endl;

    return 0;
}

Sample Output:

Enter 1 to Enter  Values
Enter 2 to Update Values
Enter 3 to Delete Values

 or Enter E to EXIT

Enter Option: ->  1
Insert Function Called
Enter 10 Values

 

Leave a Reply