Created
March 3, 2019 03:21
-
-
Save graphoarty/d13dd1ffed169a4d822acf26d3e8d7cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment