fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_SIZE 100
  5.  
  6. int arr[MAX_SIZE];
  7. int size = 0;
  8.  
  9. // Function prototypes
  10. void createArray();
  11. void displayArray();
  12. void insertElement();
  13. void deleteElement();
  14.  
  15. int main() {
  16. int choice;
  17.  
  18. do {
  19. printf("\nMenu:\n");
  20. printf("1. Create an Array of N Integer Elements\n");
  21. printf("2. Display Array Elements\n");
  22. printf("3. Insert an Element at a given valid Position\n");
  23. printf("4. Delete an Element at a given valid Position\n");
  24. printf("5. Exit\n");
  25. printf("Enter your choice: ");
  26. scanf("%d", &choice);
  27.  
  28. switch (choice) {
  29. case 1:
  30. createArray();
  31. break;
  32. case 2:
  33. displayArray();
  34. break;
  35. case 3:
  36. insertElement();
  37. break;
  38. case 4:
  39. deleteElement();
  40. break;
  41. case 5:
  42. printf("Exiting the program.\n");
  43. break;
  44. default:
  45. printf("Invalid choice. Please try again.\n");
  46. }
  47. } while (choice != 5);
  48.  
  49. return 0;
  50. }
  51.  
  52. void createArray() {
  53. printf("Enter the number of elements (max %d): ", MAX_SIZE);
  54. scanf("%d", &size);
  55.  
  56. if (size > MAX_SIZE || size <= 0) {
  57. printf("Invalid size. Please enter a value between 1 and %d.\n", MAX_SIZE);
  58. size = 0; // Reset size to indicate no valid array is created
  59. return;
  60. }
  61.  
  62. printf("Enter %d integer elements:\n", size);
  63. for (int i = 0; i < size; i++) {
  64. scanf("%d", &arr[i]);
  65. }
  66. printf("Array created successfully.\n");
  67. }
  68.  
  69. void displayArray() {
  70. if (size == 0) {
  71. printf("Array is empty. Please create an array first.\n");
  72. return;
  73. }
  74.  
  75. printf("Array Elements:\n");
  76. for (int i = 0; i < size; i++) {
  77. printf("%d ", arr[i]);
  78. }
  79. printf("\n");
  80. }
  81.  
  82. void insertElement() {
  83. if (size == 0) {
  84. printf("Array is empty. Please create an array first.\n");
  85. return;
  86. }
  87.  
  88. int pos, element;
  89. printf("Enter the position to insert (0 to %d): ", size);
  90. scanf("%d", &pos);
  91.  
  92. if (pos < 0 || pos > size) {
  93. printf("Invalid position. Please enter a valid position between 0 and %d.\n", size);
  94. return;
  95. }
  96.  
  97. if (size >= MAX_SIZE) {
  98. printf("Array is full. Cannot insert new element.\n");
  99. return;
  100. }
  101.  
  102. printf("Enter the element to insert: ");
  103. scanf("%d", &element);
  104.  
  105. // Shift elements to the right to make space for the new element
  106. for (int i = size; i > pos; i--) {
  107. arr[i] = arr[i - 1];
  108. }
  109. arr[pos] = element; // Insert the new element
  110. size++;
  111. printf("Element inserted successfully.\n");
  112. }
  113.  
  114. void deleteElement() {
  115. if (size == 0) {
  116. printf("Array is empty. Please create an array first.\n");
  117. return;
  118. }
  119.  
  120. int pos;
  121. printf("Enter the position to delete (0 to %d): ", size - 1);
  122. scanf("%d", &pos);
  123.  
  124. if (pos < 0 || pos >= size) {
  125. printf("Invalid position. Please enter a valid position between 0 and %d.\n", size - 1);
  126. return;
  127. }
  128.  
  129. // Shift elements to the left to fill the gap
  130. for (int i = pos; i < size - 1; i++) {
  131. arr[i] = arr[i + 1];
  132. }
  133. size--;
  134. printf("Element deleted successfully.\n");
  135. }
  136.  
Success #stdin #stdout 0.03s 25816KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

int arr[MAX_SIZE];
int size = 0;

// Function prototypes
void createArray();
void displayArray();
void insertElement();
void deleteElement();

int main() {
    int choice;

    do {
        printf("\nMenu:\n");
        printf("1. Create an Array of N Integer Elements\n");
        printf("2. Display Array Elements\n");
        printf("3. Insert an Element at a given valid Position\n");
        printf("4. Delete an Element at a given valid Position\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                createArray();
                break;
            case 2:
                displayArray();
                break;
            case 3:
                insertElement();
                break;
            case 4:
                deleteElement();
                break;
            case 5:
                printf("Exiting the program.\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != 5);

    return 0;
}

void createArray() {
    printf("Enter the number of elements (max %d): ", MAX_SIZE);
    scanf("%d", &size);

    if (size > MAX_SIZE || size <= 0) {
        printf("Invalid size. Please enter a value between 1 and %d.\n", MAX_SIZE);
        size = 0;  // Reset size to indicate no valid array is created
        return;
    }

    printf("Enter %d integer elements:\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }
    printf("Array created successfully.\n");
}

void displayArray() {
    if (size == 0) {
        printf("Array is empty. Please create an array first.\n");
        return;
    }

    printf("Array Elements:\n");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void insertElement() {
    if (size == 0) {
        printf("Array is empty. Please create an array first.\n");
        return;
    }

    int pos, element;
    printf("Enter the position to insert (0 to %d): ", size);
    scanf("%d", &pos);

    if (pos < 0 || pos > size) {
        printf("Invalid position. Please enter a valid position between 0 and %d.\n", size);
        return;
    }

    if (size >= MAX_SIZE) {
        printf("Array is full. Cannot insert new element.\n");
        return;
    }

    printf("Enter the element to insert: ");
    scanf("%d", &element);

    // Shift elements to the right to make space for the new element
    for (int i = size; i > pos; i--) {
        arr[i] = arr[i - 1];
    }
    arr[pos] = element;  // Insert the new element
    size++;
    printf("Element inserted successfully.\n");
}

void deleteElement() {
    if (size == 0) {
        printf("Array is empty. Please create an array first.\n");
        return;
    }

    int pos;
    printf("Enter the position to delete (0 to %d): ", size - 1);
    scanf("%d", &pos);

    if (pos < 0 || pos >= size) {
        printf("Invalid position. Please enter a valid position between 0 and %d.\n", size - 1);
        return;
    }

    // Shift elements to the left to fill the gap
    for (int i = pos; i < size - 1; i++) {
        arr[i] = arr[i + 1];
    }
    size--;
    printf("Element deleted successfully.\n");
}