Implement operations on Arrays.
Source Code
#include<stdio.h>
void Insert(int,int[],int,int);
void Delete(int,int[],int);
int Search(int,int[],int);
void Traverse(int,int[]);
int main(){
int ary[20],n,i,pos,ele,choice,index;
printf("Enter array size:\n");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
printf("::ARRAY OPERATIONS::\n1. Insert\n2. Delete\n3. Search\n4. Traverse\n");
printf("Select array operation:\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter new element:\n");
scanf("%d",&ele);
printf("Enter position:\n");
scanf("%d",&pos);
if(pos<=n){
Insert(n,ary,ele,pos);
n++;
Traverse(n,ary);
}else{
printf("Insertion is not possible.\n");
}
break;
case 2:
printf("Enter position:\n");
scanf("%d",&pos);
if(pos<n){
Delete(n,ary,pos);
n--;
Traverse(n,ary);
}else{
printf("Deletion is not possible.\n");
}
break;
case 3:
printf("Enter element:\n");
scanf("%d",&ele);
index = Search(n,ary,ele);
if(index==-1)
printf("%d is not found in the array.\n",ele);
else
printf("%d is found in the array at %d position.\n",ele,index);
break;
case 4:
Traverse(n,ary);
break;
default:
printf("Invalid operation.\n");
}
return 0;
}
void Insert(int n,int ary[],int ele,int pos){
int i;
for(i=n;i>pos;i--)
ary[i] = ary[i-1];
ary[pos] = ele;
}
void Delete(int n, int ary[],int pos){
int i;
for(i=pos;i<n;i++)
ary[i] = ary[i+1];
}
int Search(int n,int ary[],int ele){
int i;
for(i=0;i<n;i++){
if(ary[i]==ele){
return i;
}
}
return -1;
}
void Traverse(int n,int ary[]){
int i;
printf("Array elements are:\n");
for(i=0;i<n;i++){
printf("%d ",ary[i]);
}
}
void Insert(int,int[],int,int);
void Delete(int,int[],int);
int Search(int,int[],int);
void Traverse(int,int[]);
int main(){
int ary[20],n,i,pos,ele,choice,index;
printf("Enter array size:\n");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
printf("::ARRAY OPERATIONS::\n1. Insert\n2. Delete\n3. Search\n4. Traverse\n");
printf("Select array operation:\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter new element:\n");
scanf("%d",&ele);
printf("Enter position:\n");
scanf("%d",&pos);
if(pos<=n){
Insert(n,ary,ele,pos);
n++;
Traverse(n,ary);
}else{
printf("Insertion is not possible.\n");
}
break;
case 2:
printf("Enter position:\n");
scanf("%d",&pos);
if(pos<n){
Delete(n,ary,pos);
n--;
Traverse(n,ary);
}else{
printf("Deletion is not possible.\n");
}
break;
case 3:
printf("Enter element:\n");
scanf("%d",&ele);
index = Search(n,ary,ele);
if(index==-1)
printf("%d is not found in the array.\n",ele);
else
printf("%d is found in the array at %d position.\n",ele,index);
break;
case 4:
Traverse(n,ary);
break;
default:
printf("Invalid operation.\n");
}
return 0;
}
void Insert(int n,int ary[],int ele,int pos){
int i;
for(i=n;i>pos;i--)
ary[i] = ary[i-1];
ary[pos] = ele;
}
void Delete(int n, int ary[],int pos){
int i;
for(i=pos;i<n;i++)
ary[i] = ary[i+1];
}
int Search(int n,int ary[],int ele){
int i;
for(i=0;i<n;i++){
if(ary[i]==ele){
return i;
}
}
return -1;
}
void Traverse(int n,int ary[]){
int i;
printf("Array elements are:\n");
for(i=0;i<n;i++){
printf("%d ",ary[i]);
}
}
Enter array size:
4
Enter 4 elements:
5 10 20 25
::ARRAY OPERATIONS::
1. Insert
2. Delete
3. Search
4. Traverse
Select array operation:
1
Enter new element:
15
Enter position:
2
Array elements are:
5 10 15 20 25
4
Enter 4 elements:
5 10 20 25
::ARRAY OPERATIONS::
1. Insert
2. Delete
3. Search
4. Traverse
Select array operation:
1
Enter new element:
15
Enter position:
2
Array elements are:
5 10 15 20 25