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

Array Operations

This document contains a C program that implements basic array operations such as creating an array, inserting elements at the beginning, middle, or end, deleting an element, and displaying the array. The program uses a menu-driven approach to allow users to choose the desired operation. It handles user input and performs the corresponding actions on a statically allocated array of integers.

Uploaded by

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

Array Operations

This document contains a C program that implements basic array operations such as creating an array, inserting elements at the beginning, middle, or end, deleting an element, and displaying the array. The program uses a menu-driven approach to allow users to choose the desired operation. It handles user input and performs the corresponding actions on a statically allocated array of integers.

Uploaded by

colabpython39
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include<stdlib.h>
int a[10], n, elem, i, pos;
void create() {
printf("Enter the size of the array elements\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
}
void display() {
int i;
if(n) {
printf("Array elements are:\n");
for(i=0; i<n; i++)
printf("%d\t", a[i]);
printf("\n");
}
}
void insert_end() {
printf("\n Enter the element to be inserted:");
scanf("%d", &elem);
a[n]=elem;
n++;
}
void insert_beg_mid() {
int n1;
printf("Enter the position of the new element:");
scanf("%d", &pos);
if(pos<=0)
printf("Position value should be above zero\n");
else {
printf("\n Enter the element to be inserted:");
scanf("%d", &elem);
n1=n;
for(i=n1-1; i>=pos-1; i--) {
a[n1]=a[n1-1];
n1--;
}
a[pos-1]=elem;
n++;
}
}
void del() {
printf("\n Enter the position of the element to be deleted:");
scanf("%d", &pos);
if(pos>n) {
printf("Array have %d elements. Hence enter the position value properly \n", n);
}
else {
elem=a[pos-1];
for(i=pos-1;i<n-1;i++)
a[i]=a[i+1];
n=n-1;
printf("The deleted element is = %d\n", elem);
}
}
int main() {
int ch;
do {
printf("1 - Create\t");
printf("2-Add begining or middle\t");
printf("3-Add at the end\t");
printf("4- Delete an element\t");
printf("5- Display\t");
printf("6- Exit\t");
printf("Enter choice\n");
scanf("%d", &ch);
switch(ch) {
case 1: create();
break;
case 2: insert_beg_mid();
break;
case 3: insert_end();
break;
case 4: del();
break;
case 5: display();
break;
case 6: return 0;
default: printf ("Enter valid choice\n");
}
} while(ch!=6);
return 0;
}

You might also like