Yep. Newby.
I am asked to find the closest value to user defined value if not right on the mark. I can get it to determin it I have selected one of the generated numbers, but am at a loss how to have it find the closes. here is my code.
Thanks if anyone could assist.
-
#include "stdafx.h"
-
#include <iostream>
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
-
int linearSearch( const int[],int,int);
-
-
int _tmain(int argc, _TCHAR* argv[])
-
{
-
const int max = 100;
-
int a[max];
-
int searchKey;
-
-
for (int i = 0; i < max; i++)
-
a[i] = 2*i;
-
-
-
cout << "Enter a guess for one of the randomly generated numbers: ";
-
cin >> searchKey;
-
-
int element = linearSearch ( a, searchKey, max );
-
-
if ( element != -1)
-
cout << "You found a match in element " << element << endl;
-
else
-
cout << "Value not found" << endl;
-
-
return 0;
-
}
-
int linearSearch(const int array[], int key, int sizeOfArray)
-
{
-
for ( int j = 0; j < sizeOfArray;j++)
-
if ( array[j] == key )
-
return j;
-
-
return -1;
-
}