Original Post
After reading the STL and newbies I realized I should get on it. So I just got home, and after about 20 mins total I created this: I had a little trouble with clearing out the type-casting warnings (conversion from 'x' to 'y' possible loss of data), but it works now. I went to the DADS site linked in that prevoius thread, and that is where I got the idea to do the mean. I still want to make templated functions for median, midrange, and mode algorithms too. All-in-all, its a great thread to force you into sucking it up and learning STL. Not a lot of resources came up easily with google, so here is one that helped me with the std::vector stuff. Comments welcome, I just wanted to share my learning experience.
// testie2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
template <typename T>
T Mean( vector<T> t )
{
// set result to 0
T re = 0;
// cast and set the size
int sz = (int)t.size();
// loop and sum
for(size_t i = 0; i < t.size(); ++i)
re += t;
// return the sum/size
return (T)(re / sz);
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> V;
vector<float> F;
// seed the generator
srand( (unsigned)time(NULL));
for(int i = 0; i < 100; ++i)
{
// generate a random number
int rnd = rand() % 1000;
// insert the number
V.insert(V.begin(), rnd);
F.insert(F.begin(), (float)rnd);
}
// calculate the mean of the numbers
int res = Mean<int>(V);
float re2 = Mean<float>(F);
// display results
cout << res << endl;
cout << re2 << endl;
system("pause");
return 0;
}