Notices
Results 1 to 13 of 13
Like Tree2Likes
  • 1 Post By Strange
  • 1 Post By RedPanda

Thread: What boolean operation will suit this need in C++?

  1. #1 What boolean operation will suit this need in C++? 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    anyway, my code is
    Code:
    bool checker(void){
        ​         bool result=0;
                 for(int y=0;y<5;y++)
                     {result^=(box[y]!=pill[0]&&box[y]!=pill[1]);}
    return result;
                    }
    While looping, i want 'result' to be, and only be 1 if all variables in the array box[] contain either pill[0] or pill[1]

    i used ^= but it seems to not work.

    all help deeply apprecieated


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,540
    I would code it as something like
    Code:
    bool result=true;
    for (int i = 0; i < N; i++) {
        if (box[y] != pill[0]) && (box[y] != pill[1]) {
            result = false;
            // note: you could break out of the loop at this point to save time
        }
    }
    Have you learnt de Morgan's theorem? It can be useful when sorting out the logic for complicated conditions like this.

    It is more intuitive, perhaps, to write the condition as
    Code:
    ! ( (box[y] == pill[0]) || (box[y] == pill[1]) )
    which is, by de Morgan's theorem, the same as above. (I hope!)


    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  4. #3  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    i actually have done what i needed, actually i was coding in a 2d array....
    my current code that works
    its kinda long, i cant find a way to shorten it.
    Code:
    bool drawfind (void){
    		bool allfilled=1;
                        for(int x=0;x<3;x++)
                                {for(int y=0;y<3;y++){
                                    if(allfilled==1)
                                        {allfilled=(box[x][y]==sym[0]||box[x][y]==sym[1]);}
                                }
                                }
                        return(allfilled&&!winfind());
    }
    any way to shorten this long code?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  5. #4  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,540
    Quote Originally Posted by RamenNoodles View Post
    any way to shorten this long code?
    I would go for clarity rather than brevity. Every time. Much, much more important. (Wait until you have to go back and modify some code you wrote a year ago.) And it won't make any difference to the compiler; the output will probably be pretty much identical. You have to work quite hard to write code that a good compiler can't optimise.

    In fact, it is so important, I'm going to repeat it: I would always go for clarity rather than brevity.
    RedPanda likes this.
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  6. #5  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    i dont get what you meant by "Brevity"
    • brev·i·ty/ˈbrevitē/

      Noun
      1. Concise and exact use of words in writing or speech.
      2. Shortness of time.
      Synonyms
      briefness - conciseness - shortness - concision - short

    i still dont get what it means
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  7. #6  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,540
    Make your code clear. Not short. If writing 3 lines instead of a single (complex) expression makes it easier to understand then that is fine.
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  8. #7  
    ▼▼ dn ʎɐʍ sıɥʇ ▼▼ RedPanda's Avatar
    Join Date
    Aug 2012
    Location
    UK
    Posts
    2,737
    Quote Originally Posted by Strange View Post
    In fact, it is so important, I'm going to repeat it: I would always go for clarity rather than brevity.
    And if I could 'Like' your post twice, I would.
    Strange likes this.
    SayBigWords.com/say/3FC

    "And, behold, I come quickly;" Revelation 22:12

    "Religions are like sausages. When you know how they are made, you no longer want them."
    Reply With Quote  
     

  9. #8  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    Hey, i have a problem about variable initialization.
    what i want is, for example,
    Code:
    void main (void){int 
    a=1,
    b=2,
    sum=a+b;
    cout<<sum;
    cin>>a>>b;
    cout<<sum;}
    i want sum to be always a+b, instead of always typing sum=a+b before everytime i need to use sum
    (by the way, a and b will keep changing, in the program i am doing)

    is it something to do with const or something else im rusty about this

    will const sum=a+b; make sum be permanently at a+b at the moment it was defined, or keep changing whenever a or b changes?
    Last edited by RamenNoodles; June 29th, 2013 at 11:40 AM.
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  10. #9  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,540
    Quote Originally Posted by RamenNoodles View Post
    i want sum to be always a+b, instead of always typing sum=a+b before everytime i need to use sum
    Then you are using the wrong language.

    This would be trivial in a functional programming language, such as ML or Haskell. I don't think there is a way of doing this in C++. (Note: I am deliberately not mentioning macro definitions as they are evil and against the spirit of C++. But they would allow you to do what you want. At the cost of making your program less clear. Which Is A Very Bad Thing.)
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  11. #10  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    what if i use...
    Code:
     const int sum=a+b;
    1) will the value of sum be constanted of the sum of values a and b, with the values a and b only at the moment sum is defined?

    2) or will the value of sum change whenever a or b changes?
    Last edited by RamenNoodles; July 8th, 2013 at 08:08 AM.
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  12. #11  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,540
    1.

    Because const tells the compiler that the value of a variable will not be changed (and, therefore allows it to make various optimizations).
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  13. #12  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    so will

    const int sum = a + b;

    does it mean that const will always be a + b,
    or is the calculation a + b; done at the moment sum is initialized, and remains constant at that value throughout the program, no matter if a is changed, or b is changed?

    when you said
    1.
    then can c++ support my request?
    any class or any function in any libraries?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  14. #13  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,540
    Quote Originally Posted by RamenNoodles View Post
    so will

    const int sum = a + b;

    does it mean that const will always be a + b,
    or is the calculation a + b; done at the moment sum is initialized, and remains constant at that value throughout the program, no matter if a is changed, or b is changed?
    The value of sum is fixed at the time it is defined and will not depend on any later changes to a or b. That is because C++ is a procedural language where the order and timing of statements is what defines the behaviour.

    then can c++ support my request?
    any class or any function in any libraries?
    No. And it would be confusing if it could because someone looking at the code would expect the value of sum to reflect the last value assigned to it (and not to change because some other variable changes).

    Perhaps you could explain why you want to do this. It may be that there is a better way of changing the same thing. For example, say you had a variable called area which you wanted to be automatically updated when you change the value of width or length, then one way of doing this would be to define a class which implements this behaviour.
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

Similar Threads

  1. Reducing the operation by using the modulo
    By amit28it in forum Mathematics
    Replies: 3
    Last Post: November 16th, 2011, 08:04 AM
  2. Boolean algebra question
    By xtheunknown0 in forum Computer Science
    Replies: 5
    Last Post: April 21st, 2011, 05:08 PM
  3. Operation Systems Concept help
    By Shadowsundying in forum Computer Science
    Replies: 8
    Last Post: August 2nd, 2010, 01:24 AM
  4. Boolean algebra
    By asalvani in forum Computer Science
    Replies: 16
    Last Post: June 3rd, 2009, 02:31 AM
  5. Boolean Alegbra
    By cheekychop in forum Computer Science
    Replies: 2
    Last Post: May 14th, 2008, 05:15 PM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •