- Diberikan suatu matriks berordo n. Buatlah algoritma dan program untuk mencetak matriks identitas berodo n.
source code :
#include"iostream"
using namespace std;
main(){ int i,j,n;
cout<<"masukkan ordo matriks : "; cin>>n;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(i==j) cout<<"1 ";
else cout<<"0 ";
}
cout<<endl;
}
}
- Buatlah program untuk menghasilkan transpose suatu matriks.
source code :
#include
<iostream>
using namespace std;
main(){ int i,j,m,n,o;
cout<<"Program transpose matriks C(m,n)\nMasukkan nilai m : ";
cin>>m; cout<<"masukkan nilai n : "; cin>>n;
int mat[m][n];
for(i=0;i<m;i++){
for(j=0;j<n;j++){
cout<<"nilai C("<<i+1<<","<<j+1<<")=";
cin>>mat[i][j];
}
cout<<endl;
}
system("cls");
cout<<"matriks diatas diilustrasikan seperti gambaran dibawah ini\n\n";
for(i=0;i<m;i++){
for(j=0;j<n;j++){
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
o=m; m=n; n=o;
cout<<"\nTranspose matriksnya adalah seperti gambar dibawah ini\n\n";
for(j=0;j<m;j++){
for(i=0;i<n;i++){
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
using namespace std;
main(){ int i,j,m,n,o;
cout<<"Program transpose matriks C(m,n)\nMasukkan nilai m : ";
cin>>m; cout<<"masukkan nilai n : "; cin>>n;
int mat[m][n];
for(i=0;i<m;i++){
for(j=0;j<n;j++){
cout<<"nilai C("<<i+1<<","<<j+1<<")=";
cin>>mat[i][j];
}
cout<<endl;
}
system("cls");
cout<<"matriks diatas diilustrasikan seperti gambaran dibawah ini\n\n";
for(i=0;i<m;i++){
for(j=0;j<n;j++){
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
o=m; m=n; n=o;
cout<<"\nTranspose matriksnya adalah seperti gambar dibawah ini\n\n";
for(j=0;j<m;j++){
for(i=0;i<n;i++){
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
- Buatlah program mengalikan matriks dengan suatu skalar (konstanta).
Dev C++
source code :
source code :
#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;
}