Thread: struggeling with an if statment clause

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

    struggeling with an if statment clause

    ill try to explain the clauses for it to be true

    x can be 1 or -1 but has to be one of those 2
    y has to be 1 if king is false or y can be 1 or -1 if king is true
    Code:
      
        x=1 | x=-1 | y=1 | y=-1 | king true | king false | desired result
        ____|______|_____|______|___________|____________|_________________
          0 |  1   |  0  |  1   |    0      |    1       |   0
          0 |  1   |  1  |  0   |    0      |    1       |   1    
          1 |  0   |  0  |  1   |    0      |    1       |   0
          1 |  0   |  1  |  0   |    0      |    1       |   1
          0 |  1   |  0  |  1   |    1      |    0       |   1
          0 |  1   |  1  |  0   |    1      |    0       |   1    
          1 |  0   |  0  |  1   |    1      |    0       |   1
          1 |  0   |  1  |  0   |    1      |    0       |   1
    hopefully the truth table will come out ok
    coop

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i have tried
    Code:
    if (((x == -1 || x == 1) && (y == 1 || y ==-1) && king == true) || ((x == -1 || x== 1) && y == 1))
    but it always was true regardless of y and the king
    coop

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I would focus on the two result=0 cases.

    This is true, regardless of the state of x/y
    if ( king == 1 ) result = 1;

    Also
    if ( !king && ( (x==-1 && y==-1) || (x == 1 && y== -1) ) ) result = false;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    This is not a truth map.... I'm assuming (x==-1) means truth and (y==-1) means truth, otherwise, false. You'll end up with this table:
    Code:
    x=-1 | y=-1 | king true | result
     (A) |  (B) |    (C)    |   (S)
    _____|______|___________|_________________
     0   |  0   |    0      |    1 = ~A~B~C
     0   |  0   |    1      |    1 = ~A~BC
     0   |  1   |    0      |    0
     0   |  1   |    1      |    1 = ~ABC
     1   |  0   |    0      |    1 = A~B~C
     1   |  0   |    1      |    1 = A~BC
     1   |  1   |    0      |    0
     1   |  1   |    1      |    1 = ABC
    Using a kmap (as I showed before), you can simplify this with:
    struggeling with an if statment clause-map-png
    Which will result in S=~B || C. Your condition should be:
    Code:
    if (y==1 || king ) ...

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    trouble is i havent done demorgans and kaunaugh for about 25 years and then it confused me how you decided which square was A and which was ~A for example however i take your point all i need is the statment
    if ((king == true || y == 1) && (x == -1 || x == 1)) as although x doesn't infulence the outcome i need to test that it is either 1 or -1

    coop
    Last edited by cooper1200; 05-07-2019 at 09:21 AM.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cooper1200 View Post
    trouble is i havent done demorgans and kaunaugh for about 25 years and then it confused me how you decided which square was A and which was ~A
    It is in the FIRST sentence on my text!
    Quote Originally Posted by cooper1200
    for example however i take your point all i need is the statment
    if ((king == true || y == 1) && (x == -1 || x == 1)) as although x doesn't infulence the outcome i need to test that it is either 1 or -1
    Then, again, your truth table is wrong... There are 5 vars, not 6, unless "king" can be different from true or false.
    With 5 vars you should have a truth table with 32 entries... with 6, 64...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If clause based on a string
    By Interista in forum C Programming
    Replies: 15
    Last Post: 10-14-2011, 03:01 AM
  2. need help with go to statment
    By begginer in forum C Programming
    Replies: 15
    Last Post: 04-06-2011, 10:54 AM
  3. LINQ, Verbose WHERE Clause
    By alphaoide in forum C# Programming
    Replies: 4
    Last Post: 08-04-2010, 09:04 AM
  4. problem with cin.fail() in if/else clause
    By clearcom in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2010, 10:11 PM
  5. if clause not working as I expect.
    By luise.valencia in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 08:35 PM

Tags for this Thread