C++ Programming Code Examples C++ > Arrays and Matrices Code Examples Insert update delete element in array Insert update delete element in array Write a c++ program which manages data in array having insert, update and delete value functions. Program should be menu based properly asking for user to enter an option and on wrong or invalid input program should display proper message and ask user to enter value again. This program is helpful to manage a menu base database using array and shows how to prevent from a wrong input. Its is of very basic level to introduce the simplest database management using array. It is recommended to copy the code make changes in it and examine output. 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 #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(){ cout<<"Defalult Values :"<<endl; for(int i=0;i<10;i++) { array[i]=-1; cout<<"array ["<<i<<"]"<<"= "<<array[i]<<endl; } } void InsertValues(){ cout<<"Enter 10 Values "<<endl; for(int i=0;i<10;i++) { cin>>array[i]; } cout<<"\n\t\t\tArray 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<<"\n\t\t\tArray 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<<"\n\t\t\tArray Updated... Successfully "<<endl; } } int main() { char option; SetDefaultValues(); do { cout<<"\t\t\tEnter 1 to Enter Values\n\t\t\tEnter 2 to Update Values\n\t\t\tEnter 3 to Delete Values\n\n\t\t\t or Enter E to EXIT\n\n\t\t\t Enter 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<<"\n\n\t\t\tSelect A Valid Option From Below\n\n"; } }while(option!='e'&&option!='E'); system("cls");// To Clear The Screen cout<<"\n\n\n\n\n\n\n\n\n\n\t\tProgram Ended Press Any Key To Exit Screen.....\n\n\n\n\n\n\n\n\n\n\n\n"<<endl; return 0; }