Created
September 15, 2012 00:27
-
-
Save QuanSai/3725776 to your computer and use it in GitHub Desktop.
Helping Loic with his homework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int is_inside(int a[]) | |
{ | |
int to_compare, flag; //the number to compare | |
// followed by the "is inside" flag, yes or no (1, NULL) | |
flag = 0; //first it's assumed that the number to_compare to the array elements is non-existent | |
cout << "Enter a value to check for: " << endl; | |
cin >> to_compare; //set to_compare to user input | |
for(int i = 0; i <= (sizeof a); i++) //start at the beginning of the array; iterate until the maximum sizeof the array is reached | |
if(to_compare == a[i]) //compare the number to_compare to the current array element, and if they're the same | |
flag = 1; // then the flag is up (on; 1; true; yes), which means the number was found | |
else | |
continue; //or else, just continue to loop | |
if(flag == 1) cout << "It's there." << endl; //After the iteration through the array is complete, check the state of the flag | |
else cout << "Non-existent." << endl; | |
return 0; //return successfully | |
} | |
int main() | |
{ | |
int aTest[] = { 1, 2, 3, 4, 5 }; //put some dummy values in a test array | |
is_inside(aTest); //pass the test array of dummy values to your function is_inside() | |
system("pause"); //this is here to pause the console window before exiting. Not needed. | |
return 0; //return success | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment