Skip to content

Instantly share code, notes, and snippets.

@kdmkdmkdm
Created April 23, 2012 21:43
Show Gist options
  • Select an option

  • Save kdmkdmkdm/2474066 to your computer and use it in GitHub Desktop.

Select an option

Save kdmkdmkdm/2474066 to your computer and use it in GitHub Desktop.
revisedLinearC++
#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