Advertisement
Syndragonic

Lab 10 CS311

Nov 24th, 2019
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. /*Funtion template to find val in a partially filled array*/
  6. template<typename T>
  7. int findVal(T arr[], int n, T val)
  8. {
  9.     for(int i = 0; i < n; i++)
  10.         if(arr[i] == val)//Value found
  11.             return i;//Return index of value
  12.     return -1;//If not found, then return -1
  13. }
  14.  
  15. int main()
  16. {
  17.     int arr[20] = {1, 8, 4, 9, 3, 7, 5, 6, 20, 40};//Partially filled array
  18.     int n = 10;//Size of array
  19.     int val = 5;//Value to find
  20.     int index = findVal(arr, n, val);
  21.     if(index == -1)
  22.         cout<<val<<" not found in partially filled array\n";
  23.     else
  24.         cout<<val<<" found at index "<<index<<"\n";
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement