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

Check out this stream