- Back to Home »
- ALPRO »
- Menentukan Nilai maksimal dan Minimal dengan C++ (ALPRO)
#include <iostream.h>
#include <conio.h>
#include <cstdlib>
#include <iostream>
using namespace std;
class aku{
friend istream& operator >> (istream&, aku&);
friend ostream& operator << (ostream&, aku&);
public :
void maks();
private :
int n, max, min, total;
int A[20];
};
istream& operator >> (istream& in,aku& a){
a.total=0;
cout<<"Banyaknya data : "; in>>a.n;
for(int i=0;i<a.n;i++){
cout<<"Data ke- : " <<i+1 <<" > "; in>>a.A[i];
a.total=a.total+a.A[i];
}
}
void aku::maks(){
max=min=A[0];
for(int i=1;i<n;i++){
if(A[i] > max) max=A[i];
if(A[i] < min) min=A[i];
}
}
ostream& operator << (ostream& out, aku& b){
b.maks();
out<<endl<<"Nilai terbesar : " <<b.max <<endl;
out<<"Nilai terkecil : " <<b.min <<endl;
out<<endl<<"Total jumlah dari semua bilangan tersebut adalah : " <<b.total;
}
int main(int argc, char *argv[])
{
aku x;
cin>>x;
cout<<x;
cout<<endl <<endl;
system("PAUSE");
return EXIT_SUCCESS;
}