PROGRAM FOR LINEAR SEARCH IN ARRAY.
#include <iostream>
#include <conio.h>
using namespace std;
class Array
{
private:
int *Arr, Size, Num;
public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I < Siz; I++)
{
Arr[I] = 0;
}
Num = 0;
Size = Siz;
}
void Input()
{
cout << "Enter Size Of Array: ";
cin >> Num;
cout << "\nEnter Elements Of Array: " << endl;
for (int I = 0; I < Num; I++)
{
cin >> Arr[I];
}
}
void L_Search()
{
int Value, Index, Times = 0;
bool Find = false;
cout << "\nEnter Value U want To Search: ";
cin >> Value;
for (int I = 0; I < Num; I++)
{
if (Arr[I] == Value)
{
Index = I;
Find = true;
Times++; ///Times work as counter and will count occurrences;
}
}
if (!Find)
cout << "\nValue Not Found...." << endl;
else
cout << "Value Find " << Times << " times in an Array." << endl;
}
};
int main()
{
Array Obj1(10);
Obj1.Input();
Obj1.L_Search();
getch();
return 0;
}
0 comments:
Post a Comment