Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- /*Funtion template to find val in a partially filled array*/
- template<typename T>
- int findVal(T arr[], int n, T val)
- {
- for(int i = 0; i < n; i++)
- if(arr[i] == val)//Value found
- return i;//Return index of value
- return -1;//If not found, then return -1
- }
- int main()
- {
- int arr[20] = {1, 8, 4, 9, 3, 7, 5, 6, 20, 40};//Partially filled array
- int n = 10;//Size of array
- int val = 5;//Value to find
- int index = findVal(arr, n, val);
- if(index == -1)
- cout<<val<<" not found in partially filled array\n";
- else
- cout<<val<<" found at index "<<index<<"\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement