Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define max 5
- using namespace std;
- typedef int SparseMatrix[max][max];
- typedef int Matrix3C[3][max];
- SparseMatrix S;
- Matrix3C M;
- int LastIdx, Pilih;
- int i, j;
- void InitMatrix3C(){
- LastIdx = 0;
- for(i = 0; i < max; i++){
- M[0][i] = 0; // Baris
- M[1][i] = 0; // Kolom
- M[2][i] = 0; // Nilai
- }
- }
- void AddSparse(int row, int col, int value){
- if(row > max || col > max){
- cout << "\n> Kolom / baris melebihi batas maksimal" << endl;
- }else{
- S[row][col] = value;
- cout << "\n> Elemen ditambahkan" << endl;
- }
- }
- void SparseMatrixToMatrix3C(){
- for(i = 0; i < max; i++){
- for(j = 0; j < max; j++){
- if(S[i][j] != 0){
- LastIdx++;
- M[0][LastIdx] = i;
- M[1][LastIdx] = j;
- M[2][LastIdx] = S[i][j];
- }
- }
- }
- cout << "\n> Sparse Matriks telah diubah !";
- }
- void SearchSMValue(int nilaiCari){
- bool found;
- i = 0;
- found = false;
- while(i < max && !found){
- if(M[2][i] == nilaiCari){
- found = true;
- }else{
- i++;
- }
- }
- if(found){
- cout << "\n> Nilai " << nilaiCari << " terletak pada Sparse Matrix baris ke-" << M[0][i] << " kolom ke-" << M[1][i];
- }else{
- cout << "\n> Nilai " << nilaiCari << " tidak ditemukan";
- }
- }
- void SearchSMPosition(int row, int col){
- bool found;
- i = 0;
- found = false;
- while(i <= max && !found){
- if(M[0][i] == row && M[1][i] == col){
- found = true;
- }else{
- i++;
- }
- }
- if(found){
- cout << "\n> Baris ke-" << row << " kolom ke-" << col << " bernilai : " << M[2][i];
- }else{
- cout << "\n> Baris ke-" << row << " kolom ke-" << col << " tidak ditemukan";
- }
- }
- void ViewSM(){
- for(i = 0; i < max; i++){
- for(j = 0; j < max; j++){
- cout << "Baris ke-" << i+1 << " kolom ke-" << j+1 << " = " << S[i][j] << endl;
- }
- }
- }
- int main(){
- InitMatrix3C();
- do{
- cout << "\n***************************\n";
- cout << "* M E N U U T A M A *\n";
- cout << "***************************\n";
- cout << "* 1. Add Sparse Matriks *\n";
- cout << "* 2. Ubah Sparse Matriks *\n";
- cout << "* 3. Cari Nilai *\n";
- cout << "* 4. Cari Posisi *\n";
- cout << "* 5. View *\n";
- cout << "* 6. Exit *\n";
- cout << "**************************\n";
- cout << "\nPilih Menu [1..5] : "; cin >> Pilih;
- switch(Pilih){
- case 1 :{
- int row, col, value;
- cout << "\nMasukan Baris : "; cin >> row;
- cout << "\nMasukan Kolom : "; cin >> col;
- cout << "\nMasukan Nilai : "; cin >> value;
- AddSparse(row, col, value);
- break;
- }
- case 2 :{
- SparseMatrixToMatrix3C();
- break;
- }
- case 3 :{
- int nilaiCari;
- cout << "\nMasukan nilai yang ingin di cari : "; cin >> nilaiCari;
- SearchSMValue(nilaiCari);
- break;
- }
- case 4 :{
- int barisCari, kolomCari;
- cout << "\nMasukan baris dan kolom yang ingin dicari nilai nya :\n";
- cout << " Baris : "; cin >> barisCari;
- cout << " Kolom : "; cin >> kolomCari;
- SearchSMPosition(barisCari, kolomCari);
- break;
- }
- case 5 : {
- ViewSM();
- break;
- }
- case 6 : cout << "\n> End of Application!!!"; break;
- default : cout << "\n> Salah pilih !!!"; break;
- }
- }while (Pilih >= 1 && Pilih <= 4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement