Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.(Maximum and minimum ) Implement the following functions that find the maximum

ID: 3849214 • Letter: 1

Question

1.(Maximum and minimum ) Implement the following functions that find the maximum and

minimum elements in a first-class container:

template<typename ElementType, typename ContainerType>

ElementType maxElement(ContainerType& container){

}

template<typename ElementType, typename ContainerType>

ElementType minElement(ContainerType& container){

}

2. (Occurrence of a value ) Implement the following function that finds the number of occurrences of

a specified value in a first-class container:

template<typename ElementType, typename ContainerType>

int countElement(ContainerType& container, const ElementType& value){

}

3. Implement the reverse and reverse_copy functions.

template<typename BidirectionalIterator>

void reverse(BidirectionalIterator beg,BidirectionalIterator end){

}

template<typename BidirectionalIterator, typename OutputIterator>

OutputIterator reverse_copy(BidirectionalIterator beg,BidirectionalIterator end, OutputIterator targetPosition){

}

4.  Implement the replace and replace_if functions.

template<typename ForwardIterator, typename T>

void replace(ForwardIterator beg, ForwardIterator end,const T& oldValue, const T& newValue){

}

template<typename ForwardIterator, typename boolFunction, typename T>

void replace_if(ForwardIterator beg, ForwardIterator end,boolFunction f, const T& newValue){

}

Explanation / Answer

1)

template<typename ElementType, typename ContainerType>
ElementType maxElement(ContainerType& container){
ElementType val = 999;
   for (std::vector<ElementType >::iterator it = ContainerType.begin() ; it != ContainerType.end(); ++it){
       if(*it > val) {
           val = *it;
       }
   }
return val;
}

template<typename ElementType, typename ContainerType>
ElementType minElement(ContainerType& container){
   ElementType val = 999;
   for (std::vector<ElementType >::iterator it = ContainerType.begin() ; it != ContainerType.end(); ++it){
       if(*it < val) {
           val = *it;
       }
   }
return val;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------

2)

template<typename ElementType, typename ContainerType>
int countElement(ContainerType& container, const ElementType& value){
   int count = 0;
   for (std::vector<ElementType >::iterator it = ContainerType.begin() ; it != ContainerType.end(); ++it){
       if(*it == value) {
           count++;
       }
   }
   return count;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

3)

template<typename BidirectionalIterator>
void reverse(BidirectionalIterator beg,BidirectionalIterator end){
   while ((beg!=end)&&(beg!=--end)) {
       std::iter_swap (beg,end);
       ++beg;
   }
}

template<typename BidirectionalIterator, typename OutputIterator>
OutputIterator reverse_copy(BidirectionalIterator beg,BidirectionalIterator end, OutputIterator targetPosition){
   while ((beg!=end)&&(beg!=--end)) {
       std::iter_swap (beg,end);
       ++beg;
   }
   targetPosition = end;
   return targetPosition;
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

4)

template<typename ForwardIterator, typename T>
void replace(ForwardIterator beg, ForwardIterator end,const T& oldValue, const T& newValue){
   while ((beg!=end)&&(beg!=--end)) {
   if(beg == oldValue) {
           beg = newValue;
       }
       ++beg;
   }
}
template<typename ForwardIterator, typename boolFunction, typename T>
void replace_if(ForwardIterator beg, ForwardIterator end,boolFunction f, const T& newValue){
   while ((beg!=end)&&(beg!=--end)) {
   if(beg == oldValue) {
           beg = newValue;
           f = true;
       }
       ++beg;
   }
}