0% found this document useful (0 votes)
3 views

Array Operations

The document contains a C program that implements basic array operations such as insertion, deletion, linear search, bubble sort, and display. It defines functions for each operation and provides a menu-driven interface for user interaction. The program ensures that operations are performed within the bounds of the array and handles errors accordingly.

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Array Operations

The document contains a C program that implements basic array operations such as insertion, deletion, linear search, bubble sort, and display. It defines functions for each operation and provides a menu-driven interface for user interaction. The program ensures that operations are performed within the bounds of the array and handles errors accordingly.

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#define MAX 100

void insert(int arr[], int *size, int element, int position) {


if (*size >= MAX) {
printf("Array is full! Cannot insert.\n");
return;
}
if (position < 0 || position > *size) {
printf("Invalid position!\n");
return;
}
for (int i = *size; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element;
(*size)++;
}

void delete(int arr[], int *size, int position) {


if (*size <= 0) {
printf("Array is empty! Cannot delete.\n");
return;
}
if (position < 0 || position >= *size) {
printf("Invalid position!\n");
return;
}
for (int i = position; i < *size - 1; i++) {
arr[i] = arr[i + 1];
}
(*size)--;
}

int linear_search(int arr[], int size, int key) {


for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}

void bubble_sort(int arr[], int size) {


for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void display(int arr[], int size) {


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

int main() {
int arr[MAX], size = 0, choice, element, position, key;

while (1) {
printf("\nMenu:\n");
printf("1. Insert Element\n");
printf("2. Delete Element\n");
printf("3. Search Element\n");
printf("4. Sort Array\n");
printf("5. Display Array\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter element to insert: ");
scanf("%d", &element);
printf("Enter position (0-based index): ");
scanf("%d", &position);
insert(arr, &size, element, position);
break;
case 2:
printf("Enter position to delete (0-based index): ");
scanf("%d", &position);
delete(arr, &size, position);
break;
case 3:
printf("Enter element to search: ");
scanf("%d", &key);
position = linear_search(arr, size, key);
if (position != -1)
printf("Element found at index %d\n", position);
else
printf("Element not found!\n");
break;
case 4:
bubble_sort(arr, size);
printf("Array sorted.\n");
break;
case 5:
display(arr, size);
break;
case 6:
return 0;
default:
printf("Invalid choice!\n");
}
}
}

You might also like