Thread: Logical operators help with formula

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    18

    Logical operators help with formula

    Hi guys, I have problem with this: I need program A,B,C,D:
    that when I write 4 numbers program solve
    truth (1)/false (0) I have "formula" ¬(A ∨ B) ∧ ¬C ∧ (C ∨ D)

    I have program, but i can write whatever and everytime program write 0 (False). I am beginner in programming. Can you help me?

    I have two version:
    Code:
    int main()
    {
        printf("A,B,C,D:\n");
        int a,b,c,d;
        int first = (!(a||b));
        int second = (!(c));
        int third = (c||d);
        scanf("%d,%d,%d,%d",&a,&b,&c,&d);
        printf("%d",(!(a||b))&&(!(c))&&(c||d));
        return 0;
    }
    OR
    Code:
    int main()
    {
        printf("A,B,C,D:\n");
        int a,b,c,d;
        scanf("%d,%d,%d,%d",&a,&b,&c,&d);
        result =((((!(a||b))&&(!(c))&&(c||d));
        printf("%d",result);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Your first version appears to work, but you do have three unused variables in there: "first", "second", and "third".

    Each of the three combinations must be true for the total output to be true:

    !(a||b) is true only if a=0 and b=0
    !(c) is true only if c=0
    (c||d) is true only if d=1 (c must be 0 for the second combination to be 1)

    So only one set of input values will produce an output of 1:

    0, 0, 0, 1

    -
    Last edited by megafiddle; 10-16-2015 at 09:49 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > scanf("%d,%d,%d,%d",&a,&b,&c,&d);
    Did you really type in the commas when you gave your input to the program?

    If you didn't, then scanf would end early, and you would be left with garbage in your variables.

    Consider
    Code:
    if ( scanf("%d,%d,%d,%d",&a,&b,&c,&d) == 4 ) {
      // do work
    } else {
      // print error message
    }
    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
    Oct 2015
    Posts
    18
    Quote Originally Posted by Salem View Post
    > scanf("%d,%d,%d,%d",&a,&b,&c,&d);
    Did you really type in the commas when you gave your input to the program?

    If you didn't, then scanf would end early, and you would be left with garbage in your variables.

    Consider
    Code:
    if ( scanf("%d,%d,%d,%d",&a,&b,&c,&d) == 4 ) {
      // do work
    } else {
      // print error message
    }
    Yes I have commas in scanf.

  5. #5
    Registered User
    Join Date
    Oct 2015
    Posts
    18
    Quote Originally Posted by megafiddle View Post
    Your first version appears to work, but you do have three unused variables in there: "first", "second", and "third".

    Each of the three combinations must be true for the total output to be true:

    !(a||b) is true only if a=0 and b=0
    !(c) is true only if c=0
    (c||d) is true only if d=1 (c must be 0 for the second combination to be 1)

    So only one set of input values will produce an output of 1:

    0, 0, 0, 1

    -
    So, How to define
    !(a||b) is true only if a=0 and b=0
    !(c) is true only if c=0
    (c||d) is true only if d=1 (c must be 0 for the second combination to be 1) ?
    define this in integers or some with IF ?

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by Jamesss View Post
    Yes I have commas in scanf.
    Just to be clear, you are using commas between the values when you input them:

    0,0,0,1 <Enter>

    This will not work:

    0001 <Enter>

    Quote Originally Posted by Jamesss View Post
    So, How to define
    !(a||b) is true only if a=0 and b=0
    !(c) is true only if c=0
    (c||d) is true only if d=1 (c must be 0 for the second combination to be 1) ?
    define this in integers or some with IF ?
    They are already defined by the operators in the statements.

    For example, ! is the NOT operator:
    If you enter a value of 0 for c, then !(c) will equal 1
    If you enter a value of 1 for c, then !(c) will equal 0

    -

  7. #7
    Registered User
    Join Date
    Oct 2015
    Posts
    18
    Quote Originally Posted by megafiddle View Post
    Just to be clear, you are using commas between the values when you input them:

    0,0,0,1 <Enter>

    This will not work:

    0001 <Enter>



    They are already defined by the operators in the statements.

    For example, ! is the NOT operator:
    If you enter a value of 0 for c, then !(c) will equal 1
    If you enter a value of 1 for c, then !(c) will equal 0

    -
    Ou, now i understand. So I delete commas in scanf so now I can use values : 5 2 4 5 <enter>

    But still not work.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code, the test input, expected output and actual output?
    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

  9. #9
    Registered User
    Join Date
    Oct 2015
    Posts
    18
    Quote Originally Posted by laserlight View Post
    What is your current code, the test input, expected output and actual output?

    I want to scanf 4 numbers and get back 0 or 1.

    Code:
    int main()
    {
        printf("A,B,C,D:\n");
        int a,b,c,d;
        int first = (!(a||b));
        int second = (!(c));
        int third = (c||d);
        scanf("%d %d %d %d",&a,&b,&c,&d);
        (printf("%d",(!(a||b))&&(!(c))&&(c||d))
        return 0;
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jamesss
    I want to scanf 4 numbers and get back 0 or 1.
    Your code does not compile. Fix the compile error.

    If you posted the compile error and told us what you tried to do to fix it but failed, I would have told you how to fix it. Since you didn't, I am leaving finding out the compile error as a necessary exercise.
    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

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why did you write this part?
    Code:
       int first = (!(a||b));
       int second = (!(c));
       int third = (c||d);

    Are you aware that you can do first && second && third in printf, and yet still analyze the parts separately? That way you can make changes that lead to the results you want.

  12. #12
    Registered User
    Join Date
    Oct 2015
    Posts
    18
    Quote Originally Posted by whiteflags View Post
    Why did you write this part?
    Code:
       int first = (!(a||b));
       int second = (!(c));
       int third = (c||d);

    Are you aware that you can do first && second && third in printf, and yet still analyze the parts separately? That way you can make changes that lead to the results you want.
    I try it but it doesnt work so I was think that full formula will be better. But all formula doesnt work too so i have problem in code somewhere and I dont know where. I delete commas in scanf. Whatever i write some 4 random number program give me back only 0.

    Code:
    int main()
    {
        printf("A,B,C,D:\n");
        int a,b,c,d;
        int first = (!(a||b));
        int second = (!(c));
        int third = (c||d);
        scanf("%d %d %d %d",&a,&b,&c,&d);
        printf("%d",first && second && third);
        return 0;
    }
    Last edited by Jamesss; 10-18-2015 at 12:55 PM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jamesss
    I try it but it doesnt work
    Stop saying this. Back in post #8, I asked "What is your current code, the test input, expected output and actual output?" That is what you should provide when you try something and it doesn't work. Just saying that you tried it is not enough. Just saying that it doesn't work is not enough. What did you try and how does it not work? Posting the code that you tried answers the "what did you try"; posting the test input, expected output and actual output answers "how does it not work".

    Quote Originally Posted by Jamesss
    so I was think that full formula will be better. But all formula doesnt work too so i have problem in code somewhere and I dont know where. I delete commas in scanf. Whatever i write some 4 random number program give me back only 0.
    Break the problem into smaller pieces that you can solve. For example, start by hard coding test input, i.e., do not use scanf. Just write the part that does the formula and prints the result.

    EDIT:
    Oh, I see that you edited your post to include code. That is good. The problem is that you are performing the operations before you have read the input. I suggest something like this:
    Code:
    if (scanf("%d %d %d %d", &a, &b, &c, &d) == 4)
    {
        printf("%d\n", !(a || b) && !(c) && (c || d));
    }
    You can see that Salem already suggested something similiar earlier.
    Last edited by laserlight; 10-18-2015 at 01:07 PM.
    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

  14. #14
    Registered User
    Join Date
    Oct 2015
    Posts
    18
    Quote Originally Posted by laserlight View Post
    Stop saying this. Back in post #8, I asked "What is your current code, the test input, expected output and actual output?" That is what you should provide when you try something and it doesn't work. Just saying that you tried it is not enough. Just saying that it doesn't work is not enough. What did you try and how does it not work? Posting the code that you tried answers the "what did you try"; posting the test input, expected output and actual output answers "how does it not work".


    Break the problem into smaller pieces that you can solve. For example, start by hard coding test input, i.e., do not use scanf. Just write the part that does the formula and prints the result.

    EDIT:
    Oh, I see that you edited your post to include code. That is good. The problem is that you are performing the operations before you have read the input. I suggest something like this:
    Code:
    if (scanf("%d %d %d %d", &a, &b, &c, &d) == 4)
    {
        printf("%d\n", !(a || b) && !(c) && (c || d));
    }
    You can see that Salem already suggested something similiar earlier.
    Oh my god The only problem it scanf on the end... I thought that scanf order it doesnt matter. I first write all INT and after this scanf and final OUT ....I am stupid ... but now I know it for future Thanks very much for helping and strong nerves

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators
    By Geagle in forum C Programming
    Replies: 2
    Last Post: 08-30-2012, 10:02 PM
  2. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  3. logical operators
    By Marlon in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 02:55 AM
  4. Logical Operators
    By E i F x 65 in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2002, 08:45 AM
  5. logical operators
    By sballew in forum C Programming
    Replies: 4
    Last Post: 09-04-2001, 06:24 PM

Tags for this Thread