assalamualaikum wr. wb
c++ :
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
class HitungRATA {
friend ostream& operator<<(ostream&, HitungRATA&);
friend istream& operator>>(istream&, HitungRATA&);
public:
float rata();
private:
int n;
int A[20];
};
istream& operator>>(istream& in, HitungRATA& a) {
cout << "Banyaknya data : ";
in >> a.n;
for (int i = 0; i < a.n; i++) {
cout << "Masukkan data ke- : " << i+1 << " > ";
cin >> a.A[i];
}
return in;
}
float HitungRATA::rata() {
float total=0;
for (int i = 0; i<n; i++) total = total + A[i];
return(total/n);
}
ostream& operator<<(ostream& out, HitungRATA& a) {
cout << "Rata-rata dari " << a.n;
cout << " bilangan adalah : "<< a.rata();
return out;
}
main() {
HitungRATA run;
cin >> run;
cout << run;
return 0;
}
0 komentar:
Posting Komentar