Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphoarty/d13dd1ffed169a4d822acf26d3e8d7cd to your computer and use it in GitHub Desktop.
Save graphoarty/d13dd1ffed169a4d822acf26d3e8d7cd to your computer and use it in GitHub Desktop.
#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