Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I can't define a variable inside an if-condition, how can I reduce searching through my array multiple times in this code?

Tags:

arrays

c

I have a function who's body looks a lot like this:

if (contains(array, element1) > -1){
        // do something
    } else if (contains(array, element2) > -1) {
        // do something
    } else if (contains(array, element3) > -1) {
        // do someting
    }...

The function contains will loop through my array and check to see if it contains an element that I pass to it and return either its position if it exists, or -1 if it doesn't.

In my // do something portion, I need the position of this element in the array. There are a couple of different ways I can do this:

  1. I can call my contains() function once more to get the position.
  2. I can define several variables that are defined as the return of the contain function, and then check them in my if-else block. So something like:

    int element1Loc = contains(array, element1);
    int element2Loc = contains(array, element2);
    int element3Loc = contains(array, element3);
    
    if (element1Loc > -1){
        // do something
    } else if (element2Loc > -1) {
        // do something
    } else if (element3Loc > -1) {
        // do someting
    }...
    
  3. I can perhaps modify contain to return an int array[2], with array[0] equal to 0 or 1 whether the element is in it or not, and then array[1] qwould equal the actual location, making the code look like thiss:

    if (contains(array, element1)[0] > -1){
        // do something
    } else if (contains(array, element2)[0] > -1) {
        // do something
    } else if (contains(array, element3)[0] > -1) {
        // do something
    }...
    
  4. I can say screw the if-else block, save the return of contains in a variable and run several if-statements.

Solution one will search through my array at least twice. Solution two will search at least as many times as there are elements I'm looking for. Solution 3 is perhaps the best, but maybe not the most elegant. Solution 4 will run each if statement...

What is the best way to search just once? Should I make a function that takes all the things I am looking for and returns an array with the position of each element? Am I overthinking it?

like image 560
John Lexus Avatar asked Dec 02 '25 05:12

John Lexus


2 Answers

I would modify contains to only use the return value to indicate the error/success of the find, and, if the parameter was found, output the parameter by reference.

int contains(int *data, int value, int *loc)
{
    for(int i = 0; i < SIZE; i++)
    {
         if(data[i]==value)
         {
              *loc = i;
              return 1; // success
         }
    }
    *loc = -1;
    return 0; // failure
}

Now, you can just do:

int elem1loc, elem2loc, elem3loc;
if(contains(data, val1, &elem1loc))
     // use elem1loc...
if(contains(data, val2, &elem2loc))
     // use elem2loc...  
like image 198
Govind Parmar Avatar answered Dec 03 '25 19:12

Govind Parmar


You could pass a pointer to say int which would be populated when the contains function finds an element. Then inside your if block you would be assured that pos is the correct index.

Example:

int pos;

if (contains(array, element1, &pos) > -1) {
    // Here you can check pos for the position
} else if (contains(array, element2, &pos) > -1) {
    // Here you can check pos as well...
}
like image 37
Chris Frank Avatar answered Dec 03 '25 18:12

Chris Frank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!