100% program to insert a element from given location in the array


#include <stdio.h>
void main()
{
    int a[20],i,n,location, nayA;
    printf("\n enter no of elements in the array with max limit of 19:"); /*the max limit u enter should be 1 less than array size (of 20) used (here) in the initial declaration becoz after inserting the element overall size should not cross initially declared size, which is 20 here*/
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\n enter the (integer) element a[%d]:",i);
        scanf("%d",&a[i]);
    }
    printf("\n enter the location where new element has to be inserted:");
    scanf("%d",&location);
    printf("\n enter the integer element to be inserted:");
    scanf("%d",&nayA);
    for(i=n;i>=location;i--)
     a[i]=a[i-1]; //shifting the elements
    a[location-1]=nayA;
    printf("\n the elements in the array after deletion of 1 element are:\n");
    for(i=0;i<=n;i++) // now the array size increased by 1
     printf("a[%d]=%-4d    ",i,a[i]);
}    

తెరమీద 
enter no of elements in the array with max limit of 19:4
enter the (integer) element a[0]:2
enter the (integer) element a[1]:4
enter the (integer) element a[2]:7
enter the (integer) element a[3]:-1
enter the location where new element has to be inserted:
3
enter the integer element to be inserted:0
the elements in the array after deletion of 1 element are:
a[0]=2       a[1]=4       a[2]=0       a[3]=7       a[4]=-1    

మరోసారి 
enter no of elements in the array with max limit of 19:4
enter the (integer) element a[0]:2
enter the (integer) element a[1]:5
enter the (integer) element a[2]:1
enter the (integer) element a[3]:-1
enter the location where new element has to be inserted:2
enter the integer element to be inserted:4
the elements in the array after deletion of 1 element are:
a[0]=2       a[1]=4       a[2]=5       a[3]=1       a[4]=-1     

కామెంట్‌లు

ఈ బ్లాగ్ నుండి ప్రసిద్ధ పోస్ట్‌లు

100% C prog to write content into a file and then read it back onto the console

Spic macay

శ్రీరస్తు.. రామలింగ 18-1-24 (సూరసాని వారి ఆహ్వానము)