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.

Related Links :

3 comments:

  1. nice program ...
    I like it.........

    ReplyDelete
  2. 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
  3. 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


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!