prak4

#include <iostream>
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 hapus_elemen();

private:
char A[5];
int posisi;
};

Array1D::Array1D(){
for(int i=0;i<5;i++)
A[i]=’O’;
}

void Array1D::cetak(){
for(int i=0;i<5;i++)
cout<<A[i]<<” “;
}

ostream& operator<<(ostream& out, const Array1D& x){
for(int i=0;i<5;i++)
out<<x.A[i]<<” “;
out<<endl;
return out;
}

istream& operator>>(istream& in, Array1D& x){
int posisi;
for (int posisi=1; posisi<=5; posisi++){
cout<<“masukkan nilai array posisi ke- : “;
in>>x.posisi;
if(posisi >= 0 && posisi <= 5){cout<<“masukkan elemen arraynya :”;
in>>x.A[posisi-1];
}
}
return in;
}

void Array1D::geser_kanan(){
int n=5;
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=5;
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 posisi;
cout<<“Pilih indeks berapa yg akan di hapus : “;
cin>>posisi;
if(posisi>0 && posisi<=5)
A[posisi-1]=’O’;
else cout<<“indeks yg anda masukan salah karena indek hanya terdiri dari 1 – 5\n”;
}

int main()
{
Array1D x;
cout<<“Array masih kosong : “<<x;
cin>>x;

cout<<“Isi Array saat ini : “<<x;
x.geser_kiri();
cout<<“Isi Array setelah di geser kiri : “<<x;
x.geser_kanan();
cout<<“Isi Array setelah di geser kanan : “<<x;
cout<<“Urutan elemen pada indeksnya saat ini : “<<x;
x.hapus_elemen();
cout<<“Setelah dihapus menjadi : “<<x;

system(“PAUSE”);
system(“cls”);
return 0;
}


This entry was posted in praktikum sdata. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s