# Or operator?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-13-2006
Cdrwolfe
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
• 07-13-2006
twomers
>> 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.
• 07-13-2006
Cdrwolfe
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
• 07-13-2006
twomers
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.
• 07-13-2006
anon
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.
• 07-13-2006
Cdrwolfe

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 :D

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();

}

}

}

• 07-13-2006
twomers
>> (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.
• 07-13-2006
Cdrwolfe
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.
• 07-13-2006
twomers
Try taking them out, and see if it works. And post the answer here.
• 07-13-2006
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..
• 07-13-2006
SlyMaelstrom
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.
• 07-13-2006
Cdrwolfe
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
• 07-13-2006
Mario F.
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

• 07-13-2006
Cdrwolfe
Thanks Mario.

Regards Wolfe
• 07-14-2006
Cdrwolfe
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
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last