1. Identifikasi masalah
Menggeser elemen array ke kanan dan ke kiri
2. Menentukan input dan output
input:123
output:312
3. Membuat flowchat
4. Implementasi dengan menggunakan C++:
#include <cstdlib>
#include <iostream>
#define maks5
using namespace std;
class Array1D{
friend ostream& operator<<(ostream&, const Array1D&);
friend istream& operator>>(istream&, Array1D&);
public:
Array1D();
void cetak();
void geser_kanan();
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- : "<<posisi;
if(posisi >= 0 && posisi <= 5){cout<<"\nmasukkan 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;
}
int main(int argc, char *argv[])
{
Array1D x;
cout<<"Array masih kosong : "<<x;
cin>>x;
cout<<"Isi Array saat ini : "<<x;
x.geser_kanan();
cout<<"Isi Array setelah di geser kanan : "<<x;
system("PAUSE");
return EXIT_SUCCESS;
}
5. Eksekusi#include <iostream>
#define maks5
using namespace std;
class Array1D{
friend ostream& operator<<(ostream&, const Array1D&);
friend istream& operator>>(istream&, Array1D&);
public:
Array1D();
void cetak();
void geser_kanan();
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- : "<<posisi;
if(posisi >= 0 && posisi <= 5){cout<<"\nmasukkan 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;
}
int main(int argc, char *argv[])
{
Array1D x;
cout<<"Array masih kosong : "<<x;
cin>>x;
cout<<"Isi Array saat ini : "<<x;
x.geser_kanan();
cout<<"Isi Array setelah di geser kanan : "<<x;
system("PAUSE");
return EXIT_SUCCESS;
}
1. Identifikasi masalah
Menggeser ke kiri
2. Menentukan
Input: 123
Output:231
3. Membuat Flowchat
4. Menginplementasikan dengan C++
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
void b(int f[100], int u){
int temp;
cout<<"Data Sebelum digeser"<<endl;
for(int i=0;i<u;i++){
cout<<f[i]<<" ";
}
cout<<endl<<endl;
cout<<"Data setelah digeser ke kiri"<<endl;
temp=f[0]; //u-1
for(int i=0;i<u-1;i++){
f[i]=f[i+1]; //i-1
}
f[u-1]=temp; //f[0]
for(int i=0;i<u;i++){
cout<<f[i]<<" ";
}
}
int main(int argc, char** argv) {
int u;
cout<<"================================"<<endl;
cout<<" PROGRAM MENGGESER KEKIRI "<<endl;
cout<<"================================"<<endl;
cout<<"Masukkan banyak data : ";
cin>>u;
int f[u];
for(int i=0;i<u;i++){
cout<<"Data ke-"<<i<<" = ";
cin>>f[i];
}
cout<<endl;
b(f,u);
return 0;
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
void b(int f[100], int u){
int temp;
cout<<"Data Sebelum digeser"<<endl;
for(int i=0;i<u;i++){
cout<<f[i]<<" ";
}
cout<<endl<<endl;
cout<<"Data setelah digeser ke kiri"<<endl;
temp=f[0]; //u-1
for(int i=0;i<u-1;i++){
f[i]=f[i+1]; //i-1
}
f[u-1]=temp; //f[0]
for(int i=0;i<u;i++){
cout<<f[i]<<" ";
}
}
int main(int argc, char** argv) {
int u;
cout<<"================================"<<endl;
cout<<" PROGRAM MENGGESER KEKIRI "<<endl;
cout<<"================================"<<endl;
cout<<"Masukkan banyak data : ";
cin>>u;
int f[u];
for(int i=0;i<u;i++){
cout<<"Data ke-"<<i<<" = ";
cin>>f[i];
}
cout<<endl;
b(f,u);
return 0;
}