W.A.P. C++

WAP | CPP | Programs in C++ | C++ Solutions

Write a program to Insert and Delete elemnt of an array


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 :)
  1. #include<conio.h>
  2. #include<iostream.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[100],i,pos,num,item,size;
  7. cout<<"How many element: ";
  8. cin>>size;
  9. cout<<"Enter the elements of array: "<<endl;
  10. for(i=0;i<size;i++)
  11. cin>>a[i];
  12. cout<<endl;
  13. cout<<"Now Array is: "<<endl;
  14. for(i=0;i<size;i++)
  15. cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
  16. cout<<endl<<"Press any key to add another element"<<endl<<endl;
  17. getch();
  18. //Coding by: Snehil Khanor
  19. //http://WapCPP.blogspot.com
  20. cout<<"Enter another element: ";
  21. cin>>num;
  22. cout<<"Enter position number: ";
  23. cin>>pos;
  24. cout<<endl;
  25. --pos;
  26. for(i=size;i>=pos;i--)
  27. a[i+1]=a[i];
  28. a[pos]=num;
  29. size++;
  30. cout<<"New Array: "<<endl;
  31. for(i=0;i<size;i++)
  32. cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
  33. cout<<endl<<"Press any key to delete any element"<<endl<<endl;
  34. getch();
  35. //Coding by: Snehil Khanor
  36. //http://WapCPP.blogspot.com
  37. cout<<"Enter position number to delete: ";
  38. cin>>pos;
  39. --pos;
  40. item=a[pos];
  41. for(i=pos;i<size;i++)
  42. a[i]=a[i+1];
  43. size--;
  44. cout<<"Deleted element: "<<item<<endl;
  45. cout<<endl<<"New array: "<<endl;
  46. for(i=0;i<size;i++)
  47. cout<<"Element at position "<<i+1<<" is "<<a[i]<<endl;
  48. getch();
  49. }

3 comments:

Vikram February 11, 2010 at 11:28 AM  

please send code of insertion sort or selection sort

Anonymous January 8, 2012 at 8:18 PM  

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....

RDZMUZIK November 18, 2013 at 4:04 PM  

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..

Search

About WAP C++

Here you'll find a wide range of programs' solution ranging from beginer level to advanced level.
All programs here are made, compiled and tested by me..and are running absolutely fine.. still if you find any bug in any program do let me know :)

Followers

Subscribe via email

Enter your email address:

Delivered by FeedBurner

my Binary Log

Recent Posts

Blog Archive



eXTReMe Tracker