C Language | Arrays 10

Question:22
C program to insert an element in the array?
Sol:

#include<stdio.h>

int main()
{
   int a[50],n,i,n1,pos,b;
//clrscr();
printf("Enter total elements ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element ");
scanf("%d",&a[i]);
}

printf("Enter the position ");
scanf("%d",&pos);
printf("Enter the value to insert ");
scanf("%d",&n1);

pos=pos-1;
b=n;
while(b>=pos)
{
a[b]=a[b-1];
b--;
}
a[pos]=n1;
n++; //
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
    return 0;
}
/* Output */
Enter total elements 5
Enter the element 10
Enter the element 20
Enter the element 30
Enter the element 40
Enter the element 50
Enter the position 3
Enter the value to insert 500
10
20
500
30
40
50

Question:23
C program to delete an element from the array?
Sol:

#include<stdio.h>

int main()
{
    int a[50],n,i,n1,pos,b;
    /* clrscr(); */

    printf("Enter total elements ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter the element ");
        scanf("%d",&a[i]);
    }
    printf("Enter the position ");
    scanf("%d",&pos);
    pos=pos-1;
    b=pos;
    while(b<=n)
    {
        a[b]=a[b+1];
        b++;
    }
    n--;//
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
    return 0;
}
/* Output */
Enter total elements 5
Enter the element 10
Enter the element 20
Enter the element 30
Enter the element 40
Enter the element 50
Enter the position 3
10
20
40
50

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects