Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array with boolean values

Status
Not open for further replies.

maximusAF

IS-IT--Management
Feb 11, 2003
6
0
0
US
Can someone advise on how to create an array with bool values? How you tell each element of bool array if record exist or not? Keep simple as possible.
 
Code:
bool myBArray[10];
memset(&myBArray, false, sizeof(bool) * 10); // set all false

for(int n=0;n<10;n++){
  if( .... some condition)
    myBArray[n] = true;
  else
    myBArray[n] = false;
}

does that help?
-pete
 
The numbered elements in the array correspond to other values. I need check each element to see if been used or not. For examp. each pass thru a number is called, when a number is called, i need to tell the array true for that array element. If prog is going to call a value of 14, first of all, i need to check array[13] to see if it is true or false. If it is false(meaning has not been used), i can use 14. I would then set array[13] to true. The next number called is 45, i would check array[44] to see if true or false, if false, i can use 45 and then set array[44] to true. If next number called is 14, i check array[13] again and see that it is true, so number 14 can't be used again, so i must go to next number.

Hope make sense for you
 
Code:
const int maxsize=100;
int tempi;

bool barray=new bool[maxsize];
for (tempi=0;tempi<maxsize;tempi++) barray[tempi]=false;//not as effiecient as memset


while(notdone)
{
tempi=getnextInt();
if (tempi>=maxsize) throw &quot;Out of bounds&quot;; //alternativly
//tempi%=maxsize;
if (!barray[tempi])
{
//do stuff
barray[tempi]=true;
}
//optionally
//else
//{
//do other stuff
//}
}
an alternative bounds safe approach would be to use a container class (say a list or a btree) to hold all of the ones that you have used. Instead of checking that element in the array you would check to see if that element was in the set. The drawback to this is a minor speed hit. It is probabally worth it to be able to handle data without bounds.
that approach would look something like

int tempi;
list intlist;
while(condition)
{
tempi=getnextInt();
if (intlist.find(tempi)==-1) //notfound
{
//do stuff
intlist.add(tempi);
}
else
{
//do other stuff
}
}






} WR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top