Respuesta :

Answer:

Following are the program in C++ language  

#include <iostream> // header file

using namespace std; // namespace

int search(int s[],int s1,int n) // function search  

{

   for (int k= 0; k<n; k++) // iterating over the loop

 {

   if (s[k] == s1)   // searching the element  

   {

   return(k); // return the index

     break;

   }

 

}

return(-1); // return(-1)

}

int main() // main function

{

int arr[100],n1,i,s; // variable declaration  

cout<<"Enter number you want in the array:";

cin>>n1;

cout<<"Enter the elements in the array\n ";

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

{

   cin>>arr[i]; // taking input into the array  

}

 cout<<"Enter a number you want to search: ";

cin>>s; // read the serach element by the user  

int res= search(arr,s,n1); // calling function search

cout<<res; // display result

 return 0;

}

Output:

Enter number you want in the array:3

Enter the elements in the array

1

2

34

Enter a number you want to search: 2

2

Explanation:

Following are the description of the program  

  • Read the number you want in array in the "n1" variable of int type.
  • Read the array by the user in the "arr ".
  • Read the searching element in the  "s" variable.
  • calling the function search by passing array arr, searching element "s" and "n1".
  • In the search function, it returns the index of its position in the array, if not found then it return -1.
  • Finally print the index.  

ACCESS MORE
EDU ACCESS