Geser Kanan Dan Kiri
#include <cstdlib>
#include <iostream>
#include <vector>
#define maks 5
using namespace std;
class Array1D{
public:
Array1D();
void input();
void cetak();
void geser_kanan();
void geser_kiri();
private:
char A[maks];
};
Array1D::Array1D(){
cout << "Belajar array 1D" << endl << endl;
}
void Array1D::input(){
for(int i=0; i<maks; i++){
cout << "Masukkan index ke-" << i << " = ";
cin >> A[i];
}
}
void Array1D::cetak(){
for(int i=0; i<maks; i++){
cout << A[i] << " ";
}
cout << endl;
}
void Array1D::geser_kanan(){
int n = maks;
char temp = A[n-1];
for(int i=n-2; i>=0; i--)
A[i+1] = A[i];
A[0] = temp;
}
void Array1D::geser_kiri(){
int n = maks;
char temp = A[n-1];
for(int i=n-2; i>=0; i--)
A[i+1] = A[i];
A[0] = temp;
}
int main(int argc, char *argv[])
{
Array1D x;
x.input();
x.cetak();
x.geser_kanan();
x.cetak();
x.geser_kiri();
x.cetak();
system("PAUSE");
return EXIT_SUCCESS;
}
Tidak ada komentar:
Posting Komentar