ccplusplus.com
Learn C, C++ Concepts
Wednesday, September 14, 2011
Liner Search in C++
/****************************************************** * File : linear-search-in-array.cpp * Author : Saurabh Gupta * Desc : liner search example * * Source : http://saurabhgupta0527.blogspot.com/ * Created : AM 09:14 14 September 2011 *****************************************************/ #include
using namespace std; class CLinarSearch { private: int m_nElementsToSearch[25], m_nLocation; public: CLinarSearch (); ~CLinarSearch (); public: int m_nTotalElements, m_nSearchNumber; void m_getdata(); void m_search(); }; ///////////////////////////////// // construction / destruction // ///////////////////////////////// CLinarSearch :: CLinarSearch () : m_nTotalElements (5), m_nSearchNumber (0), m_nLocation (0) { } CLinarSearch :: ~CLinarSearch () { } void CLinarSearch :: m_getdata() { cout << "Enter the total number of the elements \nyou want to enter to search for !" << endl; cin >> m_nTotalElements; cout << "Enter the numbers from which you want to search" <
> m_nElementsToSearch[i]; } } void CLinarSearch :: m_search() { cout << "Enter the number you want to search" << endl; cin >> m_nSearchNumber; cout << "\nSearching - " << m_nSearchNumber << endl; int k = 1; while(m_nLocation == 0 && k <= m_nTotalElements) { if (m_nSearchNumber == m_nElementsToSearch[k]) { m_nLocation = k; } else { k = k + 1; } } if (m_nLocation == 0) { cout << m_nSearchNumber <<" not found in the numbers you entered" << endl; } else { cout << m_nSearchNumber << " found in the numbers you entered" << endl; } } int main() { cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"; cout << "XXXXX Liner Search XXXXX\n"; cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"; CLinarSearch l; l.m_getdata(); l.m_search(); cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"; return 0; } /* * * [sgupta@rhel54x64 c]$ c++ linear-search-in-array.cpp -o linear-search-in-array [sgupta@rhel54x64 c]$ ./linear-search-in-array XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX Liner Search XXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Enter the total number of the elements you want to enter to search for ! 5 Enter the numbers from which you want to search 2 5 9 33 1 Enter the number you want to search 9 Searching - 9 9 found in the numbers you entered XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel54x64 c]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment