C++/Generic/Generic Algorithm
A generic bubble sort.
#include <iostream>
using namespace std;
template <class X> void bubble(X *data, int size)
{
register int a, b;
X t;
for(a=1; a < size; a++)
for(b=size-1; b >= a; b--)
if(data[b-1] > data[b]) {
t = data[b-1];
data[b-1] = data[b];
data[b] = t;
}
}
int main()
{
int i[] = {3, 2, 5, 6, 1, 8, 9, 3, 6, 9};
double d[] = {1.2, 5.5, 2.2, 3.3};
int j;
bubble(i, 10); // sort ints
bubble(d, 4); // sort doubles
for(j=0; j<10; j++)
cout << i[j] << " ";
cout << endl;
for(j=0; j<4; j++)
cout << d[j] << " ";
cout << endl;
return 0;
}
Generic sum method
#include <iostream>
using namespace std;
template <class X> X sum(X *data, int size)
{
int i;
X result = 0;
for(i = 0; i <size; i++) result += data[ i ];
return result;
}
int main()
{
int i[] = {1, 2, 3, 4};
double d[] = {1.1, 2.2, 3.3, 4.4};
cout << sum(i, 4) << endl;
cout << sum(d, 4) << endl;
return 0;
}