I have a code, and this is another problem, that's not working;

Given:
an int variable k ,
an int array currentMembers that has been declared and initialized,
an int variable nMembers that contains the number of elements in the array,
an int variable memberID that has been initialized, and
a bool variable isAMember ,

Write code that assigns true to isAMember if the value of memberID can be found in currentMembers , and that assigns false to isAMember otherwise.
Use only k , currentMembers , nMembers , and isAMember .

my code:
main() {
int currentMembers[]={1,2,3},nMembers=3,k,memberID=2;
bool isAMember;
do
{
if (memberID == currentMembers[nMembers]){
isAMember = 1;
}
}while (memberID != currentMembers[nMembers]);
}

Your declarations or allmost right. I changed main to int main() (that's the way it's allways suppose to be). I've also given each var a initial value.

int main() { 
int currentMembers[]={1,2,3},nMembers = 3, k =  0, memberID = 2; 
bool isAMember = FALSE;

I see you declare nMembers as 3, but every time you change the size of your array you'll have to change nMembers too. It's a better idea to use nMembers = sizeof(currenMembers)/sizeof(int); Next you need to find loop trough the array to look for memberID. So you could use a for loop.

for (k = 0; k <= nMembers; k++)
{
    if (currentMembers[k] == memberID)
        isAMember = TRUE; 
}

And that's it.

Edited 6 Years Ago by Nick Evan: n/a

This question has already been answered. Start a new discussion instead.