# Arrays, how to find if some number is the element of an array.

• 03-15-2006
InvariantLoop
Arrays, how to find if some number is the element of an array.
Well im trying to figure out arrays and such and working on a program to find if a number inputed by the user belongs to a particular array. this is what i have so far and im kinda stuck as i got no idea how to make the next step.

So i have initialized 2 arrays with different numbers and im asking the user to find a number. When the user lets say, enters 4. The program should say 4 is in ArrayA, if the user enters 7 then the program should say 7 is in ArrayB. Any suggestions?
Code:

```#include <iostream> using namespace std; int main() {                 int ArrElement;         int *pArrElement =&ArrElement;         int ArrayA[8];         int ArrayB[8];         for (int i =0; i<5; i++)         {                 ArrayA[i] = i+1;                 cout << "ArrayA["<< i << "]: " << ArrayA[i] << "\n";                 cout <<"\n";         }                 for (int j=5; j<10; j++)                 {                         ArrayB[j] = j+1;                         cout << "ArrayB["<< j << "]: " << ArrayB[j] << "\n";                         cout <<"\n";                 }                 cout << "Find a number between 1 and 10 \n";                 cin >> ArrElement;                 cout << *pArrElement << " belongs to ArrayA\n";         return 0; }```
• 03-15-2006
kuphryn
In theory, one solution is binary search. In practice, check out STL.

Kuphryn
• 03-15-2006
Bench82
Is there any reason you're using arrays instead of lists or vectors?

If you're going to use arrays, loop through one element at a time and use an if to compare that element with the value you are matching to.

Code:

```for(int x=0; x!=arraySize; ++x) {   if (array[x] == matchValue)       // Do something }```
• 03-15-2006
brute force

Code:

```#include <iostream> using namespace std; int main() {         int num;         int arrayA[4] = {0,1,2,3};         int arrayB[5] = {4,5,6,7,8};         cout << "Enter a number 0-8";         cin >> num;         for (int i = 0; i < sizeof(arrayA)/sizeof(*arrayA); i++)         {                 if (arrayA[i] == num)                         cout << "The number is in arrayA";         }         for (int i = 0; i < sizeof(arrayB)/sizeof(*arrayB); i++)         {                 if (arrayB[i] == num)                         cout << "The number is in arrayB";         } }```
• 03-15-2006
InvariantLoop
in not very familiar with lists or vectors yet so thats why. I search for Binary search and it looks promising, as it is better than brute force, but now i gotta figure out how it works and not just what it does.

Oh another question. The book im studying from Sams Teach yourself C++, says that you can combine 2 arrays and make a 3rd array. But it doesnt explain how that is done. my other step on this would be to find 1 element from each array, delete those values from the arrays that they belong and save them into the 3rd array. Any suggestion, or if there is any other better way to do this?
• 03-15-2006
ChaosEngine
you do realise your code will crash?
Quote:

Originally Posted by InvariantLoop
Code:

```        int ArrayB[8];                 for (int j=5; j<10; j++)                 {                         ArrayB[j] = j+1; // oh dear, accessing ArrayB[9] on an 8 element array```

besides that you want std::find
• 03-15-2006
InvariantLoop
Quote:

Originally Posted by ChaosEngine
you do realise your code will crash?

besides that you want std::find

it doesnt crash, ive already tested it, i get 0 errors, 0 warnings.
• 03-15-2006
Quote:

it doesnt crash, ive already tested it, i get 0 errors, 0 warnings.
It does crash for me, and I would suspect for most compilers

0 errors or warnings has nothing to do with it
• 03-15-2006
dpro
It will either crash or give you a funky result, either way, its a semantic error not an error a compiler will normally catch.

If you set ArrayB[j+1] = anything, you will find some bad things happening. You are going outside your bounds of array. If you run from 5-10, i.e. 5,6,7,8,9 and you defined ArrayB as ArrayB[10], the elements will go from 0,1,2,3,4,5,6,7,8,9 which is 10 elements. At j == 10, you are going to the 11th element in the array. Sound like the right thing to you?
• 03-15-2006
7stud
Quote:

it doesnt crash, ive already tested it, i get 0 errors, 0 warnings.
The point isn't whether your program crashes or not. When you access an index that is out of bounds, the behavior is undefined and therefore doing so will cause unpredicatable results. An array of size 7 has index values that run from 0-6.

Quote:

my other step on this would be to find 1 element from each array, delete those values from the arrays that they belong and save them into the 3rd array. Any suggestion, or if there is any other better way to do this?
Omit the delete part, and you've got it
• 03-15-2006
InvariantLoop
i understand what you mean.
• 03-16-2006
qqqqxxxx
>it doesnt crash, ive already tested it, i get 0 errors, 0 warnings.
it will crash when u try running it.
• 03-17-2006
homeyg
Quote:

Originally Posted by qqqqxxxx
>it doesnt crash, ive already tested it, i get 0 errors, 0 warnings.
it will crash when u try running it.

Like 20 people already said the same thing before you so shut up!
• 03-17-2006
user_231
>Like 20 people already said the same thing before you so shut up!

please refrain from such nonsense .
• 03-18-2006
jafet
Well, if you use STL iterators, you can stop worrying about overflows.