kasus 6.4 diktat bab 7(mencari nilai min,max)

mencari nilai min,max c++
output :
 codingan nya :

#include <iostream>
#include <math.h>
using namespace std;

class HitungStatistik {
friend ostream& operator<<(ostream&, HitungStatistik&);
friend istream& operator>>(istream&, HitungStatistik&);
public:
void maks_min();

private:
int m1, m2;
int n;
int A[20];
};

istream& operator>>(istream& in, HitungStatistik& a)
{
cout << "Banyaknya data : ";
cin >> a.n;
for (int i = 0; i < a.n; i++)
{
cout << "Data ke- : " << i+1 << " > ";
cin >> a.A[i];
}
return in;
}

void HitungStatistik::maks_min()
{ m1 = m2 = A[0];
for (int i=1; i<n; i++)
{
if (A[i] > m1) m1 = A[i];
if (A[i] < m2) m2 = A[i];
}
}

ostream& operator<<(ostream& out, HitungStatistik& a) {
a.maks_min();
cout << "Nilai terbesar : " << a.m1 << endl;
cout << "Nilai terkecil : " << a.m2;
return out;
}

main() {
HitungStatistik run;
cin >> run;
cout << run;
return 0;
}


EmoticonEmoticon