How to write generic print function arrays of numbers in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

template <class BBL> void print(BBL *arr, int size)
{
	for (int i = 0; i < size; i++)
		cout << arr[i] << ' ';
	cout << endl;
}

int main()
{
	int arrint[] = { 7, 3, 9, 2, 1, 8, 1, 5, 4 };

	print(arrint, 9);

	double arrdouble[] = { 3.14, 1.11, 1.21, 5.67, 3.98 };

	print(arrdouble, 5);

	return 0;
}


/*
run:

7 3 9 2 1 8 1 5 4
3.14 1.11 1.21 5.67 3.98

*/

 



answered Aug 1, 2018 by avibootz
...