Answer to Question #185646 in C++ for P. Gokul

Question #185646

The list of random numbers stored in an integer and float array. Perform search operation to find the search number is present or not from an array using templates concept.

* Print - Element Found or Element Not Found


1
Expert's answer
2021-04-26T05:41:13-0400
#include <iostream>


using namespace std;


//define a templae function
template <class T>
T num_present(T num[], T n)
{
    int count=0;
    for (int i=0;i<10;i++)
    {
      if (num[i]==n){
         count++; 
      }
        
    }
    if (count==0){
        cout<<"Element Not Found";
    }
    else{
        cout<<"Element Found";
    }
}


int main()
{
    //define the size of the arrays
    int n;
    int num1[10]={3,5,34,2,1,34,7,87,9,3};
    float num2[10]={7.5,7.2,2.3,1.1,3.2,2.2,9.7,4.5,6.7,1.2};
    
    
    
    //get integer input
    cout << "Enter an number: "<< endl;
    cin>>n;
    
    //check if the input is a float or an integer
    char ch;
    if(ch=='.')
    {
        num_present(num2,float(n));
        
    }
    else
    {
         num_present(num1,n);
    }
    
    


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS