Tuesday, July 12, 2016

Hacker Rank 30 days of code Day 21: Generics

The problem

bjective 
Today we're discussing Generics; be aware that not all languages support this construct, so fewer languages are enabled for this challenge. Check out the Tutorial tab for learning materials and an instructional video!
Task 
Write a single generic function named printArray; this function must take an array of generic elements as a parameter (the exception to this is C++, which takes a vector). The locked Solution class in your editor tests your function.
Note: You must use generics to solve this challenge. Do not write overloaded functions.
Input Format
There is no input for this challenge. The locked Solution class in your editor will pass two different types of arrays to your printArray function.
Constraints
  • You must have exactly  function named printArray.
Output Format
Your printArray function should print each element of its generic array parameter on a new line.

My Submission

 // Add your code here  
 template <typename Type>  
   void printArray(vector<Type> input){  
   int len = input.size();  
   for (int i = 0; i<len; i++) cout<<input[i]<<endl;  
 }  

1 comment:

  1. I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. Brand

    ReplyDelete