Array element shifting to the Left and the Right

One time set up a program, a programmer may also need to knowabout the array. Basically an array of some data blocks are put together in a variable. It will be easier for us to calculate the number of characters or move the data to the index to the left or right. As an example, we have input a sentence such as PISANG JAMBU,then if the array element in the shift to the left of the expected resultsare Anana ISANG JAMBUP and if the return on the slide to the right,then the expected return to the original, the PISANG JAMBU.Well, want to know what programming algorithm? Please note theflowchart and examples of using C + + program.

Flowchart

C++ PROGRAM

#include <iostream>

#include <string>

#include <iomanip.h>
using namespace std;
class Geser{
   friend istream& operator>>(istream&, Geser&);
   friend ostream& operator<<(ostream&, Geser&);
   public:
          Geser();
          void geser_kiri(const Geser&);
          void geser_kanan(const Geser&);
          void hitung();
   private:
           int i;
           char data[100];
           int banyak;
           int temp;
};
Geser::Geser(){}
void Geser::geser_kiri(const Geser& A){
   banyak = A.banyak;
   temp   = A.data[0];
   for(int i=0; i<=banyak-1; i++){
      if(i<banyak-1) data[i] = A.data[i+1];
      else data[i] = temp;
   }
}
void Geser::geser_kanan(const Geser& A){
   banyak = A.banyak;
   temp   = A.data[banyak-1];
   for(int i=banyak; i>=0; i--){
      if(i>0) data[i] = A.data[i-1];
      else    data[0] = temp;
   }
}
void Geser::hitung(){
   banyak    = strlen(data);
}
istream& operator>>(istream& in, Geser& masukkan){
   cout << "Masukkan data kata : ";
   cin.getline(masukkan.data, 100);
   return in;
}
ostream& operator<<(ostream& out, Geser& keluar){
   for(int i=0; i<keluar.banyak; i++){
      cout << keluar.data[i] << " | ";
   }
   cout << endl;
}
int main()
{
   Geser x;
   cin >> x;
   x.hitung();
   cout << endl << "#Data Array#" << endl;
   cout << x;
   cout << endl << "#Data Array Geser Ke Kiri#" << endl;
   x.geser_kiri(x);
   cout << x;
   cout << endl << "#Data Array Geser Ke Kanan#" << endl;
   x.geser_kanan(x);
   cout << x;
   cout << endl << "#Data Array Geser Ke Kanan (Lagi: 1 elemen)#" << endl;
   x.geser_kanan(x);
   cout << x;
   cout << endl << "#Data Array Geser Ke Kanan (Lagi: 2 elemen)#" << endl;
   x.geser_kanan(x);
   x.geser_kanan(x);
   cout << x;
   cout << endl << endl;
   system("PAUSE");
   return 0;
}

RESULT OF PROGRAM

Thank you.🙂

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: