Size of array is taken from user.. Elements are Entered.. Array is shown.. user is prompted for adding another element.. new element is added at desired position.. user is prompted to delete an element.. Element is deleted from entered position..
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
- #include<conio.h>
- #include<iostream.h>
- void main()
- {
- clrscr();
- int a[100],i,pos,num,item,size;
- cout<<"How many element: ";
- cin>>size;
- cout<<"Enter the elements of array: "<<endl;
- for(i=0;i<size;i++)
- cin>>a[i];
- cout<<endl;
- cout<<"Now Array is: "<<endl;
- for(i=0;i<size;i++)
- cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
- cout<<endl<<"Press any key to add another element"<<endl<<endl;
- getch();
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- cout<<"Enter another element: ";
- cin>>num;
- cout<<"Enter position number: ";
- cin>>pos;
- cout<<endl;
- --pos;
- for(i=size;i>=pos;i--)
- a[i+1]=a[i];
- a[pos]=num;
- size++;
- cout<<"New Array: "<<endl;
- for(i=0;i<size;i++)
- cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
- cout<<endl<<"Press any key to delete any element"<<endl<<endl;
- getch();
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- cout<<"Enter position number to delete: ";
- cin>>pos;
- --pos;
- item=a[pos];
- for(i=pos;i<size;i++)
- a[i]=a[i+1];
- size--;
- cout<<"Deleted element: "<<item<<endl;
- cout<<endl<<"New array: "<<endl;
- for(i=0;i<size;i++)
- cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
- getch();
- }
7 comments:
please send code of insertion sort or selection sort
if you r giving position then the element has to be necessarily be inserted at that position itself but your code is wrong output by inserting element after the given position....
How can we insert an element into an array which has elements in ascending order..the entered element should be added to a place where the arrangement is not disturbed ..
Eg:
enter array:1
2
4
enter element to be inserted:3
new array is:1
2
3
4
please help to write a code for this..
the program is executable and very nice ...thnx a lot...the insertion sort program is available on other sites and best luck
Delete an Element from Array in C++
Thanks, its helpful
Can you join these two coding for the insertion or deletion of an array
Model out put snd
Post a Comment