9 мая 2015 г.

Линейный поиск

int ls (int array[], int size, int key){
for(int i=0;i<size;i++)
if(array[i] == key)
return i;
return -1;
}

void main()
{
setlocale(LC_ALL, "Russian");
const int arraySize=100;
int a[arraySize], searchKey, element;
for(int x=0;x<arraySize;x++)
a[x]=2*x;
cout<<"Введите число: ";
cin>>searchKey;
element=ls(a, arraySize, searchKey);
if(element!=-1)
cout<<"\nВведённое число находится в ячейке"<<element<<'\n';
else
cout<<"\nНе найдено";
}

Комментариев нет:

Отправить комментарий