Buatlah program untuk memasukan angka random kedalam matrik berdimensi 2, kemudian tampilalkan matrik tersebut : Urut pada kolom yang sama, Urut pada baris yang sama, Urut dari elemen pertama samapai terakhir
Contoh Scrip C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream.h> | |
#include <iomanip.h> | |
#include <conio.h> | |
#include <stdlib.h> | |
int main(){ | |
randomize(); | |
int matrik[10][10],matrikBaris[10][10],matrikKolom[10][10],matrikBarisKolom[10][10]; | |
int brs,col,brsInput,colInput; | |
cout<<"KlikArtikel.com"<<endl; | |
cout<<"Belajar Bersama C++"<<endl; | |
cout<<"Penggunaan Matrik Pada C++"<<endl; | |
cout<<"========================= "<<endl<<endl; | |
cout<<"Inputkan Jumlah Baris : ";cin>>brsInput; | |
cout<<"Inputkan Jumlah Kolom : ";cin>>colInput; | |
cout<<"========================= "<<endl; | |
//membuat data random | |
cout<<"Data Awal"<<endl; | |
for(int i = 0; i<=colInput;i++) | |
{ | |
cout<<"-------"; | |
} | |
cout<<endl; | |
for(brs=0;brs<brsInput;brs++) | |
{ | |
cout<<"| "; | |
for(col=0;col<colInput;col++) | |
{ | |
matrik[brs][col] = random(100); | |
matrikBaris[brs][col] = matrikKolom[brs][col] = matrik[brs][col]; | |
cout<<setw(4)<<matrik[brs][col]<<" "; | |
} | |
cout<<" |"<<endl; | |
} | |
//mengurutkan baris | |
for(int i = 0; i<=colInput;i++) | |
{ | |
cout<<"-------"; | |
} | |
cout<<endl<<endl; | |
for (brs=0;brs<brsInput;brs++) | |
{ | |
for(col=0; col<colInput; col++) | |
{ | |
for(int j=col+1; j<colInput; j++) | |
{ | |
if(matrikBaris[brs][col]>matrikBaris[brs][j]) | |
{ | |
int tmp = matrikBaris[brs][col]; | |
matrikBaris[brs][col] = matrikBaris[brs][j]; | |
matrikBaris[brs][j] = tmp; | |
} | |
} | |
} | |
} | |
cout<<"Data setelah diurutkan baris : "<<endl; | |
for(int i = 0; i<=colInput;i++) | |
{ | |
cout<<"-------"; | |
} | |
cout<<endl; | |
for(brs=0; brs<brsInput; brs++) | |
{ | |
cout<<"| "; | |
for(col=0; col<colInput; col++) | |
{ | |
cout<<setw(4)<<matrikBaris[brs][col]<<" "; | |
} | |
cout<<" |"<<endl; | |
} | |
//mengurutkan berdasarkan kolom | |
for(int i = 0; i<=colInput;i++) | |
{ | |
cout<<"-------"; | |
} | |
cout<<endl<<endl; | |
for (col=0;col<colInput;col++) | |
{ | |
for(brs=0; brs<brsInput; brs++) | |
{ | |
for(int j=brs+1; j<brsInput; j++) | |
{ | |
if(matrikKolom[brs][col]>matrikKolom[j][col]) | |
{ | |
int tmp = matrikKolom[brs][col]; | |
matrikKolom[brs][col] = matrikKolom[j][col]; | |
matrikKolom[j][col] = tmp; | |
} | |
} | |
} | |
} | |
cout<<"Data setelah diurutkan kolom : "<<endl; | |
for(int i = 0; i<=colInput;i++) | |
{ | |
cout<<"-------"; | |
} | |
cout<<endl; | |
for(brs=0; brs<brsInput; brs++) | |
{ | |
cout<<"| "; | |
for(col=0; col<colInput; col++) | |
{ | |
cout<<setw(4)<<matrikKolom[brs][col]<<" "; | |
} | |
cout<<" |"<<endl; | |
} | |
for(int i = 0; i<=colInput;i++) | |
{ | |
cout<<"-------"; | |
} | |
cout<<endl<<endl; | |
cout<<endl; | |
//mengurutkan elementer | |
//menjabarkan menjadi 1 baris | |
cout<<"Data setelah diurutkan elementnya: "<<endl; | |
brs = 0; | |
col = 0; | |
int x = 0; | |
int panjangBrsCol = (brsInput*colInput)-1; | |
for (int i=0;i<=panjangBrsCol;i++) | |
{ | |
if(x == colInput) | |
{ | |
x = 0; | |
brs++; | |
col = 0; | |
} | |
matrikBarisKolom[0][i] = matrik[brs][col]; | |
col++; | |
x++; | |
} | |
for(int i = 0; i<=panjangBrsCol;i++) | |
{ | |
cout<<matrikBarisKolom[0][i]<<" "; | |
} | |
cout<<endl; | |
//mengurutkan baris | |
for (col=0;col<=panjangBrsCol;col++) | |
{ | |
for(int j=col+1; j<=panjangBrsCol; j++) | |
{ | |
if(matrikBarisKolom[0][col]>matrikBarisKolom[0][j]) | |
{ | |
int tmp = matrikBarisKolom[0][col]; | |
matrikBarisKolom[0][col] = matrikBarisKolom[0][j]; | |
matrikBarisKolom[0][j] = tmp; | |
} | |
} | |
} | |
//menjadikan data ke dalam baris dan kolom | |
brs = 0; | |
col = 0; | |
x = 0; | |
for(int i = 0; i<=panjangBrsCol;i++) | |
{ | |
cout<<matrikBarisKolom[0][i]<<" "; | |
} | |
cout<<endl; | |
for(int i = 0; i<=colInput;i++) | |
{ | |
cout<<"-------"; | |
} | |
cout<<endl; | |
cout<<"| "; | |
for(int i = 0; i<=panjangBrsCol; i++) | |
{ | |
if(x == colInput) | |
{ | |
x = 0; | |
brs++; | |
col = 0; | |
cout<<" |"<<endl; | |
cout<<"| "; | |
} | |
col++; | |
x++; | |
cout<<setw(4)<<matrikBarisKolom[0][i]<<" "; | |
} | |
cout<<" |"<<endl; | |
for(int i = 0; i<=colInput;i++) | |
{ | |
cout<<"-------"; | |
} | |
getch(); | |
} |
Komentar
Posting Komentar
Kritik, Saran dan Komentar Kami tunggu