fork download
  1. #include <stdio.h>
  2. #define MAx 10
  3.  
  4. int search(int arr[], int n, int element) {
  5. for (int i = 0; i < n; i++) {
  6. if (arr[i] == element)
  7. return i;
  8. }
  9. return -1;
  10. }
  11.  
  12. void insert_first(int arr[], int n, int z) {
  13. for (int i = n; i > 0; i--) {
  14. arr[i] = arr[i - 1];
  15. }
  16. arr[0] = z;
  17. }
  18.  
  19. int insert_at_position(int arr[], int n, int pos, int val) {
  20. if (pos < 0 || pos > n) {
  21. printf("Invalid position.\n");
  22. return n;
  23. }
  24. for (int i = n; i > pos; i--) {
  25. arr[i] = arr[i - 1];
  26. }
  27. arr[pos] = val;
  28. return n + 1;
  29. }
  30.  
  31. int delete_last(int n) {
  32. if (n > 0)
  33. return n - 1;
  34. printf("Array is already empty.\n");
  35. return n;
  36. }
  37.  
  38. int delete_at_position(int arr[], int n, int pos) {
  39. if (pos < 0 || pos >= n) {
  40. printf("Invalid position.\n");
  41. return n;
  42. }
  43. for (int i = pos; i < n - 1; i++) {
  44. arr[i] = arr[i + 1];
  45. }
  46. return n - 1;
  47. }
  48.  
  49. void print(int arr[], int n) {
  50. if (n == 0) {
  51. printf("Array is empty.\n");
  52. return;
  53. }
  54. for (int i = 0; i < n; i++) {
  55. printf("%d ", arr[i]);
  56. }
  57. printf("\n");
  58. }
  59.  
  60. int main() {
  61. int n;
  62. printf("Enter the size of array: ");
  63. scanf("%d", &n);
  64.  
  65. if (n > MAx || n < 0) {
  66. printf("Size exceeds maximum limits.\n");
  67. return 1;
  68. }
  69.  
  70. int arr[MAx];
  71. printf("Give elements:\n");
  72. for (int i = 0; i < n; i++) {
  73. scanf("%d", &arr[i]);
  74. }
  75.  
  76. int choice;
  77. do {
  78. printf("\nMenu:\n");
  79. printf("1. Search for element\n");
  80. printf("2. Insert at first\n");
  81. printf("3. Insert at position\n");
  82. printf("4. Delete last element\n");
  83. printf("5. Delete at position\n");
  84. printf("6. View array\n");
  85. printf("0. Exit\n");
  86.  
  87. printf("Enter your choice: ");
  88. scanf("%d", &choice);
  89.  
  90. int x, pos;
  91. switch (choice) {
  92. case 1:
  93. printf("Value you want to search: ");
  94. scanf("%d", &x);
  95. pos = search(arr, n, x);
  96. if (pos == -1)
  97. printf("Not found\n");
  98. else
  99. printf("Found at position %d\n", pos + 1);
  100. break;
  101.  
  102. case 2:
  103. if (n >= MAx) {
  104. printf("Array is full.\n");
  105. break;
  106. }
  107. printf("Enter value to insert at first: ");
  108. scanf("%d", &x);
  109. insert_first(arr, n, x);
  110. n++;
  111. print(arr, n);
  112. break;
  113.  
  114. case 3:
  115. if (n >= MAx) {
  116. printf("Array is full.\n");
  117. break;
  118. }
  119. printf("Enter position (0 to %d) and value: ", n);
  120. scanf("%d %d", &pos, &x);
  121. n = insert_at_position(arr, n, pos, x);
  122. print(arr, n);
  123. break;
  124.  
  125. case 4:
  126. n = delete_last(n);
  127. print(arr, n);
  128. break;
  129.  
  130. case 5:
  131. printf("Enter position to delete (0 to %d): ", n - 1);
  132. scanf("%d", &pos);
  133. n = delete_at_position(arr, n, pos);
  134. print(arr, n);
  135. break;
  136.  
  137. case 6:
  138. print(arr, n);
  139. break;
  140.  
  141. case 0:
  142. printf("Exiting program.\n");
  143. break;
  144.  
  145. default:
  146. printf("Invalid choice. Try again.\n");
  147. }
  148.  
  149. } while (choice != 0);
  150.  
  151. return 0;
  152. }
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
Enter the size of array: Give elements:

Menu:
1. Search for element
2. Insert at first
3. Insert at position
4. Delete last element
5. Delete at position
6. View array
0. Exit
Enter your choice: Exiting program.