Advertisement
Shishu

generic search

Aug 11th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <class Data>
  6. int Search(Data *x, Data target, int n){
  7.     int i;
  8.     for(i = 0; i < n; i++){
  9.         if(x[i] == target){
  10.             return i;
  11.         }
  12.     }
  13.     return -1;
  14. }
  15.  
  16. int main(){
  17.     int age[5] = {5, 9 , 4, 7, 3};
  18.     double cgpa[5] = {3.5, 1.9, 2.9, 3.9, 2.5};
  19.     int ans;
  20.     ans = Search(cgpa, 2.5, 5);
  21.     cout << ans << endl;
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement