Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #include <iostream>
  2. #define max 5
  3. using namespace std;
  4.  
  5. typedef int SparseMatrix[max][max];
  6. typedef int Matrix3C[3][max];
  7. SparseMatrix S;
  8. Matrix3C M;
  9. int LastIdx, Pilih;
  10. int i, j;
  11.  
  12. void InitMatrix3C(){
  13. LastIdx = 0;
  14.  
  15. for(i = 0; i < max; i++){
  16. M[0][i] = 0; // Baris
  17. M[1][i] = 0; // Kolom
  18. M[2][i] = 0; // Nilai
  19. }
  20. }
  21.  
  22. void AddSparse(int row, int col, int value){
  23. if(row > max || col > max){
  24. cout << "\n> Kolom / baris melebihi batas maksimal" << endl;
  25. }else{
  26. S[row][col] = value;
  27. cout << "\n> Elemen ditambahkan" << endl;
  28. }
  29. }
  30.  
  31. void SparseMatrixToMatrix3C(){
  32. for(i = 0; i < max; i++){
  33. for(j = 0; j < max; j++){
  34. if(S[i][j] != 0){
  35. LastIdx++;
  36. M[0][LastIdx] = i;
  37. M[1][LastIdx] = j;
  38. M[2][LastIdx] = S[i][j];
  39. }
  40. }
  41. }
  42. cout << "\n> Sparse Matriks telah diubah !";
  43. }
  44.  
  45. void SearchSMValue(int nilaiCari){
  46. bool found;
  47.  
  48. i = 0;
  49. found = false;
  50.  
  51. while(i < max && !found){
  52. if(M[2][i] == nilaiCari){
  53. found = true;
  54. }else{
  55. i++;
  56. }
  57. }
  58. if(found){
  59. cout << "\n> Nilai " << nilaiCari << " terletak pada Sparse Matrix baris ke-" << M[0][i] << " kolom ke-" << M[1][i];
  60. }else{
  61. cout << "\n> Nilai " << nilaiCari << " tidak ditemukan";
  62. }
  63. }
  64.  
  65. void SearchSMPosition(int row, int col){
  66. bool found;
  67.  
  68. i = 0;
  69. found = false;
  70.  
  71. while(i <= max && !found){
  72. if(M[0][i] == row && M[1][i] == col){
  73. found = true;
  74. }else{
  75. i++;
  76. }
  77. }
  78. if(found){
  79. cout << "\n> Baris ke-" << row << " kolom ke-" << col << " bernilai : " << M[2][i];
  80. }else{
  81. cout << "\n> Baris ke-" << row << " kolom ke-" << col << " tidak ditemukan";
  82. }
  83. }
  84.  
  85. void ViewSM(){
  86. for(i = 0; i < max; i++){
  87. for(j = 0; j < max; j++){
  88. cout << "Baris ke-" << i+1 << " kolom ke-" << j+1 << " = " << S[i][j] << endl;
  89. }
  90. }
  91. }
  92.  
  93. int main(){
  94. InitMatrix3C();
  95. do{
  96. cout << "\n***************************\n";
  97. cout << "* M E N U U T A M A *\n";
  98. cout << "***************************\n";
  99. cout << "* 1. Add Sparse Matriks *\n";
  100. cout << "* 2. Ubah Sparse Matriks *\n";
  101. cout << "* 3. Cari Nilai *\n";
  102. cout << "* 4. Cari Posisi *\n";
  103. cout << "* 5. View *\n";
  104. cout << "* 6. Exit *\n";
  105. cout << "**************************\n";
  106. cout << "\nPilih Menu [1..5] : "; cin >> Pilih;
  107. switch(Pilih){
  108. case 1 :{
  109. int row, col, value;
  110. cout << "\nMasukan Baris : "; cin >> row;
  111. cout << "\nMasukan Kolom : "; cin >> col;
  112. cout << "\nMasukan Nilai : "; cin >> value;
  113. AddSparse(row, col, value);
  114. break;
  115. }
  116. case 2 :{
  117. SparseMatrixToMatrix3C();
  118. break;
  119. }
  120. case 3 :{
  121. int nilaiCari;
  122. cout << "\nMasukan nilai yang ingin di cari : "; cin >> nilaiCari;
  123. SearchSMValue(nilaiCari);
  124. break;
  125. }
  126. case 4 :{
  127. int barisCari, kolomCari;
  128. cout << "\nMasukan baris dan kolom yang ingin dicari nilai nya :\n";
  129. cout << " Baris : "; cin >> barisCari;
  130. cout << " Kolom : "; cin >> kolomCari;
  131. SearchSMPosition(barisCari, kolomCari);
  132. break;
  133. }
  134. case 5 : {
  135. ViewSM();
  136. break;
  137. }
  138.  
  139. case 6 : cout << "\n> End of Application!!!"; break;
  140. default : cout << "\n> Salah pilih !!!"; break;
  141. }
  142. }while (Pilih >= 1 && Pilih <= 4);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement