tags:

views:

227

answers:

6

Hi, I would like to see if a value equals any of the values in an array. like this

Beginning of function(here I give Val differnt values with for loops...)

       for (i=0;i<array_size;i++)
            if (Val==array[i])
            do something

    else 
    do something else if non of the above values matched val...

If none of the Arrayvalues matches my val I would like to do something else, but only once... And if it matches then I would like to go back to the beginning of the function that will give me a different value to val... Where should I put in this code

Thx, for a great forum /Buxley

+3  A: 

Use a flag to indicate whether the condition was satisfied at least once:

bool hasMatch = false;
for (i=0;i< array_size;i++) {
        if (Val==array[i]) {
            //       do something
            hasMatch = true;
        }
}
if( !hasMatch ) {
  // do whatever else
}

This will invoke the "do something" for every matching element. If you want to invoke it for the first matching element only use break; after "do something".

sharptooth
thank you very muchthis is basically what I was looking for...I just thought I could do it without using an extra var...
mrbuxley
If you need to invoke the action for every matching element you'll need the extra var to persist the fact that a match has occured while you continue the loop.
sharptooth
Thanks I used this snippet exept that I used a normal variable instead of the bool... and just gave it a value if there was a match... I got it to work now and trying to solve the rest of my 100 billion problems with c++ (=
mrbuxley
What is a "normal variable" better than a bool in this case?
sharptooth
Nope, probably not but I just did something wrong when I tried with Bool and since I'm not 100% sure how I will use this function in the very end I chose to try it out as fast as possible the way i got it to work...
mrbuxley
+1  A: 

Not entirely sure what you're after. You can always do your stuff if you find a match and then just return from the function.

for (int i=0; i < array_size; i++) {
    if (Val==array[i]) {
        // do something
        return;
    }
}

// if we end up here, there was no match
// do something else ..

Or, you could set a boolean value and break the loop instead, there are many ways. Pick one :)

Magnus Skog
+3  A: 

you can use a find function

int find(int value, int* array, int size) {
    int i;
    for (i=0; i<size; i++) {
        if (array[i]==value) {
           return i;
        }
    }
    return -1;
}

Now you can do

if (find(value, array, array_size)>=0)) {
   do_something_when_found();
} else {
   do_something_when_not_found();
}
Otto Allmendinger
or, as it's a C++ question, use +the+ find function
Pete Kirkham
+2  A: 

Sorry to intervene. I have some feeling, that you were going to ask smth. else. As I understand you, you have an ordered set of values and would like to find the first possible value from that set in an array.

If so use the find_first_of algorithm from the C++ standard library. Algorithms in STL might be better optimized for your compiler (e.g. they might support parallel search).

Here is an adapted code taken from CPPReference.com. You are not limited to int-values.

int nums[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int* nend = &nums[10];

int targets[] = { 9, 4, 7 };
int* tend=&targets[3];

using namespace std;
int* result = find_first_of( &nums[0], nend, &targets[0], tend );

if( result == nend )
  cout << "Did not find any of { 9, 4, 7 }" << endl;
else
  cout << "Found a matching target: " << *result << endl;

After the value found result points to the element in the nums array which matches first possible element of the targets array elements. If none matched result equals nend.

Best Regards,
Ovanes

ovanes
This looks pretty interesting. And in my code I actually have multiple targets so I will try it out tomorrow... Right now it's time to go home and relax...Cheers
mrbuxley
+3  A: 

For most lookup actions, the STL has an algorithm. Finding a value in an array can be done using ... std::find.

const char values[] = "abcdefg";
const char* values_end = values + _countof( values );

const bool dPresent = 
 std::find_if( values, values_end , 'd' ) != values_end ;

if( dPresent ) {
    ...
}
xtofl
+1  A: 

Please look up what the STL can do for you.
There is a whole section on algorithms:

if (std::find(array,array+array_size,Val) != array+array_size)
{
    // Found it.
}
else
{
    // Did not find it.
}
Martin York