problem with logical operators

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cube
    New Member
    • Feb 2008
    • 18

    problem with logical operators

    Hallo everyone,
    I have written the below code but it doesn't work right.
    The problem is that when I run it, and

    the score_main array has a value of -3 or 3 and at the same time

    the move_main is 9, I get as a "result" the number 3, which is wrong

    according to this:

    [CODE=c] for (i=0; i<8; i++)
    {
    if ((move_main==9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
    {
    result=3;
    }
    }[/code]
    where is my mistake???@#%#@ !!! :(

    Here is the whole code:


    [code=c]#include <stdio.h>
    #include <stdlib.h>


    int main()
    {
    int score_main[8]={0,1,-2,-1,-3,1,0,1};
    int move_main=9;

    int i,result;

    for (i=0; i<8; i++)
    {
    if (score_main[i]==3)
    {
    result=1;
    }
    }

    for (i=0; i<8; i++)
    {
    if (score_main[i]==-3)
    {
    result=2;
    }

    }

    for (i=0; i<8; i++)
    {
    if ((move_main<9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
    {
    result=0;
    }
    }
    for (i=0; i<8; i++)
    {
    if ((move_main==9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
    {
    result=3;
    }
    }


    printf("%d\n", result);



    system("PAUSE") ;

    }[/CODE]
    Last edited by Ganon11; Feb 25 '08, 07:09 PM. Reason: Please use the [CODE] tags provided.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by cube
    if ((move_main==9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
    If score_main ==3,
    score_main[i]!=-3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

    If score_main == -3
    score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
    If score_main ==2
    score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

    If score_main ==20
    score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
    If score_main ==-60
    score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

    That is, the expression (x != 3) || (x!= -3) is always TRUE.

    If you are intending "score_main[i] not equal 3 or not equal -3", then you code:

    (score_main[i]!=3) && (score_main[i]!=-3)

    This expression will be TRUE only when score_main[i] is not 3 AND not -3.

    Comment

    • cube
      New Member
      • Feb 2008
      • 18

      #3
      Originally posted by weaknessforcats
      If score_main ==3,
      score_main[i]!=-3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

      If score_main == -3
      score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
      If score_main ==2
      score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

      If score_main ==20
      score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
      If score_main ==-60
      score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

      That is, the expression (x != 3) || (x!= -3) is always TRUE.

      If you are intending "score_main[i] not equal 3 or not equal -3", then you code:

      (score_main[i]!=3) && (score_main[i]!=-3)

      This expression will be TRUE only when score_main[i] is not 3 AND not -3.
      You are right about your thoughts!!!
      Actually, this is the way I did it for the first time.
      But, even if I tried it, your way (which is the right one) I still get wrong results.
      What it happens, is that the result should be result=2
      And that is what I get as I run the program, but unfortunatelly, it doesn't stop there.
      It goes on to:

      for (i=0; i<8; i++)
      {
      if ((move_main==9) && (score_main[i]!=3) && (score_main[i]!=-3))
      {
      result=3;
      }
      }

      and returns result=3


      WHY?????????

      Comment

      • looker
        New Member
        • Dec 2007
        • 18

        #4
        You all right!... The last condition must be if ((move_main==9) && (score_main[i]!=3) && (score_main[i]!=-3)).
        In your code above, you have 4 loops. In the two above, you check for score_main[i] value, the last 2 more, you check for score_main[i] and move_main.
        All the four loops are used to assigned the value to variable result. This is the point.
        Check yoru design again, If the condition has already been true in the first loop ( example ), it means the variable result is already set value. Is it necessary to go through the rest of code, i see that is not useful and your program can be more slower.
        - In fact, you loops from 0 to 8 incremented by 1. You repeated this loop four times witch different condition stay inside. What if you just have only one loop and 4 condition ?
        - If there is not codes below your code posted here, why you not return immediately after variable result is set. because your current design lead your program to go through the rest of code even though that is not necessary.

        Comment

        • cube
          New Member
          • Feb 2008
          • 18

          #5
          Hi again and thank you for your time!

          First of all I would like to mention that I am the newbiest newbie in C programming, (I start reading about C, just 2 weeks ago) though I'm trying hard to figure everything out. Now, about my problem...

          Originally posted by looker
          - In fact, you loops from 0 to 8 incremented by 1. You repeated this loop four times witch different condition stay inside. What if you just have only one loop and 4 condition ?
          I did try to do that, but it didn't work out (due to my mistaken code...of course)...there fore I wrote the above code.
          Could I have a hint on how to do that? Pleaseeeeeeee.. ..

          Originally posted by looker
          - If there is not codes below your code posted here, why you not return immediately after variable result is set. because your current design lead your program to go through the rest of code even though that is not necessary.
          Also I would like to know how to do that too!

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            cube-

            Please actually make an attempt yourself first. That's how you will learn, by actually trying it. Post again with your updated code.

            sicarie

            Comment

            • cube
              New Member
              • Feb 2008
              • 18

              #7
              Originally posted by sicarie
              cube-

              Please actually make an attempt yourself first. That's how you will learn, by actually trying it. Post again with your updated code.

              sicarie
              I'm sorry if I made you think that I'm looking for someone to solve my assignments, I didn't mean it.
              I'm a married working woman with a child (and mature enough to respect the "laws" of universities) and I really really luck of free time.
              I do my best and I want to learn as much as I can, though my whole schedual is too hard (to keep up with), I don't want to give up. I have worked to the bone with this one and I couldn't figure out what to do.
              The help that I'm waiting for is sthg like "Try to use the "while" or the "else if" ect"
              that's why I asked for a hint and not for the solution.
              Am I asking too much???

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by looker
                You all right!... The last condition must be if ((move_main==9) && (score_main[i]!=3) && (score_main[i]!=-3)).
                In your code above, you have 4 loops. In the two above, you check for score_main[i] value, the last 2 more, you check for score_main[i] and move_main.
                All the four loops are used to assigned the value to variable result. This is the point.
                Check yoru design again, If the condition has already been true in the first loop ( example ), it means the variable result is already set value. Is it necessary to go through the rest of code, i see that is not useful and your program can be more slower.
                - In fact, you loops from 0 to 8 incremented by 1. You repeated this loop four times witch different condition stay inside. What if you just have only one loop and 4 condition ?
                - If there is not codes below your code posted here, why you not return immediately after variable result is set. because your current design lead your program to go through the rest of code even though that is not necessary.
                You do have that. You then asked for more.

                Comment

                Working...