Thread: difference between two control statements for an if

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    difference between two control statements for an if

    i have this code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_NUMBER 4000000
    
    void fill_fib_nums(int fib_nums[], int num_elements);
    int even_sum(int fib_nums[], int num_elements);
    
    int main()
    {
        int answer, num_elements, fib_nums[33];
    
        fib_nums[0] = 1;
        fib_nums[1] = 1;
        num_elements = sizeof(fib_nums) / sizeof(fib_nums[0]);
        fill_fib_nums(fib_nums, num_elements);
        answer = even_sum(fib_nums, num_elements);
        printf("the some of the even numbers in the fibonachi sequence that are less than 4,000,000 is %d\n", answer);
    
        return 0;
    }
    
    void fill_fib_nums(int fib_nums[], int num_elements)
    {
        int i, j, next_num = 2;
    
        for (i = 0, j = 1; next_num < num_elements; i++, j++, next_num++)
        {
            fib_nums[next_num] = fib_nums[i] + fib_nums[j];//add the previous two numbers in the array together and store the result in fib_nums[next_num]
        }
    
    }
    
    int even_sum(int fib_nums[], int num_elements)
    {
        int i, answer;
    
        for (i = 0; i < num_elements; i++)
        {
            if (fib_nums[i] % 2 == 0) // run if fib_nums[i] is even
            {
                answer += fib_nums[i];
            }
        }
    
        return answer;
    }
    the above doesn't work. however if i change the if statement on line 40 to
    Code:
     if (fib_nums[i] % 2 == 0)
    it does.

    i thought that
    Code:
    if(!some statement that equates to 0)
    is the same as
    Code:
    if (some statement that equates to 0 == 0)
    coop

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Uh. Line 40 and what you wrote as its replacement look the same to me.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    oh bother thats what happends when you cut and paste code after finding the soloution. line 40 was
    Code:
     if (!fib_nums[i] % 2)
    which didn't work

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, I see. This:
    Code:
    if (!fib_nums[i] % 2)
    Is equivalent to:
    Code:
    if ((fib_nums[i] == 0) % 2)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    huh surely
    Code:
     if ((fib_nums[i] == 0) % 2)
    is an error as you can't divide 0

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i see what your saying i should of written
    Code:
     if (!(fib_nums[i] % 2))

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by cooper1200 View Post
    huh surely
    Code:
     if ((fib_nums[i] == 0) % 2)
    is an error as you can't divide 0
    You can't divide by zero. Dividing zero by anything (other than zero) is certainly not an error.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    As laserlight told you, this expression: !fib_nums[i] % 2 is evaluated as (!fib_nums[i]) % 2. It is important to know the operators precedence and associativity when building an expression: C Operator Precedence.

    Precedence is what operator is evaluated first... Associativity is when you have two similar operators, which of them is evaluated first. For exemple:
    Code:
    x = a * b + c * d;
    In this expression the operator * has precedence over the operator +, and the later has precedence over the operador =. So this expression can be undestood as:
    Code:
     x = ( ( a * b ) + ( c * d ) );
    Where the multiplications are done before the add and then the assignment is done. Notice you have two multiplications, which one is done first? * operator has associativity from left to right, so (a * b) is done first, then (c * d)...

    In the expression !fib_nums[i] % 2 we got 3 operators: !, [] and %. [] has precedence over !, and % has the lesser precedence (it will be evaluated last). So the expression is ( ! ( fib_numbs[i] ) ) % 2.

    The expression fib_nums[i] % 2 == 0, == has the lesser precedence, so the expression is evaluated as ( ( fib_nums[i] ) % 2 ) == 0.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by flp1969
    Precedence is what operator is evaluated first...
    That's a common misconception. Precedence determines grouping, i.e., how the syntax is to be interpreted. It doesn't determine order of evaluation, except where the syntax implies a particular order of evaluation.

    Quote Originally Posted by flp1969
    Notice you have two multiplications, which one is done first? * operator has associativity from left to right, so (a * b) is done first, then (c * d)...
    That isn't true though. Associativity can determine order of evaluation when it is implied by the grouping imposed by association, but in this case it doesn't: the two subexpressions are separate, unlike say, a * b * c. Therefore, the order of evaluation is not specified, so either multiplication could occur first.
    Last edited by laserlight; 06-06-2019 at 04:10 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    so the ! operator has precedence over the % operator?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A difference between 2 statements related to pointers
    By Muhammad Yahya in forum C Programming
    Replies: 2
    Last Post: 04-05-2016, 10:38 AM
  2. Replies: 0
    Last Post: 07-03-2015, 12:28 PM
  3. Hosting a C# user control Hwnd within an ATL control
    By subdene in forum C++ Programming
    Replies: 0
    Last Post: 11-02-2013, 06:09 PM
  4. help with if and else statements
    By kburyanek in forum C Programming
    Replies: 7
    Last Post: 09-25-2009, 01:28 AM
  5. how to 'and' statements
    By chris285 in forum C++ Programming
    Replies: 11
    Last Post: 04-29-2003, 04:18 PM

Tags for this Thread