PPT MINGGU KE 7 ARAY SATU DIMENSI
CONTOH PROGRAMNYA :
#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<<">";
in>>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<<out<<"Rata-rata dari "<<a.n<<" bilangan adalah : "<<a.rata()<<endl;
return out;
}
main()
{
HitungRata rata;
cin>>rata;
cout<<rata;
return 0;
}
SELAMAT MENCOBA YA SEMOGA BERMAN PAAT 😉 BY MUHAMAD FIRMANSYAH
0 komentar:
Posting Komentar