Algorithm
Problem Name: 30 days of code -
Objective
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
The locked Solution class in your editor will pass different types of arrays to your printArray function.
Constraints
- You must have exactly 1 function named printArray.
Output Format
Your printArray function should print each element of its generic array parameter on a new line.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <vector>
#include <string>
using namespace std;
/**
* Name: printArray
* Print each element of the generic vector on a new line. Do not return anything.
* @param A generic vector
**/
// Write your code here
template < typename Element>
void printArray(vector<Element> arr) {
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << endl;
}
int main() {
int n;
cin >> n;
vector<int> int_vector(n);
for (int i = 0; i < n; i++) {
int value;
cin >> value;
int_vector[i] = value;
}
cin >> n;
vector < string> string_vector(n);
for (int i = 0; i < n; i++) {
string value;
cin >> value;
string_vector[i] = value;
}
printArray < int>(int_vector);
printArray(string_vector);
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with C# Programming
Code -
C# Programming
using System;
class Printer
{
/**
* Name: PrintArray
* Print each element of the generic array on a new line. Do not return anything.
* @param A generic array
**/
// Write your code here
private static void PrintArray < T>(T[] items)
{
foreach(T item in items)
{
Console.WriteLine(item);
}
}
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int[] intArray = new int[n];
for (int i = 0; i < n; i++)
{
intArray[i] = Convert.ToInt32(Console.ReadLine());
}
n = Convert.ToInt32(Console.ReadLine());
string[] stringArray = new string[n];
for (int i = 0; i < n; i++)
{
stringArray[i] = Console.ReadLine();
}
PrintArray < Int32>(intArray);
PrintArray(stringArray);
}
}
Copy The Code &
Try With Live Editor
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.*;
import java.util.*;
public class Solution {
private static < Element> void printArray(Element[] array) {
for (Element element : array) {
System.out.println(element);
}
}
public static void main(String args[]) {
Integer[] intArray = {1, 2, 3};
String[] stringArray = {"Hello", "World"};
printArray(intArray);
printArray(stringArray);
if (Solution.class.getDeclaredMethods().length > 2) {
System.out.println("You should only have 1 method named printArray.");
}
}
}
Copy The Code &
Try With Live Editor