Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int * getArr(int *, int *);
- void insertElemLast(int *, int, int *, int);
- void insertElemFirst(int *, int, int *, int);
- void deleteElem(int *, int, int *, int);
- int main(){
- int arrSize, arrElem, num;
- int * arr = getArr(&arrSize, &arrElem);
- int i;
- printf("\nCurrent content of the Array (SIZE: %d, Available Space: %d): ", arrSize, arrSize-arrElem);
- for(i = 0; i < arrElem; i++){
- printf("%d ", *(arr + i));
- }
- int choice = 0;
- printf("\n\nArray element manipulation table:\n");
- printf("1. Input an element and insert it to the last index of the array, if there is space.\n");
- printf("2. Input an element and insert it to the first index of the array, if there is space.\n");
- printf("3. Delete an element in the array and shift the elements to fill the deleted space.\n\n");
- while(choice < 1 || choice > 3){
- printf("\nEnter number of your choice: ");
- scanf("%d", &choice);
- }
- printf("\nEnter the element: ");
- scanf("%d", &num);
- switch (choice)
- {
- case 1:
- insertElemLast(arr, arrSize, &arrElem, num);
- break;
- case 2:
- insertElemFirst(arr, arrSize, &arrElem, num);
- break;
- case 3:
- deleteElem(arr, arrSize, &arrElem, num);
- break;
- }
- printf("\n\nNew content of the Array (SIZE: %d, Available Space: %d): ", arrSize, arrSize-arrElem);
- for(i = 0; i < arrElem; i++){
- printf("%d ", *(arr + i));
- }
- return 0;
- }
- int * getArr(int * size, int * numElem){
- printf("Enter the size of the array: ");
- scanf("%d", size);
- int * allocArr = (int*)calloc(*size, sizeof(int));
- int i;
- do{
- printf("Enter the number of integers you want to input in the array: ");
- scanf("%d", numElem);
- }while(*numElem < 1 || *numElem > *size);
- printf("Enter the integer values: ");
- for(i = 0; i < *numElem; i++){
- scanf("%d", allocArr + i);
- }
- return allocArr;
- }
- void insertElemLast(int * A, int size, int * numElem, int num){
- if(size > *numElem){
- A[*numElem] = num;
- (*numElem)++;
- }else printf("There is not enough space to add the element.");
- }
- void insertElemFirst(int * A, int size, int * numElem, int num){
- if(size > *numElem){
- int x;
- for(x = *numElem; x > 0; x--){
- A[x] = A[x-1];
- }
- A[0] = num;
- (*numElem)++;
- }else printf("There is not enough space to add the element.");
- }
- void deleteElem(int * A, int size, int * numElem, int num){
- int x;
- for(x = 0; x < *numElem && A[x] != num; x++){}
- if(x < size){
- int y;
- for(y = x + 1; y < *numElem; y++){
- A[y-1] = A[y];
- }
- A[*numElem] = 0;
- (*numElem)--;
- }else printf("Element is not found in the array.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement