Pertemuan Ke-6, Kasus 6.1
Hitung rata-rata dari n bilangan bulat dengan menggunakan array!
Algoritma :
Deklarasi : Integer i,jumlah;
Deskripsi : jumlah=0;
for i=1 to n do;
jumlah = jumlah +x[i]
end for;
rata = jumlah/n
Flowchart :
Program Dev C++ :
#include <iostream>
#include <cstdlib>
/* 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;
}
Sekian dan Terima Kasih, Semoga Bermanfaat.
Algoritma :
Deklarasi : Integer i,jumlah;
Deskripsi : jumlah=0;
for i=1 to n do;
jumlah = jumlah +x[i]
end for;
rata = jumlah/n
Flowchart :
Program Dev C++ :
#include <iostream>
#include <cstdlib>
/* 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;
}
Sekian dan Terima Kasih, Semoga Bermanfaat.
Comments