Скачиваний:
34
Добавлен:
02.05.2014
Размер:
804 б
Скачать
#include "iostream.h"
#include "string.h"
#include "stdio.h"

int linearSearch(int array[], int element, int size){
	bool found = false;
	int i = 0;
	while ((i < size) && (! found)){ 
		if (array[i] == element)
			found = true;
		else
			i++;
	}

	if (i < size) {
		return i;
	}
	else {
		return -1;
	}
}

void main(){
	int i, size, element;
	int *array;
    cout << "Linear Search.\nEnter array dimension: ";
    cin >> size;
	array = new int[size];
    cout << "Enter " << size << " elements: ";
	for ( i = 0; i < size; i ++ ){
		cin >> array[i];
	}
    cout << "Enter element: "; 
	cin >> element;
    cout << "Position of element " << element << " in array is " << linearSearch( array, element, size ) << "\nPress any key to continue..." << endl; 
    getchar();
}