-1

This function finds the first and last occurrence of an integer in an array. I don't understand what the second if statement does if(!found) is it the same as saying if(found==0)? How does the second statement 'find' the first occurrence? Lets say if there are 3 4's in an array the loop finds the last occurrence and sets it to plast and than it goes into the 2nd if statement how does it know to find the first occurrence and not the 2nd occurrence?

find_occurences(const int a[], size_t n, int x, size_t *pfirst, size_t *plast) {
        size_t i;
int found = 0;
for(i=0; i<; i++) 
    if (a[i] == x) 
        *plast = i;
        if (!found) {
            *pfirst = i;
            found = 1; 
        }
    }
    return found;

}

JohnDough
  • 33
  • 1
  • 8
  • 4
    Either there are missing brackets or there is weird indentation going on at the first `if` statement. Is this on purpose? – Num Lock Jul 02 '15 at 04:30
  • Yeah, also can't see `foundlast` declaration, i assume the brackets should start after the first `if` and ends after the second `if` – Ediac Jul 02 '15 at 04:36
  • Also, `foundlast = 0;` is it a part of first `if()` condition? Right now it is getting executed irrespective of what first `if()` is evaluated as. Depending upon your logic, you have either missed a `{` and `}` with the first `if()` condition, *or* indented the code in a confusing way. – WedaPashi Jul 02 '15 at 04:53
  • 1
    You might try reading a basic C tutorial before attempting to program in it. There you will learn about elementary things like the `!` (not) operator. – Jim Balter Jul 02 '15 at 06:02
  • Sorry guys I dont know how it got messed up like that i just fixed the code – JohnDough Jul 02 '15 at 06:39
  • @JohnDough Still won't compile. The middle part of `for(i=0; i<; i++) ` looks weird and you have three `}` but just two `{` – Spikatrix Jul 02 '15 at 06:44

3 Answers3

4

In C, 0 means boolean false, and any other non zero values are treated as boolean true.

So, if (!found) will go to true path if found = 0.

if(!found) is it the same as saying if(found==0)?

Yes it is!

mazhar islam
  • 5,561
  • 3
  • 20
  • 41
1

From what you describe your source should look like this:

find_occurences(const int a[], size_t n, int x, size_t *pfirst, size_t *plast)
{
    size_t i;
    int found = 0; 

    for (i=0; i<n; i++) // go through all items from first to last
    {
        if (a[i] == x) // if the item is the searched one ...
        {
            *plast = i; // mark as last (every time one is found)
            //foundlast = 0; not needed
            if (!found) // if there hasn't been one found yet ...
            {
                *pfirst = i; // mark as first
                found = 1; // will never enter this if again, thus only on first
            }
        }
    }
}

I assume you had some copy-paste errors.

And to address your main question: All non-zero values in C yield true in an if-statement.

I'd suggest taking a look at this related question for clarification.

Community
  • 1
  • 1
Num Lock
  • 742
  • 1
  • 6
  • 32
0

In C

if(!found){
    //This statements works only when "found" equal to 0
}

In contrary

if(found){
   //This statement works only when "found" not equal to 0
}

In c++ we can use bool as a Boolean variable... Now suppose

bool found
if(!found){
//This statement works when found = false;
}
sr7
  • 39
  • 4