C언어 예제4 5명의 평균 구하기
너에게제공
488 0 0
#include<iostream>
using namespace std;
class PersonAge
{
private:
int age[5];
static int cnt, tot;
static float avg;
public:
PersonAge();
void calcAvg(){ avg = (float)tot/cnt; };
void print(){ cout << "총 " << cnt << "명의 평균 나이 : " << avg << endl; };
};
int PersonAge::cnt=0;
int PersonAge::tot=0;
float PersonAge::avg=0.0;
PersonAge::PersonAge()
{
for(int i=0; i<5; i++)
{
cout << "나이 : ";
cin >> age[i];
tot=tot+age[i];
cnt++;
}
}
void main()
{
PersonAge p;
p.calcAvg();
p.print();
}