Skip to content

Instantly share code, notes, and snippets.

@kdmkdmkdm
Created April 30, 2012 02:36
Show Gist options
  • Select an option

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

Select an option

Save kdmkdmkdm/2555025 to your computer and use it in GitHub Desktop.
linearsearchworking
// linearSearch1.cpp : Defines the entry point for the console application.
//
#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;
int arraySpot = 0;
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