write a program that initializes an integerarray of 8 elements representing the ID numbers of 8students, then inputs a value for an ID to be searched for inthe array output the position of that ID in the array if found,or asuitable message if not.

Respuesta :

Answer:

#include <iostream>

using namespace std;

int main() {

   int student_id[8]={5,76,23,56,34,67,12,455};

   int i,id_s;//i iterator,id_s for search.

   cout<<"Enter the id to be searched"<<endl;

   cin>>id_s;//taking input of the id to be searched..

   bool b=false;

   for(i=0;i<8;i++)

   {

       if(student_id[i]==id_s)

       {

           cout<<"Position of the id is "<<i<<endl;//printing the position if found..

           b=true;

           break;

       }

   }

   if(b==false)//if element is not present printing the message..

   cout<<"The student does not exists"<<endl;

return 0;

}

Output:

Enter the id to be searched

76

Position of the id is 1

Enter the id to be searched

87

The student does not exists

Explanation:

I have taken a array of size 8 for eight students.Iterating over the array if the  id is found then printing the position if not found then printing the message.

ACCESS MORE