program menggeser kekanan elemen array
#include <cstdlib>
#include <iostream>
#define maks5
using namespace std;
class array{
friend ostream& operator<<(ostream&, const array&);
friend istream& operator>>(istream&, array&);
public:
array();
void cetak();
void geser_kanan();
private:
char a[5];
int posisi;
};
array::array(){
for(int i=0;i<5;i++)
a[i]='0';
}
void array::cetak(){
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
}
ostream& operator<<(ostream& out, const array& keluar){
for(int i=0;i<5;i++)
out<<keluar.a[i]<<" ";
out<<endl;
return out;
}
istream& operator>>(istream& in, array& masuk){
int posisi;
for (int posisi=1; posisi<=5; posisi++){
cout<<"Masukkan Nilai Array Posisi Ke-"<<posisi<<"= ";
//in>>masuk.posisi;
if(posisi >= 0 && posisi <= 5){
//cout<<"masukkan elemen arraynya :";
in>>masuk.a[posisi-1];
}
}
return in;
}
void array::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;
}
int main(int argc, char *argv[])
{
array X;
cout<<"Program Array Geser Kanan\n\n";
cout<<"Array Masih Kosong : "<<X;
cin>>X;
cout<<"Isi Array Saat Ini : "<<X;
X.geser_kanan();
cout<<"Isi Array Setelah Digeser Kanan : "<<X<<endl;
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Komentar
Posting Komentar