Or operator?

This is a discussion on Or operator? within the C++ Programming forums, part of the General Programming Boards category; Trying to get my Or operator to work and i guess it just isn't. Code: switch (rdm) { case 0: ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    82

    Or operator?

    Trying to get my Or operator to work and i guess it just isn't.

    Code:
    switch (rdm)
              {
                  case 0: // Go Forward
                     { if (Grid[++x][y] != 1||2||3)
                      {
                       Grid[x][y] = Output[j];
                       Pop[index][j] = 1;
                       cout << "Forward Picked" << endl;
                      }    
                      else
                      {
                          Grid[--x][y];
                          cout << "Error" << endl;
                      }    
                     }    
                     break;
    If case = 0, i want it to check the forward array in a 2d array and only procede if there isn't a 1,2 or 3 int in the element.

    Can i use an Or operator in the If statement like that?

    Thanks

    Regards Wolfe

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> if (Grid[++x][y] != 1||2||3

    Code:
    if ( Grid[++x][y] != 1 || Grid[++x][y] != 2 || Grid[++x][y] != 3 )
    It's long, and convoluted, but necessary. I think there was a post about something similar recently.

    EDIT:

    This probably does work (the ++x thing for the array), but out of habit I would be more inclined to use Grid[x+1][y], I think it's just a habit and style thing.
    Last edited by twomers; 07-13-2006 at 03:51 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Damn made a mistake elsewhere i think

    if ( Grid[++x][y] != 1 || Grid[++x][y] != 2 || Grid[++x][y] != 3 )

    Hopefully this should work but i have to change it to:

    if ( Grid[++x][y] != 1 || Grid[x][y] != 2 || Grid[x][y] != 3 )

    So it only increments once.

    back in a mo

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Does [++x] actually increment it, or does it just 'peek' to the next one. I genuinally don't know. Just wondering. Oh, please use code tags, even for small snippets, or quite tags if your quoting code.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Are you sure this is what you mean? Wouldn't this be true always?

    Example:
    a = 5
    if a != 1 (true) or ... -> true

    a = 1
    if a != 1 (false) or a != 2 (true) or ... -> true

    May-be you mean, if a!=1 && a!=2 && a!= 3 ? ( !(a==1||a==2||a==3) )

    Then may-be you could use:
    if a > 3
    and/or
    if a >=1 && a <= 3


    P.S The ++ operator changes the value, so you couldn't use it in multiple evaluations. The code would probably easier to read if you incremented the variable outside the evaluations.

    By the way: grid[--x][y]; is a really weird way to decrement x.
    Last edited by anon; 07-13-2006 at 04:36 PM.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Your right anon, thanks

    Also added a j-- so it repets without losing a turn because of error
    and i also removed the check for 3 as it wasn't needed.

    here is the whole function for anyone interested.
    Yes no strings Anon

    Code:
    void GetFillGrid(int (Output)[10], int (Pop)[PopSize][AAlgh], int (Grid)[X][Y] )
    // Fill grid with an Individual and then store the sequence {Forward,Left,Right}
    // as one of the population until whole population is created.
    {
            int x = 12;
            int y = 12;
            int rdm;
            int index;
            int j;
            
        for (index = 0; index < PopSize; index++)
        {
            
            
          Grid[x][y] = Output[0];
          
          for ( j = 1; j < PopSize; j++)
          {
             rdm = rand()%3;
              switch (rdm)
              {
                  case 0: // Go Forward
                     { if (Grid[++x][y] != 1 && Grid[x][y] != 0 /*|| Grid[x][y] != 3*/)
                      {
                       Grid[x][y] = Output[j];
                       Pop[index][j] = 1;
                       cout << "Forward Picked" << endl;
                      }    
                      else
                      {
                          Grid[--x][y];
                          j--;
                          cout << "Error" << endl;
                      }    
                     }    
                     break;
                  case 1: // Go Left
                     { if (Grid[x][--y] != 1 && Grid[x][y] != 0 /*|| Grid[x][y] != 3*/)    
                      {  
                       Grid[x][y] = Output[j];
                       Pop[index][j] = 2;
                       cout << "Left Picked" << endl;
                      }    
                      else
                      {
                          Grid[x][++y];
                          j--;
                          cout << "Error" << endl;
                      }    
                     }    
                    break;                            
                  case 2: // Go Right
                     { if (Grid[x][++y] != 1 && Grid[x][y] != 0 /*|| Grid[x][y] != 3*/)
                      {
                       Grid[x][y] = Output[j];
                       Pop[index][j] = 3; 
                       cout << "Right Picked" << endl;
                      }    
                      else
                      {
                          Grid[x][--y];
                          j--;
                          cout << "Error" << endl;
                      }    
                     }    
                     break;
                     
                     default:
                         break;
              }    
          }    
          
              for (index = 0; index < 24; index++)
              {
                  for (j = 0; j < 24; j++)
                  {
                      cout << Grid[index][j] << " ";
                  }
                  cout << endl;
                  cin.get();
                  
              }        
                              
        }    
                          
    }

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> (int (Output)[10], int (Pop)[PopSize][AAlgh], int (Grid)[X][Y] )

    Just our of curiosity, why do you have the variable name in ()'s? that could be:

    Code:
    (int Output[10], int Pop[PopSize][AAlgh], int Grid[X][Y] )
    if I'm not much mistaken.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Grid is an array i beleive it needs to be in Brackets () in order for it to be called as an argument or parameters something like that.

    could be wrong.

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Try taking them out, and see if it works. And post the answer here.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Weird i know i definately read about adding them so i did, but yes your right they are not needed, now i've got to find out where i read it

    Thanks..

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Why are you starting x and y at 12 and incrementing from there? Are you not concerned about the first 12 elements? (actually 13 if you count that you increment right away) Those kinds of things should have comments expaining why you're doing that. Magic numbers are just magic numbers if we can't see the whole code.
    Sent from my iPad®

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Sorry.....

    It's set to 12/12 becuase i need to make sure the sequence created doesn't go "out of bounds"

    Here is the wholfe thing if you want a look.

    Oh and input has to be over 10 int's

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #define RAND_MAX 3
    #include <time.h>
    #define PopSize 10
    #define AAlgh 12
    #define X  24
    #define Y  24
    
    
    
    using namespace std;
    using std::string;
    using std::cout;
    using std::endl;
    using std::cin;
    
    
    void GetUserInput(string &Input)
    
    {
        cout << "Please enter Amino Acid Sequence \"H,P\": " << endl;
        cin >> Input;
         
        cin.get();
    }
    
    void GetSetupString(string theInput, int Output[10])
    // To declare an Array as a reference from Main, syntax is differant then other 
    // references, ie: Type (&ArryName)[Size], try finding that in any tutorial :) 
    // Input is now changed into the correct alphabet {1,0} and stored in output
    // edit, don't need to reference Arrys.
    {
    
    
       int index;
       
       for (index = 0; index < 10; ++index)
       {
           switch (theInput[index])
           {
               case 'H':
                   Output[index] = 1; 
                   break;
               case 'P':
                   Output[index] = 0;
                   break;
               default:
                   break;
        
           }   
            cout << Output[index] ;
       }  
    cout << endl;
    cin.get();
    }    
    
    void GetCreateGrid(int Grid[X][Y])
    {
     // Fill each element in arry
     int index;
     int j;
     
     
     for (index = 0; index < X; index++)
     {
         for ( j = 0; j < Y; j++)
         {
             Grid[index][j] = 8;
             cout << Grid[index][j] << " " ;
         }
        cout << endl;
         
     
     }        
     cout << "Start Filling Grid" << endl;                          
    }    
    
    void GetFillGrid(int Output[10], int Pop[PopSize][AAlgh], int Grid[X][Y] )
    // Fill grid with an Individual and then store the sequence {Forward,Left,Right}
    // as one of the population until whole population is created.
    {
            int x;
            int y;
            int rdm;
            int index;
            int j;
            int i;
            
        for (index = 0; index < PopSize; index++)
        {
            // clear whole grid again?
           
           x = 12; 
           y = 12;
           
          Grid[x][y] = Output[0]; // Place first element in [12][12] so it is
                                  // impossible for it to go out of bounds
          
          for ( j = 1; j < PopSize; j++)
          {
             rdm = rand()%3;
              switch (rdm)
              {
                  case 0: // Go Forward
                     { if (Grid[++x][y] != 1 && Grid[x][y] != 0 )
                      {
                       Grid[x][y] = Output[j];
                       Pop[index][j] = 1;
                       cout << "Forward Picked" << endl;
                      }    
                      else
                      {
                          Grid[--x][y];
                          j--;
                          cout << "Error" << endl;
                      }    
                     }    
                     break;
                  case 1: // Go Left
                     { if (Grid[x][--y] != 1 && Grid[x][y] != 0 )    
                      {  
                       Grid[x][y] = Output[j];
                       Pop[index][j] = 2;
                       cout << "Left Picked" << endl;
                      }    
                      else
                      {
                          Grid[x][++y];
                          j--;
                          cout << "Error" << endl;
                      }    
                     }    
                    break;                            
                  case 2: // Go Right
                     { if (Grid[x][++y] != 1 && Grid[x][y] != 0 )
                      {
                       Grid[x][y] = Output[j];
                       Pop[index][j] = 3; 
                       cout << "Right Picked" << endl;
                      }    
                      else
                      {
                          Grid[x][--y];
                          j--;
                          cout << "Error" << endl;
                      }    
                     }    
                     break;
                     
                     default:
                         break;
                  }    
                                                
              }
                                                                                                               
              for (i = 0; i < X; i++)
              {
                  for (j = 0; j < Y; j++)
                  {
                      cout << Grid[i][j] << " ";
                  }
                  cout << endl;
                                             
              }    
              cout << endl;    
             // cout << "Grid Sequence: " << Pop[index][j] << endl;  
               
            for (i = 0; i < X; i++) 
            {      
               for ( j = 0; j < Y; j++)
                 {
                   Grid[i][j] = 8;
                   cout << Grid[i][j] << " " ;
                 }
                   cout << endl;
            
            }   
              cout << "Grid Cleared" << endl;
              cin.get();
       }    
       
    }
    
    
    int main ()
    
    {
        srand ( time(NULL) );
        string Input;
        int Output[10];
        int Pop[PopSize][AAlgh];
        int Grid[X][Y];
        
        
        
        GetUserInput(Input);
        GetSetupString(Input, Output);
        GetCreateGrid(Grid);
        GetFillGrid(Output, Pop, Grid);
        cout << "all functions done" << endl;
        cin.get();
                      
        
    }
    I'am sure there are many newb mistakes and better ways to do what i've done, but i only started really getting into c++ like 5-7 days ago so bear with me thanks.

    Regards Wolfe

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Quote Originally Posted by Cdrwolfe
    Weird i know i definately read about adding them so i did, but yes your right they are not needed, now i've got to find out where i read it

    Thanks..

    What you probably read is that to declare a pointer to an array you will in fact need to wrap the name of the array inside parenthesis because of the operator precedence rules.

    Code:
    int (*myarray)[5];  // declares a pointer to an array of 5 ints
    int *myarray[5];  // declares an array of 5 pointers to int
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Thanks Mario.

    Regards Wolfe

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Quick question?

    does this code do what iu think it does?

    Code:
     if (Grid[x-1][y] == 1) 
                       {
                           Pop[index][11] = ++fitness;
                       }
    If shall we say [x] = 5, and [y] = 5, fitness = 0.

    Then does ( Grid[x-1][y] == 1 ) equate to:

    If Grid [4][5] contains a '1' then Pop[index][11] = Pop[index][1].

    Is this right or wrong so far it just doesn't work as expected so i thought someone may know why.

    Regards Wolfe

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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