Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By memcpy

Question regarding && || and negation (!)

This is a discussion on Question regarding && || and negation (!) within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to make a program that is able to determine the color of a target stimulus (a ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    5

    Question regarding && || and negation (!)

    Hello,
    I am trying to make a program that is able to determine the color of a target stimulus (a big square) depending on the color of 3 stimuli that are presented before the target (3 small squares). The colors of these 3 small squares will be randomly determined but they have to follow a specific respons sequence (for example 2 small squares with the same color as the target square).

    One small example of how i did this for a specific subset in this sequence:
    Code:
    if (data[i].seq==0){      // Sequence : 342312143241 (expected respons = 3 on trial 1 4 on trial 2 and so on..)
        if (data[i].trialnr == 1 || 13 || 25 || 37 || 49 || 61 || 73 || 85 || 4 || 16 || 28 || 40 || 52 || 64 || 76 || 88 || 9 || 21 || 33 || 45 || 57 || 69 || 81 || 93){  // Select all the trials in which my expected respons is 3
        do {                           // in this do function i keep giving a random number (that is associated with a color in a later function)
        data[i].colorsmall1= ts_rint(4);
        data[i].colorsmall2= ts_rint(4);
        data[i].colorsmall3= ts_rint(4);    
        } while (!((data[i].colorsmall1== data[i].colorsmall2 && data[i].colorsmall2!= data[i].colorsmall3) || (data[i].colorsmall1!=data[i].colorsmall2 && data[i].colorsmall2==data[i].colorsmall3) || (data[i].colorsmall1=data[i].colorsmall3 && data[i].colorsmall2!=data[i].colorsmall3)));        // I keep generating random numbers untill the color of 2small squares is the same (and NOT 3 small squares that have the same color). There are 3 cases in which this can happen so i seperate this with an OR statement (||) 
        if (data[i].colorsmall1 == data[i].colorsmall2 && data[i].colorsmall2!= data[i].colorsmall3){
        data[i].colorbig = data[i].colorsmall1;}                                                // Make the big square the same color as the 2 squares that share the color , in this case square 1 and 2
        if(data[i].colorsmall1!=data[i].colorsmall2 && data[i].colorsmall2==data[i].colorsmall3){
        data[i].colorbig = data[i].colorsmall3;}                                            // Make the big square the same color as the 2 squares that share the color , in this case square 2and 3
    
    
    if(data[i].colorsmall1=data[i].colorsmall3 && data[i].colorsmall2!=data[i].colorsmall3){
        data[i].colorbig = data[i].colorsmall3;}                                // Make the big square the same color as the 2 squares that share the color , in this case square 1and 3
    Now I personally think the problem lies in my while statement especially the negation (!) part and if that works for the whole structure (that is why i used all the parantheses):
    Code:
     while (!((data[i].colorsmall1== data[i].colorsmall2 && data[i].colorsmall2!= data[i].colorsmall3) || (data[i].colorsmall1!=data[i].colorsmall2 && data[i].colorsmall2==data[i].colorsmall3) || (data[i].colorsmall1=data[i].colorsmall3 && data[i].colorsmall2!=data[i].colorsmall3)));
    Anyone who can explain to me what is wrong with this statement or knows of a more elegant way to program my problem?

    Thanks in advance!

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Code:
        if (data[i].trialnr == 1 || 13 || 25 || 37 || 49 || 61 || 73 || 85 || 4 || 16 || 28 || 40 || 52 || 64 || 76 || 88 || 9 || 21 || 33 || 45 || 57 || 69 || 81 || 93){  // Select all the trials in which my expected respons is 3
    No, you can't do this. Every comparison must be separate. Which is weird because you seem to know that based on your other code.

    Code:
    while (!((data[i].colorsmall1== data[i].colorsmall2 && data[i].colorsmall2!= data[i].colorsmall3) || (data[i].colorsmall1!=data[i].colorsmall2 && data[i].colorsmall2==data[i].colorsmall3) || (data[i].colorsmall1=data[i].colorsmall3 && data[i].colorsmall2!=data[i].colorsmall3)));
    Too complicated. Break it up/do it differently.

    Code:
     if (data[i].trialnr == 1 || data[i].trialnr == 13, etc)

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Oh man, what a silly mistake, can't believe I missed that. I guess staring at code for a few hours can do that... :/

    Thanks for the help! I will try to break it up in pieces. Is the way in which I use the negation (!) a possible way or can you not use (!) in that way? (i.e. can you use ! befor multiple terms, as you would for example use a - before alot of parantheses in mathmatics?)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    For the trialnr thing: you may find it easier to create an array with the numbers in sorted order, then use bsearch to check if the number exists in the array. The sorting would also make it easier for you to add or remove numbers in the future.
    rags_to_riches likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Thanks for the suggestion laserlight!
    It seems like I don't fully understand how the negation command (!) works. What happens if I for example put an ! before a statement with an OR condition:

    Code:
     ! ( x || y || z)
    Does the meaning of || change because of the negation sign (and why does it change?)? Since the code does seem to work if i would use:
    Code:
     ! ( x && y && z)
    Thanks in advance!

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You could always try something like this for your comparison

    Code:
    bool is_valid(int val, int array[], int len)
    {
    	int i;
    	
    	for (i = 0; i <= len; i++) 
    		if (val == array[i])
    			return true;
    	
    	return false;
    }
    
    
    int array[] = {1, 13, 25, 37, 49, 61, 73, 85, 4, 16, 28, 40, 52, 64, 76, 88, 9, 21, 33, 45, 57, 69, 81, 93};
    	
    if (is_valid(data[i].trialnr, array, sizeof(array))) {
    	/* do stuff
    }
    rags_to_riches likes this.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Quote Originally Posted by Teferi9 View Post
    Thanks for the suggestion laserlight!
    It seems like I don't fully understand how the negation command (!) works. What happens if I for example put an ! before a statement with an OR condition:

    Code:
     ! ( x || y || z)
    Does the meaning of || change because of the negation sign (and why does it change?)? Since the code does seem to work if i would use:
    Code:
     ! ( x && y && z)
    Thanks in advance!
    Experiment! Make some code to run tests.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    By the way, I note that memcpy's code example in post #6 contains an array out of bounds access bug. In general, don't use example code blindly because it may not have been sufficiently tested, if it was tested at all. Also, you should #include <stdbool.h> for bool, true and false.

    bsearch has an advantage in that it is likely to be implemented as binary search and hence has a better time complexity than the linear search that memcpy demonstrated. On the other hand, the array is small, so there is little benefit.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Mmm I tried various different codes and it seems my interpretation is correct (my complicated while condition even works!). Apparently the problem was that when I wanted to put my code (these complex loops were in my trial function) into my experiment I used the following code
    Code:
     for (i = 0; i < 10; i++){                  trial(i);}
    In my randomisation function i used
    Code:
     data[i].trialnr = i+1;
    So that in my file the first trial would have the value 1. When I used this code my randomisation didn't work and I sometimes got 3 different colors of squares when 2 had to be the same.

    I switched my code to
    Code:
     data[i].trialnr = i;
    and
    Code:
     for (i = 1; i < 10; i++){               
            trial(i);
    respectively, and it suddenly all works. Now I'm happy that it works, but I would like to know why? Even my data[i].trialnr starts at 1 in my file where I print the values... (and I would expect it to start at 0, since (i) starts at 0...)

    @ Laserlight : Thanks I indeed found out that i needed to include stdbool.h However i'm wondering why there is an array out of bounds access bug? Isn't it okay because all the variables are defined as integers and the size of my array ( 24) doesn't exceed the boundary? What I am wondering is how the function memcpy gave me knows the values for len & val, since they are not defined? Or is this something special in the stdbool.h package?

    I'm sorry if some of these questions are stupid, but I'm quite new to programming in general.
    Last edited by Teferi9; 02-25-2012 at 02:48 PM.

  10. #10
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Example of the array out of bounds error in memcpy's code. Note how any elements are in the array he presented, then how his bug affected the results:

    Code:
    #include <stdio.h>
    
    int main()
    {
       int array[] = {1, 13, 25, 37, 49, 61, 73, 85, 4, 16, 28, 40, 52, 64, 76, 88, 9, 21, 33, 45, 57, 69, 81, 93};
    
       printf("memcpy's sizeof array (intended to equal the number of elements in the array): %lu\n", sizeof(array));
       printf("real number of array elements): %lu\n",
          sizeof(array)/sizeof(array[0]));
    
       return 0;
    }
    Code:
    $ ./aob
    memcpy's sizeof array (intended to equal the number of elements in the array): 96
    real number of array elements): 24
    This is because whenever you use sizeof() in calculating number of elements in the array, it's not accounting for the size of the datatype contained in the array, in this case integers; it's giving you number of elements * sizeof the datatype of each element. Therefore, to use this method you must divide by the sizeof the first element in the array to get the true number of elements contained therein.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    I suggest you study De Morgan's laws - Wikipedia, the free encyclopedia to get used to manipulating and understanding boolean logic.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Ok, thanks for the help and suggestions!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Negation help!
    By Rukris in forum C Programming
    Replies: 2
    Last Post: 12-20-2011, 08:02 AM
  2. Weird number negation problem
    By barramundi9 in forum C Programming
    Replies: 6
    Last Post: 05-13-2011, 09:44 AM
  3. bitwise negation problem
    By Mr_Jack in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2004, 07:16 PM
  4. bitwise negation
    By Sathyabodh in forum C Programming
    Replies: 1
    Last Post: 08-12-2003, 09:42 AM
  5. Logical negation
    By samps005 in forum C Programming
    Replies: 7
    Last Post: 10-12-2002, 06:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21