My C++ Notes
Jump to navigation
Jump to search
- include <iostream>
using namespace std;
bool search(int[], int, int, int&);
void main() { int key, index; int vals[20] = {1,2,3,4,5,6,7,8,9,10,101,202,303,404,505,606,707,808,909,111};
cout << "Enter a number to search for: "; cin >> key;
if (search(vals, 20, key, index)) { cout << "Found at: " << index << endl; } else { cout << "Not found.\n"; }
bool search(int vals[], int num, int key, int& index) { bool found = false; index = -1; for (int i=0; i < num; i++) { if (vals[i]==key) { index = i; found = true; break; } } return found; }