C: Search an element from array.

Question:

 Write a program to find the element of array.

 Answer:

 Here  we have mentioned three ways by which we can find the element of array.

1- By using General method.

2- By using function

3- By using recursion

 

1- By using General method:

Code: -------------------------------------------

#include <stdio.h>
int main()
{
    int a[10000],i,n,key;     //Defines variables and given the size of array

    printf("Enter size of the  array : ");
    scanf("%d", &n);
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
     printf("Enter the key : ");
    scanf("%d", &key);

    for(i=0; i<n; i++)
    {
        if(a[i]==key)
        {
printf("Element %d found at %d index of array \n",key,i);
            return 0;
        }

    }

printf("Element  is not  found in array");
}

------------------------------------------------------
 Output is:

Desktop/C_Pro$ vi searchelement.c
Desktop/C_Pro$ gcc searchelement.c -o searchelement
Desktop/C_Pro$ ./searchelement
Enter size of the  array : 5
Enter elements in array : 12
13
14
16
17
Enter the key : 16
Element 16 found at 3 index of array
 
 
2- By using function:


Code:---------------------------------------------

#include <stdio.h>
int search(int *a,int n,int key)  //Function definition.
{
int i;
    for(i=0; i<n; i++)                //match key will each element of array.
    {
        if(a[i]==key)
        {
             return 1;
        }
 
    }
    
return 0;          
      
}
 
  
int main()
{
    int a[10000],i,n,key;   //Defines variables and given the size of array
  
    printf("Enter size of the  array : ");
    scanf("%d", &n);
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
     printf("Enter the key : ");
    scanf("%d", &key);
    
     if(search(a,n,key))      //Function call
     printf("Element found ");
     else
     printf("Element is not found ");
    
    
}
------------------------------------------------------
 
 

3- By using recursion:
 
Code:----------------------------------------------

#include <stdio.h>
int search(int *a,int i,int n,int key)
{
    if(i<n)
    {
     if(a[i]==key)
     return 1;
    
     return search(a,i+1,n,key);
    
}
    
return 0;          
      
}
 
  
int main()
{
    int a[10000],i,n,key;      //Defines variables and given the size of array
  
    printf("Enter size of the  array : ");
    scanf("%d", &n);
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
     printf("Enter the key : ");
    scanf("%d", &key);
    
     if(search(a,0,n,key))            //recursion, call itself again again till certain con.. 
     printf("Element found ");
     else
     printf("Element is not found ");
    
    
}
--------------------------------------------- 





Note:
Execute above code online. below link will  take you on online compiler.
 

No comments:

Post a Comment