Program to Search An Element in 2-Dimensional Array Using Linear Search



void main()
{
int i,j,item,loc=0,loc1=0;
int a[2][2];
clrscr();
printf("\n\tThis Program is Used To seaech an element in 2Dimensional Array using Linear Search\n");
printf("\n\tEneter The Value Of Array:");
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n\tEneter The Value To Be Serched:");
scanf("%d",&item);
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(item==a[i][j])
{
loc=i;
loc1=j;
break;
}
}
}
printf("\n\tThe Item is at %d Row And %d Coloumn.",loc,loc1);
printf("\n\n\t\tSearch Completed.");
getch();
}





OUTPUT

This Program is Used To search an element in 2Dimensional Array using Linear Search
Enter the value of array: 6 4
3 9

Enter the value to be searched ;3

the item is at 2 Row and 1 coloum.


Search completed.

4 comments:

  1. how do u search for an element in a 2D array using functions?
    the following program gave 11 errors:
    #include
    #include
    void abc(int a[][])
    {
    cout<<"elements of the array are";
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    cout<<"a[i][j]";
    }
    cout<<"\n";
    }
    getch();
    }
    void main()
    {
    puts x [3] [3];
    clrscr();
    cout<<"enter elements";
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    cin<.b;
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    if(b==a[i][j])
    {
    cout<<"found";
    }
    else
    cout<<"not found";
    }
    }

    ReplyDelete
  2. nice tutorial sir..
    now i want to know how to find a count of duplicate value on 2 dimentional array.
    how to find it?
    the result are same with your program..the difference is in count of duplicate and here's located.

    example :
    int data[5][5]={{2,4,10,29,7},{20,5,7,9,15}};

    we try to search "7" element.
    so the result are :
    value "7" was found 2 times
    on row "x" column "y" and row "x" column "y"

    ReplyDelete
  3. its a wrong programme. you did not write the spelling of 'enter' also.


    ReplyDelete