po1ska

Untitled

Sep 17th, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<vector<pair<int, float>>> numbers; vector<float> multVector;
  6.  
  7. void matrixVectorMult(){
  8.  
  9. printf("-%d-", numbers.size());
  10.  
  11. float finalResult[numbers.size()] = {0};
  12. int partialRes=0;
  13. for (int i = 0; i < numbers.size(); i++) {
  14. for (int j = 0; j < numbers[i].size(); j++){
  15. finalResult[i] += numbers[i][j].second*multVector[numbers[i][j].first];
  16. }
  17. }
  18. for(int i=0;i<numbers.size(); i++){
  19. cout << finalResult[i] << endl;
  20. }
  21.  
  22. }
  23.  
  24. void matrixMatrixMulti(vector<vector<pair<int, float>>> &numbers, vector<vector<pair<int, float>>> &source, int maxRow, int maxColumn){
  25. int finalRes[maxRow][maxColumn];
  26. for(int i=0; i<maxRow;i++){
  27. for(int j=0; j < maxColumn; j++){
  28. finalRes[i][j] = 0;
  29. }
  30. }
  31.  
  32. for(int i=0; i < numbers.size(); i++){
  33. for(int k=0; k < numbers[i].size(); k++){
  34. for(int j=0; j < source[numbers[i][k].first].size(); j++){ // erro em source[k]
  35. //if(j >= source[numbers[i][k].first].size()){break;}
  36. finalRes[i][source[numbers[i][k].first][j].first] += numbers[i][k].second*source[numbers[i][k].first][j].second;
  37. }
  38. }
  39. }
  40.  
  41. for(int i=0;i<maxRow;i++){
  42. for(int j=0; j<maxColumn;j++){
  43. printf("%d ", finalRes[i][j]);
  44. }
  45. printf("\n");
  46. }
  47. }
  48.  
  49. int main(){
  50.  
  51. int linha, col, linMt2, colMt2, k=0, z=0, opcao;
  52. system("cls");
  53. printf("Hello mate:)\n\n");
  54.  
  55. while(1){
  56. printf("Choose an operation : \n\n -1- (Sparse Matrix - Vector Multiplication) \n -2- (Sparse - Sparse Matrix Multiplication) \n -3- (Sparse - Dense Matrix Multiplication)\n -0- (Exit)\n\nOption : ");
  57. scanf("%d", &opcao);
  58. system("cls");
  59.  
  60. switch(opcao){
  61.  
  62. case 1:
  63.  
  64. {
  65. int op1;
  66. vector<float> multVector = {1,2,3,4};
  67. cin >> linha >> col;
  68.  
  69. float matrizEsp[linha][col];
  70.  
  71. pair<int, float> testing;
  72. vector<vector<pair<int, float>>> numbers;
  73.  
  74. for(int i=0; i<linha;i++){
  75. vector<pair<int, float>> testVector;
  76. for(int j=0; j<col; j++){
  77. scanf("%f", &matrizEsp[i][j]);
  78. if(matrizEsp[i][j] != 0){
  79. testing.first = j;
  80. testing.second = matrizEsp[i][j];
  81. testVector.push_back(testing);
  82. }
  83. //printf("%d - %f\n", testing.first, testing.second);
  84. }
  85. numbers.push_back(testVector);
  86. }
  87. printf("--%d--", numbers.size());
  88. matrixVectorMult();
  89. printf("\n-Any number- : Menu\n");
  90. if(scanf("%d", &op1)){
  91. system("cls");
  92. break;
  93. }
  94.  
  95. }
  96.  
  97. case 2: case 3:
  98.  
  99. {
  100. int op2;
  101. cin >> linha >> col;
  102. float matrizEsp[linha][col];
  103.  
  104. pair<int, float> testing;
  105. vector<vector<pair<int, float>>> numbers;
  106.  
  107. for(int i=0; i<linha;i++){
  108. vector<pair<int, float>> testVector;
  109. for(int j=0; j<col; j++){
  110. scanf("%f", &matrizEsp[i][j]);
  111. if(matrizEsp[i][j] != 0){
  112. testing.first = j;
  113. testing.second = matrizEsp[i][j];
  114. testVector.push_back(testing);
  115. }
  116. //printf("%d - %f\n", testing.first, testing.second);
  117. }
  118. numbers.push_back(testVector);
  119. }
  120.  
  121. cin >> linMt2 >> colMt2;
  122. vector<vector<pair<int, float>>> segNumbers;
  123.  
  124. for(int i=0; i<linMt2;i++){
  125. vector<pair<int, float>> testVector;
  126. for(int j=0; j<colMt2; j++){
  127. scanf("%f", &matrizEsp[i][j]);
  128. if(matrizEsp[i][j] != 0){
  129. testing.first = j;
  130. testing.second = matrizEsp[i][j];
  131. testVector.push_back(testing);
  132. }
  133. //printf("%d - %f\n", testing.first, testing.second);
  134. }
  135. segNumbers.push_back(testVector);
  136. }
  137. matrixMatrixMulti(numbers, segNumbers, linha, colMt2);
  138. printf("\n(Any number) - Menu\n");
  139. if(scanf("%d", &op2)){
  140. system("cls");
  141. break;
  142. }
  143. }
  144.  
  145. default:
  146. printf("Choose an right operation :(\n");
  147. }
  148.  
  149. };
  150. /*for (int i = 0; i < numbers.size(); i++) {
  151. for (int j = 0; j < numbers[i].size(); j++)
  152. printf("%d,%.1f", numbers[i][j].first,numbers[i][j].second);
  153. cout << endl;
  154. }
  155. */
  156.  
  157. return 0;
  158. }
  159.  
  160.  
Advertisement
Add Comment
Please, Sign In to add comment