My C++ Notes

From Got Opinion Wiki
Revision as of 18:51, 17 November 2011 by Paul (talk | contribs) (Created page with "#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,8...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
  1. 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; }