Pages

SEARCHING IN 1-D ARRAY

It is used to find out the location of the data item if it exists in the given collection of data items. 

Example: We have linear array A as below:

1
2
3
4
5
15
50
35
20
25

Suppose item to be searched is 20. We will start from beginning and will compare 20 with each element. This process will continue until element is found or array is finished. Here:

1) Compare 20 with 15
20 # 15, go to next element.

2) Compare 20 with 50
20 # 50, go to next element.

3) Compare 20 with 35
20 #35, go to next element.

4) Compare 20 with 20
20 = 20, so 20 is found and its location is 4.



1A_Pro6: W.A.P. to perform searching operation on Array.


#include<stdio.h>
#include<conio.h>
void main()
{
            int array[10],i,key,n;
            clrscr();
            printf("\tEnter the number of array elements = ",n);
            scanf("%d",&n);

            for(i=0;i<n;i++)
            {
                        printf("\n\tEnter the %d element of array = ",i);
                        scanf("%d",&array[i]);
            }
           
            printf("\n\tEnter the element to be searched:\t");
         scanf("%d",&key);
         for(i=0;i<n;i++)
            {
                        if(array[i]==key)
                        {
                                    printf("\n\tThe element is present at position %d",i+1);
                                    break;
                        }
            }
            if(i==n)
            {
                        printf("\n\tThe search is unsuccessful");
            }
            getch();
}


Output:


Deletion in 1-D Array Sorting in 1-D Array

PPS PREPARATION 3:

Preparation Questions 3: 1) WHILE Loop (With Syntax and Program) 2) DO  WHILE Loop  (With Syntax and Program) 3) FOR loop  (With Syntax and ...