Laman

Powered By Blogger

Selasa, 25 Desember 2012

Array 1 Dimensi

Static array is an array of array [0] is shifted to the left then the element will be lost and the next element will be the first element, for the reason that, in panning slider either left or right, dilai array [0] if geserkiri or array [max] if geserkanan values ​​should be saved first, and newly allocated back to the first element and the last element.

#include <iostream.h>
#define maks 5

class Array1D{
friend ostream& operator<<(ostream&, const Array1D&);
friend istream& operator>>(istream&, Array1D&);
public :
Array1D();
void cetak();
void geser_kiri();
void geser_kanan();
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;
cout<<”Mengisi array pada posisi ke : “;
in>>posisi;
if(posisi>0&&posisi<=maks){
cout<<”Masukan elemen array-nya : “;
in>>x.A[posisi-1];
}
else
cout<<”Anda memasukan posisi 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(){
Array1D x;
cout<<”Array masih kosong : “<<x;
cin>>x;
cout<<”isi array saat ini : “<<x;
x.geser_kiri();
cout<<”isi array setelah digeser kekiri :”<<x;
x.geser_kanan();
cout<<”isi array setelah digeser kekanan :”<<x;
return 0;
}

Tidak ada komentar:

Posting Komentar