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
#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;
}
Comments
Leave a comment