Selasa, 02 Juni 2015

Script C++ Memindah Array 1 Dimensi ke 2 Dimensi

#include <cstdlib>
#include <iostream>

using namespace std;

class convert{
friend istream& operator>>(istream&, convert&);
friend ostream& operator<<(ostream&, convert&);

public:
convert();
void pindah();

private:
int x[100],y[100][100];
int l,k,m,n;
};

convert::convert(){
cout<<"===============================\n"<<endl;
cout<<"Memindah Array 1 ke 2 Dimensi\n"<<endl;
cout<<"===============================\n"<<endl;
}

istream& operator>>(istream& in, convert& a){
cout<<"Masukkan Banyak Digit Array 1 Dimensi :";
in>>a.l;
for(int i=0; i<a.l; i++){
cout<<"Imput Data ke ["<<i+1<<"]:";
in>>a.x[i];
}

cout<<"\nMasukkan Baris Array 2 Dimensi :";
in>>a.n;
cout<<"\nMasukkan Kolom Array 2 Dimensi :";
in>>a.m;
return in;}

ostream& operator<<(ostream& out, convert& b){
out<<"Data Array 1 Dimensi :"<<endl;
for(int i=0; i<b.l; i++){
out<<b.x[i]<<" ";
}

out<<"\nData Array Setelah di Pindah :"<<endl;
for(int i=0; i<b.n; i++){
for(int j=0; j<b.m; j++){
out<<b.y[i][j]<<" ";
}
out<<endl;
}
return out;}

void convert::pindah(){
k=0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
y[i][j]=x[k];
k=k+1;
}
cout<<endl;
}
}

int main(int argc, char *argv[])
{
convert marty;
cin>>marty;
marty.pindah();
cout<<marty;

system("PAUSE");
return EXIT_SUCCESS;
}

Hasil Comple :


0 komentar:

Posting Komentar