Created
April 23, 2012 21:43
-
-
Save kdmkdmkdm/2474066 to your computer and use it in GitHub Desktop.
revisedLinearC++
This file contains hidden or 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 "stdafx.h" | |
| #include <iostream> | |
| using namespace std; | |
| int main() | |
| { | |
| int x[17] = {7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1, 5, 42, 39, 8}; | |
| int position = -1; | |
| int value = 0; | |
| // is below the best or most efficient way I could list the values one by one of the array x[17]? | |
| // does a shortcut like: | |
| // cout << "List = " << x[17] << endl; | |
| // exist? | |
| cout << "List = 7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1, 5, 42, 39, 8" << endl; | |
| cout << "Enter an integer in the list to search for: "; | |
| cin >> value; | |
| for(int i = 0; i <= (17 - 1); ++i) | |
| { | |
| if(x[i] == value) | |
| { | |
| // assign i to position. | |
| position = i; | |
| break; | |
| } | |
| else | |
| { | |
| continue; | |
| } | |
| } | |
| cout << "Item found at index [" << position << "]" << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment