CodeClerks

Linear Search at C++



Write the reason you're deleting this FAQ

Linear Search at C++

I have just wrote the linear search program today. here is the code . . .

#include <iostream>
using namespace std;

int main ()

{

int data[] = {10,20,36,98,55,200,258,986,458,88};

int i,key,loc,check=0;

cout << "Please enter search term";

cin >> key;

for (i = 0; i < 10; i++)

{

  if (data[i] == key)

  {

check = 1;

loc = i;

break;

  }

}

if (check == 1)

{

  cout << "Search Result :" <<endl;

  cout << key << " is in our database" << endl;

  cout << "Location of " << key << " is " << loc+1 << endl;

}

else

{

  cout << "Sorry !" << key << " Doesn't exist in our database" <<endl;

}

return 0;

}
 
Its working fine. Its really easy to understand the linear search algorithm. I don't know if we can do the same for 2D array !! will this algorithm work for 2D array also ? or will we need any other algorithm ? If you know then please share with me. I am trying myself to figure this but its not working yet. If I will success I will share the code here.

Thanks

Comments

Please login or sign up to leave a comment

Join