Write a template function that returns the largest value and

Write a template function that returns the largest value and the smallest value(use call-by reference) stored in a vector, the function should also returns a new list which stores all of the values between two immediately adjacent values in the original vector. Assume the operator

Solution

#include<iostream>
#include<vector>

//change Type to type of vector you want , if you want float type just change int to float
#define Type int
using namespace std;

template<typename type>
void largeAndSmall(std::vector<type> &a,type &tmin, type &tmax)
{
   tmax = a[0] , tmin = a[0];
   for ( int i = 0 ; i < a.size() ; i++ )
   {
       if( a[i] < tmin )
           tmin = a[i] ;
       if( a[i] > tmax )
           tmax = a[i] ;
   }
}

int main()
{
   std::vector <Type> int_arr;

   int_arr.push_back(3);
   int_arr.push_back(0);
   int_arr.push_back(9);
   int_arr.push_back(-8);
   int_arr.push_back(-1);

   cout<<\"Elements of vector are: \";
   for ( int i = 0 ; i < int_arr.size() ; i++ )
   {
       cout<<int_arr[i]<<\"\\t\";
   }
   cout<<endl;
   //print largest and smallest element in a vector
   Type max,min;
   largeAndSmall(int_arr,min,max);
   cout<<\"Max = \"<<max<<\"\\t\"<<\"Min = \"<<min<<endl;
}

 Write a template function that returns the largest value and the smallest value(use call-by reference) stored in a vector, the function should also returns a n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site