Array 2 dimensi to finding vector

Posted by Didi Setyapramana On 2:40 AM 0 komentar

#include <cstdlib>
#include <iostream>

using namespace std;

class vektor{ 
friend ostream& operator<<(ostream&, vektor&);
friend istream& operator>>(istream&, vektor&);
public:
       vektor();
       void jumlah(vektor&, vektor&);
       void kali(float, vektor&);
       void nilai(int);
private:
        int elemen[100];
        int banyak;
};

vektor::vektor(){
     banyak=3;
     for(int i=0;i<banyak;i++)
     elemen[i]=0;
}

void vektor::nilai(int i){
     banyak=i;
}

istream& operator>>(istream& in, vektor& input){
          cout<<"Banyak Elemen:";
          in>>input.banyak;
          cout<<"Masukan data vektor"<<endl;
          for(int i=0;i<input.banyak;i++){
                  cout<<"Data["<<i+1<<"]:";
                  cin>>input.elemen[i];
          }
    return in;
}

ostream& operator<<(ostream& out, vektor& output){
         cout<<endl;
         for(int i=0;i<output.banyak;i++)
                 cout<<"s["<<i+1<<"]="<<output.elemen[i]<<endl;
         return out;
}

void vektor::jumlah(vektor& a, vektor& b){
     if(a.banyak>b.banyak)banyak=a.banyak;
     else banyak=b.banyak;
     for(int i=0; i<banyak;i++)
     elemen[i]=a.elemen[i]+b.elemen[i];
}

void vektor::kali(float k, vektor& a){
     banyak=a.banyak;
     for(int i=0;i<banyak;i++)
     elemen[i]=k*a.elemen[i];
}

int main(int argc, char *argv[])
{
    vektor x,y,z;
    cin>>x;
    cout<<x;
    cin>>y;
    cout<<y;
    z.jumlah(x,y);
    cout<<"Jumlah 2 vektor"<<z;
    z.kali(3,x);
    cout<<"Kali vektor"<<z;
    cout<<z;
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

Categories: ,

0 Response for the "Array 2 dimensi to finding vector"

Post a Comment