#include <stdio>
#include <vector>
class CPerson
{
public:
CPerson( float old, float height, float weight )
: mOld( old ), mHeight( height ), mWeight( weight )
{}
float getOld( void ) { return mOld; }
float getHeight( void ) { return mHeight; }
float getWeight( void ) { return mWeight; }
private:
float mOld, mHeight, mWeight;
};
#define PERSON_OLD 0
#define PERSON_HEIGHT 1
#define PERSON_WEIGHT 2
typedef unsigned int uint;
float* average_persons( std::vector<CPerson> &vec )
{
float result[3] = { 0 };
for( uint n = 0; n < vec.size(); n++ )
{
result[PERSON_OLD] += vec[n].getOld();
result[PERSON_HEIGHT] += vec[n].getHeight();
result[PERSON_WEIGHT] += vec[n].getWeight();
}
for( uint n = 0; n < 3; n++ )
result[n] /= (float)vec.size();
return result;
}
int main()
{
std::vector<CPerson> persons;
persons.push_back( CPerson( 1, 2, 3 ) );
persons.push_back( CPerson( 2, 3, 4 ) );
float *res = average_persons( persons );
printf( "Averages:\nOlds: %f\nHeights: %f\nWeights: %f\n",
res[PERSON_OLD], res[PERSON_HEIGHT], res[PERSON_WEIGHT] );
getchar();
return 0;
}
Proste zadanie, wystarczy wiedzieć JAK się oblicza średnią :)
Btw. Osoby dodaje się po przez: 'persons.push_back( CPerson( x, y, z ) );'