Ask Question
27 June, 11:42

Given: an int variable k, an int array current Members that has been declared and initialized, an int variable memberID that has been initialized, and a boolean variable isAMember. Write code that assigns true to isAMember if the value of memberID can be found in current Members, and that assigns false to isAMember otherwise. Use only k, currentMembers, memberID, and isAMember.

+5
Answers (1)
  1. 27 June, 13:03
    0
    The c+ + program is given below. Nothing is displayed as per the question.

    #include

    using namespace std;

    int main () {

    / / declaration and initialization of integer variables

    int k, memberID = 12, nMembers=5;

    bool isAMember;

    / / declaration and initialization of integer array

    int currentMembers[] = {12, 34, 56, 78, 90};

    for (k=0; k
    {

    if (memberID = = currentMembers[k])

    {

    / / when member is found in the array, the loop is exited using break

    isAMember = true;

    break;

    }

    else

    isAMember = false;

    }

    return 0;

    }

    Explanation:

    The program begins with declaration and initialization of integer variables and followed by initialization of the array holding the id of all the members.

    The Boolean variable is declared but not initialized.

    int k, memberID = 12, nMembers=5;

    bool isAMember;

    int currentMembers[] = {12, 34, 56, 78, 90};

    After this, the array holding the id of the members is searched for the given member id. This is done using a for loop and a if else statement inside the loop.

    If the member id is present in the array, the variable isAMember is initialized to true otherwise it is assigned false.

    When the variable isAMember is initialized to true, the break statement is used to exit from the loop.

    for (k=0; k
    {

    if (memberID = = currentMembers[k])

    {

    isAMember = true;

    break;

    }

    else

    isAMember = false;

    }

    The break is used since other values of id in the array will not match the given member id and the variable, isAMember will be initialized to false even if the given member id is present in the array. Hence, it is mandatory to exit the loop once the given member id is found in the array.

    This program can be tested for different values of the id of the members and different sizes of the array.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Given: an int variable k, an int array current Members that has been declared and initialized, an int variable memberID that has been ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers