Jumat, 08 April 2016

BAB 9 Nomor 4

Membuat program untuk mengalikan suatu matriks dengan konstanta.

Menggunakan C++ :

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void Masukan(int *q, int brs, int klm) {
    int i, j;
    for(i = 0; i < brs; i++) {
        for(j = 0; j < klm; j++) {
            cout << " Elemen [" << (i + 1) << "][" << (j + 1) << "]? ";
            scanf("%d", (q + i * klm + j));
        }
    }
}

void Tampilkan(int *q, int brs, int klm) {
    int i, j;
    for(i = 0; i < brs; i++) {
        cout << endl;
        for(j = 0; j < klm; j++) {
            cout <<" ";
            cout << *(q + i * klm + j) << "\t";
        }
    }
    cout << endl;
}

void Kali(int *p, int *q, int *z, int bA, int kA, int bB, int kB) {
    int i, j, k;
    for(i = 0; i < bA; i++) {
        for(j = 0; j < kB; j++) {
            *(z + i * bB + j) = 0;
            for(k = 0; k < kA; k++) {
                *(z + i * bB + j) += *(p + i * kA + k) * (*(q + k * kB + j));
            }
        }
    }
}

int main() {
cout<<"\t\t\tPROGRAM    : PERKALIAN MATRIKS DENGAN SKALAR\n";
awal:
    int *a, *b, *c, brsA, klmA, brsB, klmB;
    cout<<"\n SYARAT...!!!\n";
    cout<<" Jumlah Kolom A HARUS SAMA dengan Baris B\n\n";
    cout << " Jumlah Baris Matriks A? ";
    cin >> brsA;
    cout << " Jumlah Kolom Matriks A? ";
    cin >> klmA;
    cout << " Jumlah Baris Matriks B? ";
    cin >> brsB;
    cout << " Jumlah Kolom Matriks B? ";
    cin >> klmB;
    if(klmA != brsB) {
        cout << " Perkalian TIDAK Dapat Dilakukan." << endl;
        cout << " Jumlah Kolom A TIDAK Sama Dengan Jumlah Baris B." << endl;
    } else {
        a = (int *) malloc(brsA * klmA * 4);
        b = (int *) malloc(brsB * klmB * 4);
        c = (int *) malloc(brsA * klmB * 4);

        cout << "\n Kordinat Inputan [y][x]" ;
        cout << "\n Contoh: ordo 2x2" ;
        cout << "\n [1][1]\t[1][2]\t" ;
        cout << "\n [2][1]\t[2][2]\t" ;
        cout << "\n\n Input Matriks A:\n" ;
        cout << " ----------------\n";
        Masukan(a, brsA, klmA);
        cout << "\n Input Matriks B:\n" ;
        cout << " ----------------\n";
        Masukan(b, brsB, klmB);

        cout << "\n Matriks A:" << endl;
        Tampilkan(a, brsA, klmA);
        cout << "\n Matriks B:" << endl;
        Tampilkan(b, brsB, klmB);
        Kali(a, b, c, brsA, klmA, brsB, klmB);
        cout << "\n Matriks A x B:" << endl;
        Tampilkan(c, brsA, klmB);
    }
cout<<"\n\n Apakah Anda Ingin Mengulang [Y/N] ? "  ;
char m ;
cin>>m;
if (m == 'y' || m== 'Y')
goto awal;
else if (m == 'n' || m== 'N')
{
goto akhir;
}
akhir:
cout<<"\n\n\t\t\t    \"TERMA KASIH\"";
    return 0;
}

output :


0 komentar:

Posting Komentar

Popular Posts

Recent Posts

Unordered List

Text Widget

Kategori

Kategori

Diberdayakan oleh Blogger.

Recent Comments

Looped Slider

Formulir Kontak

Nama

Email *

Pesan *

Find Us On Facebook

Advertisement

Featured Video

Featured Video

Sponsor

Gagdet

Video Of Day

Gallery

Sports

Popular Posts