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 , andan int variable isAMember, write code that assigns 1 to isAMember if the value of memberID can be found in currentMembers, and that assigns 0 to isAMember otherwise. Use only k, currentMembers, nMembers, and isAMember.

Respuesta :

Answer:

The c++ program is given below. Nothing is displayed as per the question.

#include <iostream>

using namespace std;

int main() {    

   // declaration and initialization of integer variables

   int k, memberID = 12, nMembers=5, isAMember;    

   // declaration and initialization of integer array

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

   for(k=0; k<nMembers; k++)

   {

       if(memberID == currentMembers[k])

       {

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

           isAMember = 1;

           break;

       }

       else

           isAMember = 0;

   }

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.

int k, memberID = 12, nMembers=5, 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 1 otherwise it is assigned 0.

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

for(k=0; k<nMembers; k++)

   {

       if(memberID == currentMembers[k])

       {

           isAMember = 1;

           break;

       }

       else

           isAMember = 0;

 }

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 assigned value 0 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.

Also, the value of the variable isAMember is not displayed since it is not mentioned in the question.

This program can be tested for different values of the id of the members and different sizes of the array.