<\/span><\/h2>\n\n\n\nToday we’re discussing Generics; be aware that not all languages support this construct<\/em>, so fewer languages are enabled for this challenge.<\/p>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nWrite a single generic function named printArray<\/em>; this function must take an array of generic elements as a parameter (the exception to this is C++, which takes a vector<\/em>). The locked Solution<\/em> class in your editor tests your function.<\/p>\n\n\n\nNote:<\/strong> You must use generics to solve this challenge. Do not<\/em> write overloaded functions.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe locked Solution<\/em> class in your editor will pass different types of arrays to your printArray<\/em> function.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- You must have exactly 1<\/strong> function named printArray<\/em>.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nYour printArray<\/em> function should print each element of its generic array parameter on a new line.<\/p>\n\n\n\n<\/span>Solution – Day 21: Generics<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <iostream>\n#include <vector>\n#include <string>\n\nusing namespace std;\n\n\/\/ Write your code here\ntemplate <class T> \n void printArray(vector<T> i) \n { \n for(int j=0;j<i.size();j++) \n cout<<i[j]<<endl;\n } \nint main() {\n\tint n;\n\t\n\tcin >> n;\n\tvector<int> int_vector(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tint value;\n\t\tcin >> value;\n\t\tint_vector[i] = value;\n\t}\n\t\n\tcin >> n;\n\tvector<string> string_vector(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tstring value;\n\t\tcin >> value;\n\t\tstring_vector[i] = value;\n\t}\n\n\tprintArray<int>(int_vector);\n\tprintArray<string>(string_vector);\n\n\treturn 0;\n}<\/pre>\n\n\n\n