#include<stdio.h>
#include<conio.h>
int create();
int display(int [],int);
int insert(int [],int);
int delete_fromlist(int [],int);
int update(int [],int);
int a[100],n,i;
void main(){
create();
display(a,n);
insert(a,n);
delete_fromlist(a,n);
update(a,n);
getch();
}
int create(){
printf("Enter n: ");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
int display(int a[],int n){
printf("Current array elements: ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
int insert(int a[],int n){
int pos, nel;
printf("\nEnter position where you want to insert: ");
scanf("%d",&pos);
printf("Enter new element: ");
scanf("%d",&nel);
if(pos>n)
printf("Invalid Position");
else{
for(i=n-1;i>=pos;i--)
a[i+1]=a[i];
a[pos]=nel;
n++;
display(a,n);
}
}
int delete_fromlist(int a[],int n){
int pos;
printf("\nEnter position where you want to delete: ");
scanf("%d",&pos);
if(pos>n)
printf("Invalid Position");
else{
for(i=pos;i<n;i++)
a[i]=a[i+1];
//n--;
display(a,n);
}
}
int update(int a[],int n){
int pos, nel;
printf("\nEnter position where you want to update: ");
scanf("%d",&pos);
printf("Enter new element: ");
scanf("%d",&nel);
if(pos>n)
printf("Invalid Position");
else{
a[pos]=nel;
display(a,n);
}
}
No comments:
Post a Comment