Linear Search | A Helpful Line-by-Line Code Tutorial

#include <iostream>
using namespace std;

int getPosition(int key, int searchSpace[], int length){

    for(int i = 0; i < length; i++){

        if(searchSpace[i] == key){

            return i;
        }

    }

    return -1;

}

int main(){

    int searchSpace[] = {20, 1, 3, 5, 2 , 77, 32, 67, 23, 21};
    int key = 3;
    int length = sizeof(searchSpace) / sizeof(searchSpace[0]);

    int position = getPosition(key, searchSpace, length);

    if(position != -1){

        cout << "The element is at position " << position + 1 << "." << endl;

    } else {

        cout << "The key is not present in the program." << endl;

    }

return 0; }