In a previous post I explained about the array of one-dimensional this time I will create a program to reverse the array already in inputkan. More following code:
#include <cstdlib>
#include <iostream>
using namespace std;
class array{
public:
array();
void masuk();
void proses();
void keluar();
private:
int t;
int i;
int x;
int r;
int a[100];
};
array::array(){
cout<<"Membalik nilai didalam elemen array\n\n";
}
void array::masuk(){
cout<<"Banyak elemen : ";
cin>>x;
for(i=0;i<x;i++){
cout<<"data ke-"<<(i+1)<<" : ";
cin>>a[i];
}
cout<<endl<<endl;
cout<<"Data sebelum dibalik : ";
for(i=0;i<x;i++){
cout<<a[i]<<" ";
}
}
void array::proses(){
r = x / 2;
for(i=0;i<r;i++){
t = a[i];
a[i] = a[x-i-1];
a[x-i-1] = t;
}
}
void array::keluar(){
cout<<endl<<endl;
cout<<"Data setelah dibalik : ";
for(i=0;i<x;i++){
cout<<a[i]<<" ";
}
cout<<endl<<endl;
}
int main(int argc, char *argv[])
{
array a;
a.masuk();
a.proses();
a.keluar();
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
using namespace std;
class array{
public:
array();
void masuk();
void proses();
void keluar();
private:
int t;
int i;
int x;
int r;
int a[100];
};
array::array(){
cout<<"Membalik nilai didalam elemen array\n\n";
}
void array::masuk(){
cout<<"Banyak elemen : ";
cin>>x;
for(i=0;i<x;i++){
cout<<"data ke-"<<(i+1)<<" : ";
cin>>a[i];
}
cout<<endl<<endl;
cout<<"Data sebelum dibalik : ";
for(i=0;i<x;i++){
cout<<a[i]<<" ";
}
}
void array::proses(){
r = x / 2;
for(i=0;i<r;i++){
t = a[i];
a[i] = a[x-i-1];
a[x-i-1] = t;
}
}
void array::keluar(){
cout<<endl<<endl;
cout<<"Data setelah dibalik : ";
for(i=0;i<x;i++){
cout<<a[i]<<" ";
}
cout<<endl<<endl;
}
int main(int argc, char *argv[])
{
array a;
a.masuk();
a.proses();
a.keluar();
system("PAUSE");
return EXIT_SUCCESS;
}