tampurus

1 Array operation

Nov 25th, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. /*
  2. Input and Output :
  3. Enter the number of element in array 2
  4. 1 2
  5. I for instertion and D for deletion
  6. or any key to just print the array  
  7. i
  8.  
  9.  
  10. Possition where you want to insert
  11. 2
  12. Now enter the element
  13. 0
  14. 1 0 2
  15. */
  16. #include<stdio.h>
  17. #include<conio.h>
  18. void Print(int [],int);
  19. void Insertion(int [],int);
  20. void Deletion(int [],int);
  21. int main(){
  22.     int arr[20],n;
  23.     printf("Enter the number of element in array ");
  24.     scanf("%d",&n);
  25.     for(int i=0 ; i<n ; i++){
  26.         scanf("%d",&arr[i]);
  27.     }
  28.     fflush(stdin);
  29.     printf("I for instertion and D for deletion \nor any key to just print the array\n");
  30.     char c; scanf("%c",&c);
  31.     if(c=='I'|| c=='i'){
  32.         Insertion(arr,n+1);
  33.     }
  34.     else if(c=='D' || c=='d'){
  35.         Deletion(arr,n);
  36.     }
  37.     else
  38.         Print(arr,n);
  39.     return 0;
  40. }
  41. void Insertion(int arr[],int n){
  42.     int pos,element;
  43.     printf("\n\nPossition where you want to insert\n");
  44.     scanf("%d",&pos);
  45.     printf("Now enter the element\n");
  46.     scanf("%d",&element);
  47.     for(int i=n-2 ; i>=pos-1 ; i--){
  48.         arr[i+1] = arr[i];
  49.     }
  50.     arr[pos-1]=element;
  51.     Print(arr,n);
  52. }
  53. void Deletion(int arr[],int n){
  54.     int pos;
  55.     printf("Enter possition want to delete\n");
  56.     scanf("%d",&pos);
  57.     for(int i=pos-1 ; i<n-1 ; i++){
  58.         arr[i]=arr[i+1];
  59.     }
  60.     Print(arr,n-1);
  61. }
  62. void Print(int arr[],int n){
  63.     for(int i=0 ; i<n; i++){
  64.         printf("%d ",arr[i]);
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment