0% found this document useful (0 votes)
3 views6 pages

DSA 1

This C program provides a menu-driven interface for users to insert, delete, and display elements in a fixed-size array. It includes error handling for invalid positions and full or empty arrays. The program can be enhanced by implementing dynamic memory allocation to overcome the limitations of a fixed array size.

Uploaded by

anjalipawar0320
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

DSA 1

This C program provides a menu-driven interface for users to insert, delete, and display elements in a fixed-size array. It includes error handling for invalid positions and full or empty arrays. The program can be enhanced by implementing dynamic memory allocation to overcome the limitations of a fixed array size.

Uploaded by

anjalipawar0320
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Atharva Pawar

AIDS/B/67

EXPERIMENT NO: -01


Program code:
#include <stdio.h>

#define MAX_SIZE

100

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

if (*size >= MAX_SIZE) {

printf("Error: Array is full. Cannot insert element.\n");

return;

if (position < 1 || position > *size + 1)

{ printf("Error: Invalid position.\n");

return;

for (int i = *size; i >= position; i--)

{ arr[i] = arr[i - 1];

arr[position - 1] = element; (*size)

++;

printf("Element inserted successfully.\n");

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

if (*size == 0) {

printf("Error: Array is empty. Cannot delete element.\n");

return;
}

if (position < 1 || position > *size)

{ printf("Error: Invalid position.\

n"); return;

for (int i = position - 1; i < *size - 1; i++)

{ arr[i] = arr[i + 1];

(*size)--;

printf("Element deleted successfully.\n");

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

if (size == 0) {

printf("Array is empty.\

n"); return;

printf("Array elements:

"); for (int i = 0; i < size; i+

+) {

printf("%d ", arr[i]);

printf("\n");

int main() {

int

arr[MAX_SIZE];

int size = 0;

int choice, element,

position; while (1) {


printf("\nMenu:\n");

printf("1. Insert element\n");

printf("2. Delete element\

n"); printf("3. Display array\

n"); printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

{ case 1:

printf("Enter the element to insert: ");

scanf("%d", &element);

printf("Enter the position to insert the element: ");

scanf("%d", &position);

insertAtPosition(arr, &size, element,

position); break;

case 2:

printf("Enter the position to delete the element: ");

scanf("%d", &position);

deleteAtPosition(arr, &size,

position); break;

case 3:

displayArray(arr,

size); break;

case 4:

printf("Exiting the program.\

n"); return 0;

default:
printf("Invalid choice. Please try again.\n");

return 0;

Output:
Conclusion:
This C program allows users to insert, delete, and display elements in an array using a menu-
driven approach. It ensures proper bounds checking and handles errors effectively. However, it is
limited by a fixed array size and can be improved with dynamic memory allocation.

You might also like