Difference between revisions of "My C++ Notes"

From Got Opinion Wiki
Jump to navigation Jump to search
m (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...")
 
m (Blanked the page)
Line 1: Line 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;
}

Revision as of 18:51, 17 November 2011