Post Test
Tentang Array 1 Dimensi, program ini mencakup sort bubble(mengurutkan array) dan menghapus sebuah array.
Silahkan dinikmati gan!!
#include <cstdlib> |
#include <iostream> |
#define maks 5 |
using namespace std; |
class Array1D{ |
friend ostream& operator<<(ostream&, const Array1D&); |
friend istream& operator>>(istream&, Array1D&); |
public : |
Array1D(); |
void cetak(); |
void geser_kiri(); |
void geser_kanan(); |
void sort(); |
void hapus_elemen(); |
private : |
char A[maks]; |
}; |
Array1D::Array1D(){ |
for(int i=0;i<maks;i++) |
A[i]= '0'; |
} |
void Array1D::cetak(){ |
for(int i=0;i<maks;i++) |
cout<<A[i]<<""; |
} |
ostream& operator<<(ostream& out, const Array1D& x){ |
for(int i=0;i<maks;i++) |
cout<<x.A[i]<<""; |
cout<<endl; |
return out; |
} |
istream& operator>>(istream& in, Array1D& x){ |
int posisi, i; |
cout<<"masukkan 5 elemen array :"<<endl; |
for(int i=0;i<maks;i++){ |
cout<<"elemenke ["<<i+1<<"] :"; |
in>>x.A[i]; |
} |
return in; |
} |
void Array1D::sort(){ |
int a, b, t; |
for(a=1; a < maks; a++) |
for(b=maks-1; b >= a; b--) |
if(A[b-1] > A[b]) { |
t = A[b-1]; |
A[b-1] = A[b]; |
A[b] = t; |
} |
} |
void Array1D::geser_kanan(){ |
int n=maks; |
int temp=A[n-1]; |
for(int i=n-1;i>=0;i--) |
A[i+1]=A[i]; |
A[0]=temp; |
} |
void Array1D::geser_kiri(){ |
int n; |
int temp=A[0]; |
for(int i=0;i<n;i++) |
A[i]=A[i+1]; |
A[n-1]=temp; |
} |
void Array1D::hapus_elemen(){ |
int z; |
cout<<"hapus indek ke : "; |
cin>>z; |
if(z>0 && z<=5) |
A[z-1]='O'; |
else cout<<"diluar range\n"; |
} |
int main(int argc, char *argv[]) |
{ |
Array1D x; |
cout<<"Array masih kosong :"<<x; |
cin>>x; |
cout<<"Isikan Array saat ini :"<<x; |
x.geser_kiri(); |
cout<<"Isikan Array setelah digeser ke kiri :"<<x; |
x.geser_kanan(); |
cout<<"Isi Array setelah digeser ke kanan :"<<x; |
x.sort(); |
cout<<"Isi Array setelah diurutkan :"<<x; |
x.hapus_elemen(); |
cout<<"Array setelah dihapus : "<<x; |
system("PAUSE"); |
return EXIT_SUCCESS; |
Tidak ada komentar:
Posting Komentar