C언어 예제5 동적배열
너에게제공
518 0 0
#include <iostream>
using namespace std;
class Numbers{
int size;
int *num;
float avg;
public :
Numbers();
void calcAvg();
void prnNumber();
void SelSort();
~Numbers() { delete num; }
};
Numbers::Numbers()
{
cout << "몇 개의 숫자를 집어넣을건가요 ㅎㅎ? ";
cin >> size;
num = new int[size];
cout << "정수를 입력하시오." << endl;
for(int i=0; i<size; i++)
{
cin >> num[i];
}
}
void Numbers::calcAvg()
{
int sum=0;
for(int i=0; i<size; i++)
{
sum=sum+num[i];
}
avg = (float)sum/size;
}
void Numbers::prnNumber()
{
cout << "입력한 숫자 : " ;
for(int i=0; i<size; i++)
{
cout << num[i] << "t";
}
cout << endl << "평균 : " << avg << endl;
cout << "평균보다 큰 숫자 : " ;
for(int k=0; k<size; k++)
{
if(avg<num[k])
{
cout << num[k] << "t";
}
}
cout << endl;
}
void main()
{
Numbers n;
n.calcAvg();
n.prnNumber();
}
using namespace std;
class Numbers{
int size;
int *num;
float avg;
public :
Numbers();
void calcAvg();
void prnNumber();
void SelSort();
~Numbers() { delete num; }
};
Numbers::Numbers()
{
cout << "몇 개의 숫자를 집어넣을건가요 ㅎㅎ? ";
cin >> size;
num = new int[size];
cout << "정수를 입력하시오." << endl;
for(int i=0; i<size; i++)
{
cin >> num[i];
}
}
void Numbers::calcAvg()
{
int sum=0;
for(int i=0; i<size; i++)
{
sum=sum+num[i];
}
avg = (float)sum/size;
}
void Numbers::prnNumber()
{
cout << "입력한 숫자 : " ;
for(int i=0; i<size; i++)
{
cout << num[i] << "t";
}
cout << endl << "평균 : " << avg << endl;
cout << "평균보다 큰 숫자 : " ;
for(int k=0; k<size; k++)
{
if(avg<num[k])
{
cout << num[k] << "t";
}
}
cout << endl;
}
void main()
{
Numbers n;
n.calcAvg();
n.prnNumber();
}