#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
class matriks{
friend istream& operator>>(istream&, matriks&);
friend ostream& operator<<(ostream&, matriks&);
public:
void baca_matriks();
void matriks_jumlah(const matriks&, const matriks&);
void perkalian_matriks(const matriks&, const matriks&);
void cetak_matriks();
private:
int baris, kolom;
int A[10][10];
};
void matriks::baca_matriks(){
int i,j;
for(i=0; i<baris; i++)
for(j=0;j<kolom;j++){
cout<<"Data["<<i+1<<"],["<<j+1<<"] :";
cin>>A[i][j];
}
}
void matriks::matriks_jumlah(const matriks& matriks1, const matriks& matriks2){
int i,j;
baris=matriks1.baris;
kolom=matriks1.kolom;
for(i=0;i<baris;i++)
for(j=0;j<kolom;j++)
A[i][j]=matriks1.A[i][j]+matriks2.A[i][j];
//cetak_matriks();
cout <<endl;
}
void matriks::perkalian_matriks(const matriks& matriks1, const matriks& matriks2){
int i,j,k;
int barkol;
baris=matriks1.baris;
kolom=matriks1.kolom;
barkol=matriks1.kolom;
for(i=0;i<baris;i++)
for(j=0;j<kolom;j++){
A[i][j]=0;
for(k=0;k<barkol;k++)
A[i][j]+=matriks1.A[i][k]*matriks2.A[k][j];
}
// cetak_matriks();
}
void matriks::cetak_matriks(){
int i,j;
for(i=0;i<baris;i++){
for(j=0;j<kolom;j++)
cout<<setw(5)<<A[i][j]<<" ";
cout <<endl;
}
}
istream& operator>>(istream& in, matriks& A){
cout<<"Banyak baris : ";
in>>A.baris;
cout<<"Banyak kolom : ";
in>>A.kolom;
cout<<"Masukkan data matriks\n";
A.baca_matriks();
cout<<"matriks yang dibuat adalah : \n";
A.cetak_matriks();
return in;
}
ostream& operator<<(ostream& out, matriks& A){
int i,j;
for(i=0;i<A.baris;i++)
{
for(j=0;j<A.kolom;j++)
cout <<setw(5)<<A.A[i][j]<<" ";
}
return out;
}
main(){
matriks matriks1, matriks2;
matriks jumlah;
cout<< "Masukkan data matriks I\n";
cin>>matriks1;
cout<<"Masukkan data matriks II\n";
cin>>matriks2;
jumlah.matriks_jumlah(matriks1, matriks2);
cout<<"Hasil penjumlahan : \n";
jumlah.cetak_matriks();
jumlah.perkalian_matriks(matriks1,matriks2);
cout<<"Hasil perkalian : \n";
jumlah.cetak_matriks();
return 0;
};
15 May 2012
Program penjumlahan vektor
#include <iostream.h>
#include <iomanip.h>
class vektor{
friend ostream& operator << (ostream& , vektor& );
friend istream& operator >> (istream& , vektor& );
public:
vektor();
void penjumlahan(const vektor&, const vektor&);
void perkalian(float, const vektor&);
void nilai(int);
private:
int elemen[100];
int banyak;
};
vektor::vektor(){
banyak=3;
for(int i=0;i<banyak;i++)
elemen[i];
}
istream& operator >> (istream& in, vektor& a){
cout<<"\nbanyak elemen : "; in>>a.banyak;
cout<<"masukkan data vektor\n : ";
for(int i=0;i<a.banyak;i++){
cout<<"data["<<i+1<<"] :";
cin>>a.elemen[i];
}
return in;
}
void vektor::nilai(int i){
banyak= i;
}
ostream& operator << (ostream& out, vektor& a){
cout<<endl;
for(int i=0;i<a.banyak;i++)
cout<<"s["<<i+1<<"]="<<setw(5)<<a.elemen[i]<<"\n";
return out;
}
void vektor::penjumlahan(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];
}
void vektor::perkalian(float k, const vektor& a){
banyak = a.banyak;
for(int i=0;i<banyak;i++)
elemen[i]=k*a.elemen[i];
}
#include <iostream.h>
#include <iomanip.h>
#include "vektor.h"
void main(){
vektor x, y, z;
cin>>x;
cout<<x;
cin>> y;
cout<<y;
z.penjumlahan(x,y);
cout<<"\nhasil penjumlahan 2 vektor\n"<<z;
cout<<z;
// getch();
}
#include <iomanip.h>
class vektor{
friend ostream& operator << (ostream& , vektor& );
friend istream& operator >> (istream& , vektor& );
public:
vektor();
void penjumlahan(const vektor&, const vektor&);
void perkalian(float, const vektor&);
void nilai(int);
private:
int elemen[100];
int banyak;
};
vektor::vektor(){
banyak=3;
for(int i=0;i<banyak;i++)
elemen[i];
}
istream& operator >> (istream& in, vektor& a){
cout<<"\nbanyak elemen : "; in>>a.banyak;
cout<<"masukkan data vektor\n : ";
for(int i=0;i<a.banyak;i++){
cout<<"data["<<i+1<<"] :";
cin>>a.elemen[i];
}
return in;
}
void vektor::nilai(int i){
banyak= i;
}
ostream& operator << (ostream& out, vektor& a){
cout<<endl;
for(int i=0;i<a.banyak;i++)
cout<<"s["<<i+1<<"]="<<setw(5)<<a.elemen[i]<<"\n";
return out;
}
void vektor::penjumlahan(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];
}
void vektor::perkalian(float k, const vektor& a){
banyak = a.banyak;
for(int i=0;i<banyak;i++)
elemen[i]=k*a.elemen[i];
}
#include <iostream.h>
#include <iomanip.h>
#include "vektor.h"
void main(){
vektor x, y, z;
cin>>x;
cout<<x;
cin>> y;
cout<<y;
z.penjumlahan(x,y);
cout<<"\nhasil penjumlahan 2 vektor\n"<<z;
cout<<z;
// getch();
}
Label:
program C++
Program matrix_tranpose
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
class matriks{
friend istream& operator>>(istream&, matriks&);
friend ostream& operator<<(ostream&, matriks&);
public:
void baca_matriks();
matriks& transpose(const matriks&);
private:
int baris, kolom;
int A[10][10];
};
void matriks::baca_matriks(){
int i,j;
for(i=0; i<baris; i++)
for(j=0;j<kolom;j++){
cout<<"Data["<<i+1<<"],["<<j+1<<"] :";
cin>>A[i][j];
}
}
matriks& matriks::transpose(const matriks& matrik){
int i,j;
baris=matrik.kolom;
kolom=matrik.baris;
for(i=0;i<baris;i++)
for(j=0;j<kolom;j++){
A[i][j]=matrik.A[j][i];
}
return *this;
}
istream& operator>>(istream& in, matriks& A){
cout<<"Banyak baris : ";
in>>A.baris;
cout<<"Banyak kolom : ";
in>>A.kolom;
cout<<"Masukkan data matriks\n";
A.baca_matriks();
return in;
}
ostream& operator<<(ostream& out, matriks& A){
int i,j;
for(i=0;i<A.baris;i++)
{
for(j=0;j<A.kolom;j++)
cout <<setw(5)<<A.A[i][j]<<" ";
cout <<endl;
}
return out;
}
int main(){
matriks matrik, T;
cout<< "Masukkan data matriks \n"; cin>>matrik;
cout<<"Data Matrik :\n"; cout <<matrik;
cout<<"Hasil Transpose : \n";
T.transpose(matrik);
cout << T;
return 0;
}
#include <iostream.h>
#include <iomanip.h>
class matriks{
friend istream& operator>>(istream&, matriks&);
friend ostream& operator<<(ostream&, matriks&);
public:
void baca_matriks();
matriks& transpose(const matriks&);
private:
int baris, kolom;
int A[10][10];
};
void matriks::baca_matriks(){
int i,j;
for(i=0; i<baris; i++)
for(j=0;j<kolom;j++){
cout<<"Data["<<i+1<<"],["<<j+1<<"] :";
cin>>A[i][j];
}
}
matriks& matriks::transpose(const matriks& matrik){
int i,j;
baris=matrik.kolom;
kolom=matrik.baris;
for(i=0;i<baris;i++)
for(j=0;j<kolom;j++){
A[i][j]=matrik.A[j][i];
}
return *this;
}
istream& operator>>(istream& in, matriks& A){
cout<<"Banyak baris : ";
in>>A.baris;
cout<<"Banyak kolom : ";
in>>A.kolom;
cout<<"Masukkan data matriks\n";
A.baca_matriks();
return in;
}
ostream& operator<<(ostream& out, matriks& A){
int i,j;
for(i=0;i<A.baris;i++)
{
for(j=0;j<A.kolom;j++)
cout <<setw(5)<<A.A[i][j]<<" ";
cout <<endl;
}
return out;
}
int main(){
matriks matrik, T;
cout<< "Masukkan data matriks \n"; cin>>matrik;
cout<<"Data Matrik :\n"; cout <<matrik;
cout<<"Hasil Transpose : \n";
T.transpose(matrik);
cout << T;
return 0;
}
Label:
program C++
Program matrix_kali_vector
#include <iostream.h>
#include <iomanip.h>
#include <stdio.h>
class matrix{
friend ostream& operator<<(ostream&, matrix&);
friend istream& operator>>(istream&, matrix&);
public:
void input_matrix();
int baris, kolom;
int M[10][10];
};
class vektor{
friend ostream& operator<<(ostream&, vektor&);
friend istream& operator>>(istream&, vektor&);
public:
vektor& matrix_kali_vektor(matrix&, vektor&);
vektor();
int banyak;
int elemen[10];
};
vektor::vektor(){
banyak=10;
for(int i=0; i<banyak; i++) elemen[i]=0;
}
void matrix::input_matrix(){
int i,j;
for(i=0; i<baris; i++)
for(j=0;j<kolom;j++){
cout<<"Data["<<i+1<<"]["<<j+1<<"] :";
cin>>M[i][j];
}
}
istream& operator>>(istream& in, matrix& input){
cout<<"Masukkan baris : ";
in>>input.baris;
cout<<"Masukkan kolom : ";
in>>input.kolom;
cout<<"Masukkan data matrix"<<endl;
input.input_matrix();
return in;
}
ostream& operator<<(ostream& out, matrix& output){
int i,j;
for(i=0;i<output.baris;i++){
for(j=0;j<output.kolom;j++){
cout <<setw(5)<<output.M[i][j]<<" ";
}
cout <<endl;
}
return out;
}
istream& operator>>(istream& in, vektor& input){
cout<<"Banyak elemen vektor : ";
in>>input.banyak;
cout<<"Masukkan data vektor\n";
for(int i=0;i<input.banyak;i++){
cout <<"Data["<<i+1<<"]: ";
cin>>input.elemen[i];
}
return in;
}
ostream& operator<<(ostream& out, vektor& output){
for(int i=0;i<output.banyak;i++){
cout <<setw(5)<<output.elemen[i]<<endl;
}
return out;
}
vektor& vektor::matrix_kali_vektor(matrix& Matriks, vektor& Vector){
int i,j;
banyak=Matriks.baris;
if(Matriks.kolom!=Vector.banyak){
cout<<"Tidak Bisa mengalikan matrix dengan vector"<<endl;
banyak=0;
return *this;
}
for(i=0;i<Matriks.baris;i++){
for(j=0;j<Matriks.kolom;j++){
elemen[i] += Matriks.M[i][j]*Vector.elemen[j];
}
}
return *this;
}
int main(){
matrix mtr;
vektor vek, kali;
cout<<"Masukkan data matrix"<<endl;
cin>>mtr;
cout << mtr;
cout<<"Masukkan data vektor"<<endl;
cin>>vek;
cout <<vek;
cout<<"Hasil perkalian : \n";
kali.matrix_kali_vektor(mtr,vek);
cout<<kali;
return 0;
}
#include <iomanip.h>
#include <stdio.h>
class matrix{
friend ostream& operator<<(ostream&, matrix&);
friend istream& operator>>(istream&, matrix&);
public:
void input_matrix();
int baris, kolom;
int M[10][10];
};
class vektor{
friend ostream& operator<<(ostream&, vektor&);
friend istream& operator>>(istream&, vektor&);
public:
vektor& matrix_kali_vektor(matrix&, vektor&);
vektor();
int banyak;
int elemen[10];
};
vektor::vektor(){
banyak=10;
for(int i=0; i<banyak; i++) elemen[i]=0;
}
void matrix::input_matrix(){
int i,j;
for(i=0; i<baris; i++)
for(j=0;j<kolom;j++){
cout<<"Data["<<i+1<<"]["<<j+1<<"] :";
cin>>M[i][j];
}
}
istream& operator>>(istream& in, matrix& input){
cout<<"Masukkan baris : ";
in>>input.baris;
cout<<"Masukkan kolom : ";
in>>input.kolom;
cout<<"Masukkan data matrix"<<endl;
input.input_matrix();
return in;
}
ostream& operator<<(ostream& out, matrix& output){
int i,j;
for(i=0;i<output.baris;i++){
for(j=0;j<output.kolom;j++){
cout <<setw(5)<<output.M[i][j]<<" ";
}
cout <<endl;
}
return out;
}
istream& operator>>(istream& in, vektor& input){
cout<<"Banyak elemen vektor : ";
in>>input.banyak;
cout<<"Masukkan data vektor\n";
for(int i=0;i<input.banyak;i++){
cout <<"Data["<<i+1<<"]: ";
cin>>input.elemen[i];
}
return in;
}
ostream& operator<<(ostream& out, vektor& output){
for(int i=0;i<output.banyak;i++){
cout <<setw(5)<<output.elemen[i]<<endl;
}
return out;
}
vektor& vektor::matrix_kali_vektor(matrix& Matriks, vektor& Vector){
int i,j;
banyak=Matriks.baris;
if(Matriks.kolom!=Vector.banyak){
cout<<"Tidak Bisa mengalikan matrix dengan vector"<<endl;
banyak=0;
return *this;
}
for(i=0;i<Matriks.baris;i++){
for(j=0;j<Matriks.kolom;j++){
elemen[i] += Matriks.M[i][j]*Vector.elemen[j];
}
}
return *this;
}
int main(){
matrix mtr;
vektor vek, kali;
cout<<"Masukkan data matrix"<<endl;
cin>>mtr;
cout << mtr;
cout<<"Masukkan data vektor"<<endl;
cin>>vek;
cout <<vek;
cout<<"Hasil perkalian : \n";
kali.matrix_kali_vektor(mtr,vek);
cout<<kali;
return 0;
}
Label:
program C++
Program Matrik invers
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
class matriks{
friend istream& operator>>(istream&, matriks&);
friend ostream& operator<<(ostream&, matriks&);
public:
matriks(){n=2;};
void baca_matriks();
matriks& inverse(const matriks&);
float det(const matriks&);
matriks& adjoint(const matriks&);
matriks& perkalian_matriks(const matriks&, const matriks&);
private:
int n;
float A[10][10];
};
float matriks::det(const matriks& matriks1){
float m=matriks1.A[0][0]*matriks1.A[1][1]-matriks1.A[0][1]*matriks1.A[1][0];
return m;
}
matriks& matriks::adjoint(const matriks& matriks1){
A[0][0]=matriks1.A[1][1];
A[0][1]=-(matriks1.A[0][1]);
A[1][0]=-(matriks1.A[1][0]);
A[1][1]=matriks1.A[0][0];
return* this;
}
void matriks::baca_matriks(){
int i,j;
for(i=0; i<n; i++)
for(j=0;j<n;j++){
cout<<"Data["<<i+1<<"],["<<j+1<<"] :";
cin>>A[i][j];
}
}
matriks& matriks::inverse(const matriks& matriks1){
int i,j;
matriks adj;
adj.adjoint(matriks1);
for(i=matriks1.n-1;i>=0;i--)
for(j=matriks1.n-1;j>=0;j--){
A[i][j]=1/det(matriks1)*adj.A[i][j];
}
return* this;
}
istream& operator>>(istream& in, matriks& A){
cout<<"Masukkan data matriks\n";
A.baca_matriks();
return in;
}
ostream& operator<<(ostream& out, matriks& A){
int i,j;
for(i=0;i<A.n;i++)
{
for(j=0;j<A.n;j++)
cout <<setw(5)<<A.A[i][j]<<" ";
cout <<endl;
}
return out;
}
matriks& matriks::perkalian_matriks(const matriks& matriks1, const matriks& matriks2){
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++){
A[i][j]=0;
for(k=0;k<n;k++){
A[i][j]+=matriks1.A[i][k]*matriks2.A[k][j];
}
}
return* this;
}
int main(){
matriks matriks1, I, kali;
cout<< "Masukkan data matriks I\n"; cin>>matriks1;
cout<<"Data Matrik :\n"; cout <<matriks1;
I.inverse(matriks1);
cout<<"Data Matrik Inverse:\n"; cout << I;
kali.perkalian_matriks(matriks1, I);
cout<<"hasil kali : \n";
cout << kali;
return 0;
}
#include <iostream.h>
#include <iomanip.h>
class matriks{
friend istream& operator>>(istream&, matriks&);
friend ostream& operator<<(ostream&, matriks&);
public:
matriks(){n=2;};
void baca_matriks();
matriks& inverse(const matriks&);
float det(const matriks&);
matriks& adjoint(const matriks&);
matriks& perkalian_matriks(const matriks&, const matriks&);
private:
int n;
float A[10][10];
};
float matriks::det(const matriks& matriks1){
float m=matriks1.A[0][0]*matriks1.A[1][1]-matriks1.A[0][1]*matriks1.A[1][0];
return m;
}
matriks& matriks::adjoint(const matriks& matriks1){
A[0][0]=matriks1.A[1][1];
A[0][1]=-(matriks1.A[0][1]);
A[1][0]=-(matriks1.A[1][0]);
A[1][1]=matriks1.A[0][0];
return* this;
}
void matriks::baca_matriks(){
int i,j;
for(i=0; i<n; i++)
for(j=0;j<n;j++){
cout<<"Data["<<i+1<<"],["<<j+1<<"] :";
cin>>A[i][j];
}
}
matriks& matriks::inverse(const matriks& matriks1){
int i,j;
matriks adj;
adj.adjoint(matriks1);
for(i=matriks1.n-1;i>=0;i--)
for(j=matriks1.n-1;j>=0;j--){
A[i][j]=1/det(matriks1)*adj.A[i][j];
}
return* this;
}
istream& operator>>(istream& in, matriks& A){
cout<<"Masukkan data matriks\n";
A.baca_matriks();
return in;
}
ostream& operator<<(ostream& out, matriks& A){
int i,j;
for(i=0;i<A.n;i++)
{
for(j=0;j<A.n;j++)
cout <<setw(5)<<A.A[i][j]<<" ";
cout <<endl;
}
return out;
}
matriks& matriks::perkalian_matriks(const matriks& matriks1, const matriks& matriks2){
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++){
A[i][j]=0;
for(k=0;k<n;k++){
A[i][j]+=matriks1.A[i][k]*matriks2.A[k][j];
}
}
return* this;
}
int main(){
matriks matriks1, I, kali;
cout<< "Masukkan data matriks I\n"; cin>>matriks1;
cout<<"Data Matrik :\n"; cout <<matriks1;
I.inverse(matriks1);
cout<<"Data Matrik Inverse:\n"; cout << I;
kali.perkalian_matriks(matriks1, I);
cout<<"hasil kali : \n";
cout << kali;
return 0;
}
Label:
program C++
Program Matrik identitas
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
class matriks{
friend istream& operator>>(istream&, matriks&);
friend ostream& operator<<(ostream&, matriks&);
public:
void buat_identitas(int);
private:
int banyak;
int A[10][10];
};
void matriks::buat_identitas(int n){
int i,j;
banyak=n;
for(i=0; i<n; i++)
for(j=0;j<n;j++){
if(i==j) {
A[i][j]=1;
continue;
}
A[i][j]=0;
}
}
istream& operator>>(istream& in, matriks& A){
return in;
}
ostream& operator<<(ostream& out, matriks& A){
int i,j;
for(i=0;i<A.banyak;i++)
{
for(j=0;j<A.banyak;j++)
cout <<setw(5)<<A.A[i][j]<<" ";
cout <<endl;
}
return out;
}
int main(){
matriks I;
int banyak_elemen;
cout<< "Jumlah elemen \n"; cin>>banyak_elemen;
cout<<"Matriks Identitasnya : \n";
I.buat_identitas(banyak_elemen);
cout << I;
return 0;
}
#include <iostream.h>
#include <iomanip.h>
class matriks{
friend istream& operator>>(istream&, matriks&);
friend ostream& operator<<(ostream&, matriks&);
public:
void buat_identitas(int);
private:
int banyak;
int A[10][10];
};
void matriks::buat_identitas(int n){
int i,j;
banyak=n;
for(i=0; i<n; i++)
for(j=0;j<n;j++){
if(i==j) {
A[i][j]=1;
continue;
}
A[i][j]=0;
}
}
istream& operator>>(istream& in, matriks& A){
return in;
}
ostream& operator<<(ostream& out, matriks& A){
int i,j;
for(i=0;i<A.banyak;i++)
{
for(j=0;j<A.banyak;j++)
cout <<setw(5)<<A.A[i][j]<<" ";
cout <<endl;
}
return out;
}
int main(){
matriks I;
int banyak_elemen;
cout<< "Jumlah elemen \n"; cin>>banyak_elemen;
cout<<"Matriks Identitasnya : \n";
I.buat_identitas(banyak_elemen);
cout << I;
return 0;
}
Label:
program C++
Program Matrik kali Matrik
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
class matriks{
friend istream& operator>>(istream&, matriks&);
friend ostream& operator<<(ostream&, matriks&);
public:
void baca_matriks();
matriks& perkalian_matriks(const matriks&, const matriks&);
private:
int baris, kolom;
int A[10][10];
};
void matriks::baca_matriks(){
int i,j;
for(i=0; i<baris; i++)
for(j=0;j<kolom;j++){
cout<<"Data["<<i+1<<"],["<<j+1<<"] :";
cin>>A[i][j];
}
}
matriks& matriks::perkalian_matriks(const matriks& matriks1, const matriks& matriks2){
int i,j,k;
int barkol;
baris=matriks1.baris;
kolom=matriks2.kolom;
barkol=matriks2.baris;
for(i=0;i<baris;i++)
for(j=0;j<kolom;j++){
A[i][j]=0;
for(k=0;k<barkol;k++){
A[i][j]+=matriks1.A[i][k]*matriks2.A[k][j];
}
}
return* this;
}
istream& operator>>(istream& in, matriks& A){
cout<<"Banyak baris : ";
in>>A.baris;
cout<<"Banyak kolom : ";
in>>A.kolom;
cout<<"Masukkan data matriks\n";
A.baca_matriks();
return in;
}
ostream& operator<<(ostream& out, matriks& A){
int i,j;
for(i=0;i<A.baris;i++)
{
for(j=0;j<A.kolom;j++)
cout <<setw(5)<<A.A[i][j]<<" ";
cout <<endl;
}
return out;
}
int main(){
matriks matriks1, matriks2, kali;
cout<< "Masukkan data matriks I\n"; cin>>matriks1;
cout<<"Data Matrik I :\n"; cout <<matriks1;
cout<<"Masukkan data matriks II\n"; cin>>matriks2;
cout<<"Data Matrik II :\n"; cout <<matriks2;
kali.perkalian_matriks(matriks1,matriks2);
cout<<"Hasil perkalian : \n";
cout << kali;
return 0;
}
#include <iostream.h>
#include <iomanip.h>
class matriks{
friend istream& operator>>(istream&, matriks&);
friend ostream& operator<<(ostream&, matriks&);
public:
void baca_matriks();
matriks& perkalian_matriks(const matriks&, const matriks&);
private:
int baris, kolom;
int A[10][10];
};
void matriks::baca_matriks(){
int i,j;
for(i=0; i<baris; i++)
for(j=0;j<kolom;j++){
cout<<"Data["<<i+1<<"],["<<j+1<<"] :";
cin>>A[i][j];
}
}
matriks& matriks::perkalian_matriks(const matriks& matriks1, const matriks& matriks2){
int i,j,k;
int barkol;
baris=matriks1.baris;
kolom=matriks2.kolom;
barkol=matriks2.baris;
for(i=0;i<baris;i++)
for(j=0;j<kolom;j++){
A[i][j]=0;
for(k=0;k<barkol;k++){
A[i][j]+=matriks1.A[i][k]*matriks2.A[k][j];
}
}
return* this;
}
istream& operator>>(istream& in, matriks& A){
cout<<"Banyak baris : ";
in>>A.baris;
cout<<"Banyak kolom : ";
in>>A.kolom;
cout<<"Masukkan data matriks\n";
A.baca_matriks();
return in;
}
ostream& operator<<(ostream& out, matriks& A){
int i,j;
for(i=0;i<A.baris;i++)
{
for(j=0;j<A.kolom;j++)
cout <<setw(5)<<A.A[i][j]<<" ";
cout <<endl;
}
return out;
}
int main(){
matriks matriks1, matriks2, kali;
cout<< "Masukkan data matriks I\n"; cin>>matriks1;
cout<<"Data Matrik I :\n"; cout <<matriks1;
cout<<"Masukkan data matriks II\n"; cin>>matriks2;
cout<<"Data Matrik II :\n"; cout <<matriks2;
kali.perkalian_matriks(matriks1,matriks2);
cout<<"Hasil perkalian : \n";
cout << kali;
return 0;
}
Label:
program C++
Program Matriks
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <iomanip.h>
class matrix{
friend ostream& operator<<(ostream&, matrix&);
friend istream& operator>>(istream&, matrix&);
public:
int cek();
void gaus();
void hitung();
private:
float A[10][10], x[10];
int i, j, k, baris, kolom;
};
istream& operator >> (istream& in, matrix& A){
cout<<"masukkan jumlah persamaan :";
in>>A.baris;
A.kolom=A.baris;
A.baris-=1;
cout<<"masukkan konstanta\n";
for(A.i=0;A.i<=A.baris;A.i++){
for(A.j=0;A.j<=A.kolom;A.j++){
if (A.j==A.kolom) cout<<"b"<<A.i+1<<" = ";
else cout<<"x"<<A.j+1<<" = ";
cin>>A.A[A.i][A.j];
}
for(A.j=0;A.j<=A.kolom;A.j++){
if (A.j==(A.kolom-1))cout<<A.A[A.i][A.j]<<"*x"<<A.j+1<<" = ";
else if (A.j==(A.kolom))cout<<A.A[A.i][A.j]<<"\n";
else cout<<A.A[A.i][A.j]<<"*x"<<A.j+1<<" + ";
}
}
cout<<"matrix yang dibuat adalah\n";
return in;
}
void matrix::gaus(){
float g;
for(i=1;i<=baris;i++)
for(j=0;j<=kolom;j++){
if (j<i && A[i][j]!=0){
g=A[i][j]/A[j][j];
for(k=j;k<=kolom;k++){
A[i][k]=A[i][k]-A[j][k]*g;
}
}
}
for(i=0;i<=baris;i++){
g=A[i][i];
for(j=0;j<=kolom;j++){
A[i][j]=A[i][j]/g;
}
}
}
int matrix::cek(){
for(i=0;i<=baris;i++){
if(A[i][i]!=1) return 0;
for(j=0;j<i;j++){
if(A[i][j]!=0) return 0;
}
}
return 1;
}
void matrix::hitung(){
x[baris]=A[baris][kolom];
for(i=(baris-1);i>=0;i--){
x[i]=A[i][kolom];
for(j=(i+1);j<(kolom);j++){
x[i]-=(A[i][j]*x[j]);
}
}
}
ostream& operator << (ostream& out, matrix& A){
for(A.i=0;A.i<=A.baris;A.i++){
for(A.j=0;A.j<=A.kolom;A.j++)
cout<<setw(10)<<(A.A[A.i][A.j])<<"";
cout<<endl;
}
if (A.cek()) {
A.hitung();
cout<<"maka nilai dari tiap variabel adalah";
for(A.i=0;A.i<=A.baris;A.i++){
cout<<"\nx"<<A.i+1<<"="<<A.x[A.i];
}
}
return out;
}
main(){
matrix m;
cout<<"masukkan data matrix 1\n";
cin>>m;
cout<<"matrixnya\n";
cout<<m;
m.gaus();
cout<<"hasil eliminasi gaus\n";
cout<<"matrix segitiga atas\n"<<m<<"\n";
getch();
return 0;
}
#include <conio.h>
#include <stdio.h>
#include <iomanip.h>
class matrix{
friend ostream& operator<<(ostream&, matrix&);
friend istream& operator>>(istream&, matrix&);
public:
int cek();
void gaus();
void hitung();
private:
float A[10][10], x[10];
int i, j, k, baris, kolom;
};
istream& operator >> (istream& in, matrix& A){
cout<<"masukkan jumlah persamaan :";
in>>A.baris;
A.kolom=A.baris;
A.baris-=1;
cout<<"masukkan konstanta\n";
for(A.i=0;A.i<=A.baris;A.i++){
for(A.j=0;A.j<=A.kolom;A.j++){
if (A.j==A.kolom) cout<<"b"<<A.i+1<<" = ";
else cout<<"x"<<A.j+1<<" = ";
cin>>A.A[A.i][A.j];
}
for(A.j=0;A.j<=A.kolom;A.j++){
if (A.j==(A.kolom-1))cout<<A.A[A.i][A.j]<<"*x"<<A.j+1<<" = ";
else if (A.j==(A.kolom))cout<<A.A[A.i][A.j]<<"\n";
else cout<<A.A[A.i][A.j]<<"*x"<<A.j+1<<" + ";
}
}
cout<<"matrix yang dibuat adalah\n";
return in;
}
void matrix::gaus(){
float g;
for(i=1;i<=baris;i++)
for(j=0;j<=kolom;j++){
if (j<i && A[i][j]!=0){
g=A[i][j]/A[j][j];
for(k=j;k<=kolom;k++){
A[i][k]=A[i][k]-A[j][k]*g;
}
}
}
for(i=0;i<=baris;i++){
g=A[i][i];
for(j=0;j<=kolom;j++){
A[i][j]=A[i][j]/g;
}
}
}
int matrix::cek(){
for(i=0;i<=baris;i++){
if(A[i][i]!=1) return 0;
for(j=0;j<i;j++){
if(A[i][j]!=0) return 0;
}
}
return 1;
}
void matrix::hitung(){
x[baris]=A[baris][kolom];
for(i=(baris-1);i>=0;i--){
x[i]=A[i][kolom];
for(j=(i+1);j<(kolom);j++){
x[i]-=(A[i][j]*x[j]);
}
}
}
ostream& operator << (ostream& out, matrix& A){
for(A.i=0;A.i<=A.baris;A.i++){
for(A.j=0;A.j<=A.kolom;A.j++)
cout<<setw(10)<<(A.A[A.i][A.j])<<"";
cout<<endl;
}
if (A.cek()) {
A.hitung();
cout<<"maka nilai dari tiap variabel adalah";
for(A.i=0;A.i<=A.baris;A.i++){
cout<<"\nx"<<A.i+1<<"="<<A.x[A.i];
}
}
return out;
}
main(){
matrix m;
cout<<"masukkan data matrix 1\n";
cin>>m;
cout<<"matrixnya\n";
cout<<m;
m.gaus();
cout<<"hasil eliminasi gaus\n";
cout<<"matrix segitiga atas\n"<<m<<"\n";
getch();
return 0;
}
Label:
program C++
Program matrik penjumlahan dan vektor kali skalar
#include <iostream.h>
#include <conio.h>
class vektor{
friend ostream& operator<<(ostream&, const vektor&);
friend istream& operator>>(istream&, vektor&);
public:
vektor();
void penjumlahan_vektor(const vektor&,const vektor&);
void perkalian_vektor(float, const vektor&);
void beri_nilaibanyak(int);
private:
int elemen[100];
int banyak;
};
vektor::vektor(){
banyak=3;
for(int i=0; i<banyak; i++)
elemen[i]=0;
}
void vektor::beri_nilaibanyak(int i){
banyak=i;
}
istream& operator>>(istream& in, vektor& A){
cout<<"\nBanyak elemen : ";
in>>A.banyak;
cout<<"Masukkan data vektor\n";
for(int i=0; i<A.banyak; i++){
cout<<"Data ["<<i+1<<"] : ";
cin>>A.elemen[i];
}
return in;
}
ostream& operator<<(ostream& out, const vektor& A){
cout<<endl;
for(int i=0; i<A.banyak; i++)
cout<<"s ["<<i+1<<"] = "<<A.elemen[i]<<"\n";
return out;
}
void vektor::penjumlahan_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];
}
void vektor::perkalian_vektor(float k, const vektor& A){
banyak=A.banyak;
for(int i=0; i<banyak; i++)
elemen[i]=int(k*A.elemen[i]);
}
void main(){
vektor x,y,z;
cin>>x;
cout<<x;
cin>>y;
cout<<y;
z.penjumlahan_vektor(x,y);
cout<<"\nHasil penjumlahan dua vektor\n"<<z;
z.perkalian_vektor(3,x);
cout<<"\nHasil perkalian sekalar dengan vektor\n"<<z;
cout<<z;
getch();
}
#include <conio.h>
class vektor{
friend ostream& operator<<(ostream&, const vektor&);
friend istream& operator>>(istream&, vektor&);
public:
vektor();
void penjumlahan_vektor(const vektor&,const vektor&);
void perkalian_vektor(float, const vektor&);
void beri_nilaibanyak(int);
private:
int elemen[100];
int banyak;
};
vektor::vektor(){
banyak=3;
for(int i=0; i<banyak; i++)
elemen[i]=0;
}
void vektor::beri_nilaibanyak(int i){
banyak=i;
}
istream& operator>>(istream& in, vektor& A){
cout<<"\nBanyak elemen : ";
in>>A.banyak;
cout<<"Masukkan data vektor\n";
for(int i=0; i<A.banyak; i++){
cout<<"Data ["<<i+1<<"] : ";
cin>>A.elemen[i];
}
return in;
}
ostream& operator<<(ostream& out, const vektor& A){
cout<<endl;
for(int i=0; i<A.banyak; i++)
cout<<"s ["<<i+1<<"] = "<<A.elemen[i]<<"\n";
return out;
}
void vektor::penjumlahan_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];
}
void vektor::perkalian_vektor(float k, const vektor& A){
banyak=A.banyak;
for(int i=0; i<banyak; i++)
elemen[i]=int(k*A.elemen[i]);
}
void main(){
vektor x,y,z;
cin>>x;
cout<<x;
cin>>y;
cout<<y;
z.penjumlahan_vektor(x,y);
cout<<"\nHasil penjumlahan dua vektor\n"<<z;
z.perkalian_vektor(3,x);
cout<<"\nHasil perkalian sekalar dengan vektor\n"<<z;
cout<<z;
getch();
}
Label:
program C++
Program kali matrik skalar
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <iomanip.h>
class matrix{
friend ostream& operator<<(ostream&, matrix&);
friend istream& operator>>(istream&, matrix&);
public :
void input_matrix();
void kali_matrix_skalar(const matrix&, int);
private:
int M[20][20];
int baris, kolom;
};
void matrix::input_matrix(){
int i,j;
for(i=0;i<baris;i++){
for(j=0;j<kolom;j++){
cout<<"Data ["<<(i+1)<<"] ["<<(j+1)<<"] = ";
cin>>M[i][j];
}
}
}
void matrix::kali_matrix_skalar(const matrix& matrixA, int skalar){
int i,j;
baris=matrixA.baris;
kolom=matrixA.kolom;
for(i=0;i<baris;i++){
for(j=0;j<kolom;j++){
M[i][j]=skalar*matrixA.M[i][j];
}
}
}
istream& operator>>(istream& in, matrix& input){
cout<<"Masukkan baris = ";
in>>input.baris;
cout<<"Masukkan kolom = ";
in>>input.kolom;
input.input_matrix();
return in;
}
ostream& operator<<(ostream& out, matrix& output){
int i,j;
for(i=0; i<output.baris; i++){
for(j=0; j<output.kolom; j++){
cout<<setw(5)<<output.M[i][j]<<" ";
}
cout<<endl;
}
return out;
}
main(){
matrix matrixA, kali;
int skalar;
cout<<"Masukkan Data Matrix"<<endl;
cin>>matrixA;
cout<<"Masukkan Skalar = ";
cin>>skalar;
kali.kali_matrix_skalar(matrixA, skalar);
cout<<kali;
getch();
return 0;
}
#include <conio.h>
#include <stdio.h>
#include <iomanip.h>
class matrix{
friend ostream& operator<<(ostream&, matrix&);
friend istream& operator>>(istream&, matrix&);
public :
void input_matrix();
void kali_matrix_skalar(const matrix&, int);
private:
int M[20][20];
int baris, kolom;
};
void matrix::input_matrix(){
int i,j;
for(i=0;i<baris;i++){
for(j=0;j<kolom;j++){
cout<<"Data ["<<(i+1)<<"] ["<<(j+1)<<"] = ";
cin>>M[i][j];
}
}
}
void matrix::kali_matrix_skalar(const matrix& matrixA, int skalar){
int i,j;
baris=matrixA.baris;
kolom=matrixA.kolom;
for(i=0;i<baris;i++){
for(j=0;j<kolom;j++){
M[i][j]=skalar*matrixA.M[i][j];
}
}
}
istream& operator>>(istream& in, matrix& input){
cout<<"Masukkan baris = ";
in>>input.baris;
cout<<"Masukkan kolom = ";
in>>input.kolom;
input.input_matrix();
return in;
}
ostream& operator<<(ostream& out, matrix& output){
int i,j;
for(i=0; i<output.baris; i++){
for(j=0; j<output.kolom; j++){
cout<<setw(5)<<output.M[i][j]<<" ";
}
cout<<endl;
}
return out;
}
main(){
matrix matrixA, kali;
int skalar;
cout<<"Masukkan Data Matrix"<<endl;
cin>>matrixA;
cout<<"Masukkan Skalar = ";
cin>>skalar;
kali.kali_matrix_skalar(matrixA, skalar);
cout<<kali;
getch();
return 0;
}
Label:
program C++
Program Determinan
#include<iostream.h>
class determinan{
friend istream& operator >>(istream&, determinan&);
friend ostream& operator <<(ostream&, const determinan&);
private :
int a,b,c,d,hasil;
public :
determinan() {};
int hasilnya(){return (hasil=((a * d) - (b * c)));}
~determinan() {};
};
istream& operator >>(istream& in, determinan& d){
cout<<"Masukkan bil 1- "; in>>d.a;
cout<<"Masukkan bil 2- "; in>>d.b;
cout<<"Masukkan bil 3- "; in>>d.c;
cout<<"Masukkan bil 4- "; in>>d.d;
cout<<d.a<<" "<<d.b<<endl;
cout<<d.c<<" "<<d.d<<endl;
return in;
}
ostream& operator <<(ostream& out, const determinan& d){
out<<"HAsil determinannnya "<<d.hasil<<endl;
return out;
}
int main(){
determinan x;
cin>>x;
x.hasilnya();
cout<<x;
return 0;
}
class determinan{
friend istream& operator >>(istream&, determinan&);
friend ostream& operator <<(ostream&, const determinan&);
private :
int a,b,c,d,hasil;
public :
determinan() {};
int hasilnya(){return (hasil=((a * d) - (b * c)));}
~determinan() {};
};
istream& operator >>(istream& in, determinan& d){
cout<<"Masukkan bil 1- "; in>>d.a;
cout<<"Masukkan bil 2- "; in>>d.b;
cout<<"Masukkan bil 3- "; in>>d.c;
cout<<"Masukkan bil 4- "; in>>d.d;
cout<<d.a<<" "<<d.b<<endl;
cout<<d.c<<" "<<d.d<<endl;
return in;
}
ostream& operator <<(ostream& out, const determinan& d){
out<<"HAsil determinannnya "<<d.hasil<<endl;
return out;
}
int main(){
determinan x;
cin>>x;
x.hasilnya();
cout<<x;
return 0;
}
Label:
program C++