#include<stdio.h
>
#include<stdlib.h
>
#include<conio.h
>
int arr[100], size=0;
void insert_beg();
void insert_pos();
void insert_last();
void display();
int main(){
int choice;
printf("Enter size of the array : ");
scanf("%d",&size);
printf("Enter the array elements one by one.....\n");
for(int i=0;i<size
;i++){
scanf("%d",&arr[i]);
}
while(1){
printf("\n\t\t1. Insert at beginning\n\t\t2. Insert at given possition\n\t\t3. Insert at last\n\t\t4. Display array elements\n\t\t5. Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:
insert_beg();
break;
case 2:
insert_pos();
break;
case 3:
insert_last();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid Choice.....Please try again.....\n");
}
}
getch();
}
void insert_beg(){
int item;
printf("Enter element to insert : ");
scanf("%d",&item);
for(int i=size;i>0;i--){
arr[i]=arr[i-1];
}
arr[0]=item;
size++;
printf("The entered item %d is inserted at beginnng.\n",item);
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
}
void insert_pos(){
int item, pos;
printf("Enter item to insert : ");
scanf("%d",&item);
printf("Enter possition at which you want to insert : ");
scanf("%d",&pos);
for(int i=size;i>=pos;i--){
arr[i]=arr[i-1];
}
arr[pos-1]=item;
size++;
printf("The entered element %d is inserted at %d possition.\n",item,pos);
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
}
void insert_last(){
int item;
printf("Enter element to insert at last : ");
scanf("%d",&item);
for(int i=size;i>=size+1;i--){
arr[i]=arr[i-1];
}
arr[size]=item;
size++;
printf("The entered item %d is inserted at last.\n",item);
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
printf("\n");
}
void display(){
printf("The array elements.....\n");
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
printf("\n");
}
Wednesday, 22 November 2017
C Program to Insert Element into Array
Subscribe to:
Post Comments (Atom)
Python Program to find Fabonacci
fabtab={} def fabonacci(n): fabtab[0]=0 fabtab[1]=1 for i in range(2,n+1): fabtab[i]=fabtab[i-1]+fabtab[i-2] ...
-
#include stdio.h > #include stdlib.h > #include conio.h > int arr[100], size=0; void insert_beg(); void insert_pos(); void inser...
-
#include iostream> #include conio.h > using namespace std; int main() { int n,num,first=0,middle,last; ...
-
#include iostream > #include conio.h > using namespace std; int main() { float radius; double area; ...
No comments:
Post a Comment