/********************************************************************************************
JM1206-3.cpp
Jim Millard
for CO211/Fall 1999
Program that matches user input with a pre-existing array of integers
********************************************************************************************/
//included libraries
#include <iostream>
#include <string>
using namespace std;
//function prototypes
int match(const int, const int[], const int count);
//function definitions
int main()
{
const int maxelements = 20;
int list[] = {
53, 98, 0, 64, 23,
1, 21, 56, 39, 94,
61, 57, 72, 89, 93,
2, 54, 69, 90, 6
};
//inform and prompt the user
cout << endl
<< "This program will read an integer as input and" << endl
<< "return the index of a matching element in the list if it exists." << endl;
//prompt and extract the string
cout << "Enter an integer: ";
int i;
cin >> i;
//output the results
int check = match(i, list, maxelements);
if (check > -1)
cout << i << " found at subscript " << check << endl;
else
cout << i << " not found." << endl;
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Parses the array and returns the index of an element in the array that matches Arg1
// Assumes all elements are unique.
// if there is no match, -1 is returned
// Arg3/count is the number of valid elements in the array, and assumes that it is less than
// or equal to the size of the array itself.
int match(const int source, const int A[], const int count)
{
for (int i = 0; i < count; i++)
if (A[i] == source)
return i;
return -1;
}