Vous êtes sur la page 1sur 5
LAB#A» Insertion And Deletion In Linear Array Purpose of this lab cath | "Pose of this lab is to Leam insertion and deletion in Linear array in C++ Equipment/Softw: MS Visual CH lg isa container whicl.can hold a fix number of items and these ite:ns should be of the same » 7 Pe. Most of the data structures make use of arrays to implement their algorithms, Follo\ are ‘© important terms to understand the concept of Array. Element ~ Each item stored in an array is called an element. : * Index ~ Each location of an element in an array has a numerical index, which is used to identify the element. Array Representation Arrays can be declare: in various ways in different languages. For illustration, let'stake C array claration, int array (10] = { 95, 83, 42, 19, 14, 19, 27, 44, 26, 31} Arrays can be declarec in ». igus or.yin different languages. For illustration, let's take C er dectaration. Size :10 As per the above illustration, following are the important points to be considered. a ” 1 2 SN Scanned with CamScanner * Index starts with 0, Array length is 10 whieh mean: * Each element ean be aece index 6 as 9 CAN stare d vid ity index 10 clement For eximp ¢, we can fetch an el Basie Operations Following are the ‘operations supported by inray. + Traverse print all the array elements one by one o Unserti n ~ Adds an element at the given index : Deletion Deletes an element at the given index Search ~ Searches an element using the given ielex or by the value, Procedure: Insert Element in Array in C++ To inser A array get clement in an array in C-++ programming, you have to ask to the user to enter the aa Size and array elements and ask to the user to enter the element (with their position) to ert the element at desired position in the array. Afier inserting the element at the desired position in the array, display the new array on the Screen as shown here in the following program, Cte Programming Code to Insert Element in Array Following C++ program ask to the user to enter array size, then ask to the user to enter array element, then ask to the user to enter clement or number to be insert, then at last it will ask to the User to enter the position (index number) where he or she want to insert the desired element in the array, so this program insert the desired element and display the new array on the screen after inserting the element: /* C++ Program - Insert Element in Array */ #includesiostream.h> Hinclude void main() { elrscr(); int arr[50], size, insert, i, pos; cout<<"Enter Array Size :"; cin>>size; cout<<"Enter array elements : "; Scanned with CamScanner issize; i++) cin>>arr{i); COUL<<"Enter element to be insert: cin>> insert; COuL<<"At which position (Enter index numer) 7% cin>>pos; mber) 2"; 1 now create as for(i=size; i>pos; ce at the required position ) arr{iJarr{i-1); arr{pos]=insert; cout<<"Element inserted successfully..1!\n" Cout<<"Now the new array is : \n'"; for(i=0; i #include void main() { clrser(); int arr[50], size, i, del, count cout<<"Enter array size cin>>size; Scanned with CamScanner { ) cin>>arr[i; COULSS"Enter clement to be delete :". ifarr|i}-=del) . Fon(int j Ij) { ant [jJ=arrfjtl]; count+; break; } } if(count==0) { cout<<"Element not found. } else { cout<<"Element deleted successfully..!!\n", cout<<"Now the new array for(i=0; i<(size-1); i++) { cout<

Vous aimerez peut-être aussi