C Language | Arrays 9

Question:19
(Search and Replace)
C program to take input for 10 elements using an array. Further, take input for an element to search and an element to replace with. Replace and print the array?
Sol:

#include<stdio.h>

int main()
{
    int a[30],i,n,n1,n2,z=0;
    /* clrscr(); */
    printf("Enter the total number of elements ");
    scanf("%d",&n);
    printf("enter the array elements \n");
    for(i=0;i<n;i++)
    {
        printf("enter the %d element ",i);
        scanf("%d",&a[i]);
    }
    printf("\nenter the value of the element to search ");
    scanf("%d",&n1);
    printf("Enter new value ");
    scanf("%d",&n2);
    for(i=0;i<n;i++)
    {
        if (a[i]==n1)
        {
            z=1;
            a[i]=n2;
        }
    }
    if (z==0)
        printf("Element is not present");
    else
    {
        printf("Elements found and replaced\n");
        for(i=0;i<n;i++)
        {
            printf("%d\n",a[i]);
        }
    }

    return 0;
}
/* Output */

Enter the total number of elements 10
enter the array elements
enter the 0 element 25
enter the 1 element 10
enter the 2 element 6
enter the 3 element 3
enter the 4 element 10
enter the 5 element 2
enter the 6 element 4
enter the 7 element 10
enter the 8 element 95
enter the 9 element 57

enter the value of the element to search 10
Enter new value 500
Elements found and replaced
25
500
6
3
500
2
4
500
95
57

Question:20
(Bubble Sort)
C program to take input for 10 elements using an array and arrange them in increasing order using bubble sort.
Sol:

#include<stdio.h>

int main()
{
    int a[30],i,j,t,n;
    /* clrscr(); */
    printf("enter the total number of elements ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter the %d element ",i);
        scanf("%d",&a[i]);
    }
    /* to sort */
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if (a[j]>a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
    }
    printf("sorted elements list is \n");
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
    return 0;
}
/* Output */
enter the total number of elements 5
enter the 0 element 25
enter the 1 element 63
enter the 2 element 32
enter the 3 element 4
enter the 4 element 52
sorted elements list is
4
25
32
52
63

Question:21
(Binary Search/Divide and Conquer Search)
C program to search an element in the array using Binary Search?
Sol:

/* binary search: condition: the numbers should be sorted */
#include<stdio.h>

int main()
{
    int a[20],i,n,pos,x,j,t;
    int first,last,middle;
    /* clrscr(); */
    printf("enter the size of the array ");
    scanf("%d",&n);
    printf("enter elements of the array in sorted order\n");
    for(i=0;i<n;i++)
    {
        printf("enter the %d element ",i);
        scanf("%d",&a[i]);
    }
    printf("enter the element to search ");
    scanf("%d",&x);
    /* to search the array */
    first=0;
    pos=-1;
    last=n-1;
    while((first<=last) && (pos==-1))
    {
        middle=(first+last)/2;
        if (a[middle]==x)
            pos=middle+1;
        else
        if (x>a[middle])
            first=middle+1;
        else
            last=middle-1;
    }
    if (pos>-1)
        printf("the element is found at %d position\n",pos);
    else
        printf("the element is not found\n");
    return 0;
}
/* Output */

enter the size of the array 10

enter elements of the array in sorted order
enter the 0 element 2
enter the 1 element 6
enter the 2 element 9
enter the 3 element 14
enter the 4 element 18
enter the 5 element 24
enter the 6 element 32
enter the 7 element 65
enter the 8 element 98
enter the 9 element 145
enter the element to search 14
the element is found at 4 position

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