Skip to content

Postes Praktikum Sdata IV

October 30, 2010

#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();
private :
int posisi;
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]<<“”;
return out;
}

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

return in;
}
/*int temp;
int n=maks;
int nilai;
temp=x.A[0];
for(int i=0; i<maks; i++){
cout<<“Masukkan Element Ke- : “<<i<<“=”;
in>>x.A[i];
if(x.A[i]>temp){

nilai =A[n-1];
for(int i=n-1; i>=0;i–)
A[i+1]=A[i];
A[0]=nilai;
}
}*/
/*
int posisi;
cout<<“Mengisi array pada posisi ke- :”;
in>>posisi;
if (posisi>0&&posisi<=maks){
cout<<“Masukkan element array-nya : “;
in>>x.A[posisi-1];
}
else
cout<<“Anda Memasukkan Diluar range ….”;
return in;
}*/
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=maks;
int temp= A[0];
for(int i=0;i<n;i++)
A[i]=A[i+1];
A[n-1]=temp;
}

int main(int argc, char *argv[])
{
Array1D x;
cout<<“Array masih kosong :” <<x<<endl;
cin>>x;
cout<<“isi array saat ini : “<<x<<endl;
x.geser_kiri();
cout<<“Isi Array setelah digeeser ke kiri : “<<x<<endl;
x.geser_kanan();
cout<<“Isi Array setelah digeeser ke kanan : “<<x<<endl;

system(“PAUSE”);
return EXIT_SUCCESS;
}

From → struktur data

Leave a Comment

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

%d bloggers like this: