Algoritma Vektor {menjumlahkan 2 buah vektor} |
Deklarasi elemen[100] banyak : integer vektor : class |
Deskripsi int main() Vektor X,Y,Z cin >> X cout <<X cin >> Y cout <<Y Z.perkalian_vektor(X,Y) cout << "\n Hasil : \n" << Z |
Listing
#include <cstdlib> |
#include <iostream> |
#include <conio.h> |
using namespace std; |
class Vektor{ |
friend ostream& operator<<(ostream&, Vektor&); |
friend istream& operator>>(istream&, Vektor&); |
public: |
Vektor(); |
void perkalian_vektor(const Vektor&, const Vektor&); |
void beri_nilai(int); |
private: |
int elemen[100]; |
int banyak; |
}; |
Vektor::Vektor(){ |
banyak=3; |
for(int i=0; i<banyak; i++) |
elemen[i]=0; |
} |
void Vektor::beri_nilai(int i){ |
banyak=i; |
} |
istream& operator>>(istream& in, Vektor& x){ |
cout << "PROGRAM PENJUMLAHAN DUA BUAH VEKTOR \n"; |
cout << "\nBanyak elemen : "; |
in >> x.banyak; |
cout << "Masukan data vektor : \n"; |
for(int i=0; i<x.banyak; i++){ |
cout << "Data [" << i+1 << "] : "; |
in >> x.elemen[i]; |
} |
return(in); |
} |
ostream& operator<<(ostream& out, Vektor& x){ |
out << endl; |
for(int i=0; i<x.banyak; i++) |
out << "Data [" << i+1 << "] = " << x.elemen[i] << endl; |
return(out); |
} |
void Vektor::perkalian_vektor(const Vektor& A, const 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]; |
} |
int main(int argc, char *argv[]) |
{ |
Vektor X,Y,Z; |
cin >> X; |
cout <<X; |
cin >> Y; |
cout <<Y; |
Z.perkalian_vektor(X,Y); |
cout << "\n Hasil : \n" << Z; |
getch(); |
system("PAUSE"); |
return EXIT_SUCCESS; |
} |
Tidak ada komentar:
Posting Komentar