Pertemuan Ke-6, Kasus 6.3
Algoritma untuk menentukan nilai maksimum dari n bilangan.
Algoritma :
Deklarasi : integer i,temp;
deskripsi : temp=data[1];
for i=2 to n do;
temp=maksimum2(temp,data[i]);
maksimum=temp;
Flowchart :
Program Dev C++ :
#include <iostream>
#include <math.h>
using namespace std;
class HitungStatistik {
friend ostream& operator<<(ostream&, HitungStatistik&);
friend istream& operator>>(istream&, HitungStatistik&);
public:
int maksimum();
private:
int maksimum2(int, int);
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;
}
int HitungStatistik::maksimum2(int a, int b)
{ if (a>b) return(a);
else return(b);
}
int HitungStatistik::maksimum()
{ int temp = A[0];
for (int i = 1; i<n; i++)
temp = maksimum2(temp, A[i]);
return(temp);
}
ostream& operator<<(ostream& out, HitungStatistik& a) {
cout << "Nilai terbesar: " << a.maksimum();
return out;
}
main() {
HitungStatistik run;
cin >> run;
cout << run;
return 0;
}
Sekian dan Terima Kasih, Semoga Bermanfaat.
Algoritma :
Deklarasi : integer i,temp;
deskripsi : temp=data[1];
for i=2 to n do;
temp=maksimum2(temp,data[i]);
maksimum=temp;
Flowchart :
Program Dev C++ :
#include <iostream>
#include <math.h>
using namespace std;
class HitungStatistik {
friend ostream& operator<<(ostream&, HitungStatistik&);
friend istream& operator>>(istream&, HitungStatistik&);
public:
int maksimum();
private:
int maksimum2(int, int);
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;
}
int HitungStatistik::maksimum2(int a, int b)
{ if (a>b) return(a);
else return(b);
}
int HitungStatistik::maksimum()
{ int temp = A[0];
for (int i = 1; i<n; i++)
temp = maksimum2(temp, A[i]);
return(temp);
}
ostream& operator<<(ostream& out, HitungStatistik& a) {
cout << "Nilai terbesar: " << a.maksimum();
return out;
}
main() {
HitungStatistik run;
cin >> run;
cout << run;
return 0;
}
Sekian dan Terima Kasih, Semoga Bermanfaat.
Comments