Advertisement
Sierra_ONE

arrElemManipulation

Feb 15th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | Source Code | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int * getArr(int *, int *);
  5. void insertElemLast(int *, int, int *, int);
  6. void insertElemFirst(int *, int, int *, int);
  7. void deleteElem(int *, int, int *, int);
  8.  
  9. int main(){
  10.     int arrSize, arrElem, num;
  11.     int * arr = getArr(&arrSize, &arrElem);
  12.  
  13.     int i;
  14.     printf("\nCurrent content of the Array (SIZE: %d, Available Space: %d): ", arrSize, arrSize-arrElem);
  15.     for(i = 0; i < arrElem; i++){
  16.         printf("%d ", *(arr + i));
  17.     }
  18.    
  19.  
  20.     int choice = 0;
  21.     printf("\n\nArray element manipulation table:\n");
  22.     printf("1. Input an element and insert it to the last index of the array, if there is space.\n");
  23.     printf("2. Input an element and insert it to the first index of the array, if there is space.\n");
  24.     printf("3. Delete an element in the array and shift the elements to fill the deleted space.\n\n");
  25.  
  26.     while(choice < 1 || choice > 3){
  27.         printf("\nEnter number of your choice: ");
  28.         scanf("%d", &choice);
  29.     }
  30.  
  31.     printf("\nEnter the element: ");
  32.     scanf("%d", &num);
  33.  
  34.     switch (choice)
  35.     {
  36.     case 1:
  37.         insertElemLast(arr, arrSize, &arrElem, num);
  38.         break;
  39.     case 2:
  40.         insertElemFirst(arr, arrSize, &arrElem, num);
  41.         break;
  42.     case 3:
  43.         deleteElem(arr, arrSize, &arrElem, num);
  44.         break;
  45.     }
  46.  
  47.     printf("\n\nNew content of the Array (SIZE: %d, Available Space: %d): ", arrSize, arrSize-arrElem);
  48.     for(i = 0; i < arrElem; i++){
  49.         printf("%d ", *(arr + i));
  50.     }
  51.  
  52.     return 0;
  53. }
  54.  
  55. int * getArr(int * size, int * numElem){
  56.     printf("Enter the size of the array: ");
  57.     scanf("%d", size);
  58.     int * allocArr = (int*)calloc(*size, sizeof(int));
  59.  
  60.     int i;
  61.     do{
  62.         printf("Enter the number of integers you want to input in the array: ");
  63.         scanf("%d", numElem);
  64.     }while(*numElem < 1 || *numElem > *size);
  65.    
  66.     printf("Enter the integer values: ");
  67.     for(i = 0; i < *numElem; i++){
  68.         scanf("%d", allocArr + i);
  69.     }
  70.  
  71.     return allocArr;
  72. }
  73.  
  74. void insertElemLast(int * A, int size, int * numElem, int num){
  75.     if(size > *numElem){
  76.         A[*numElem] = num;
  77.         (*numElem)++;
  78.     }else printf("There is not enough space to add the element.");
  79. }
  80.  
  81. void insertElemFirst(int * A, int size, int * numElem, int num){
  82.     if(size > *numElem){
  83.         int x;
  84.         for(x = *numElem; x > 0; x--){
  85.             A[x] = A[x-1];
  86.         }
  87.         A[0] = num;
  88.         (*numElem)++;
  89.     }else printf("There is not enough space to add the element.");
  90. }
  91.  
  92. void deleteElem(int * A, int size, int * numElem, int num){
  93.     int x;
  94.     for(x = 0; x < *numElem && A[x] != num; x++){}
  95.     if(x < size){
  96.         int y;
  97.         for(y = x + 1; y < *numElem; y++){
  98.             A[y-1] = A[y];
  99.         }
  100.         A[*numElem] = 0;
  101.         (*numElem)--;
  102.     }else printf("Element is not found in the array.");
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement