Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime> //zur Laufzeitdarstellung
  4.  
  5. using namespace std;
  6.  
  7. int Ax, Ay, Bx, By;
  8.  
  9. void s_array() {
  10.  
  11. //LAUFZEIT-INIT
  12. clock_t start;
  13. double laufzeit;
  14. start = clock();
  15.  
  16. //MATRIX A
  17. ifstream fileA("A.txt");
  18. fileA >> Ax >> Ay;
  19. double arrayA[Ax][Ay];
  20.  
  21. for (int i = 0; i < Ax ; i++) {
  22. for (int j = 0; j < Ay ; j++) {
  23. fileA >> arrayA[i][j];
  24. //cout << arrayA[i][j] << " ";
  25. }
  26. }
  27. fileA.close();
  28.  
  29. //MATRIX B
  30. ifstream fileB("B.txt");
  31. fileB >> Bx >> By;
  32. double arrayB[Bx][By];
  33.  
  34. for (int j = 0; j < Bx ; j++) {
  35. for (int k = 0; k < By ; k++) {
  36. fileB >> arrayB[j][k];
  37. //cout << arrayB[j][k] << " ";
  38. }
  39. }
  40. fileB.close();
  41.  
  42. //MATRIXMULTIPLIKATION + DATEIAUSGABE
  43. ofstream fileC("sC.txt");
  44. double res=0;
  45.  
  46. for (int i = 0; i < Ax ; i++) {
  47. for (int k = 0; k < By ; k++) {
  48. for (int j = 0; j < Ay ; ++j) {
  49. res += arrayA[i][j] * arrayB[j][k];
  50. }
  51. fileC << res << " ";
  52. res=0;
  53. }
  54. fileC << "\n";
  55. }
  56. fileC.close();
  57.  
  58. //LAUFZEITBERECHNUNG
  59. laufzeit = (double)(clock() - start) / CLOCKS_PER_SEC;
  60. cout << "Laufzeit: " << laufzeit << " Sekunden" << endl;
  61.  
  62.  
  63. }
  64.  
  65. void d_array() {
  66.  
  67. //LAUFZEIT-INIT
  68. clock_t start;
  69. double laufzeit;
  70. start = clock();
  71.  
  72. //MATRIX A
  73. ifstream fileA("A.txt");
  74. fileA >> Ax >> Ay;
  75. double *arrayA = new double[Ax*Ay];
  76.  
  77. /*
  78. * Anstatt fuer jede Zeile einen neuen Speicherblock im Heap zu reservieren,
  79. * wird ein großer Block reserviert.
  80. * Zugriff auf Element a[x][y] erfolgt mit a[x*SPALTEN+y]
  81. * Dadurch muss am Ende auch nur ein Speicherblock wieder per delete freigegeben werden.
  82. */
  83.  
  84. for (int i = 0; i < Ax ; i++) {
  85. for (int j = 0; j < Ay ; j++) {
  86. fileA >> arrayA[i*Ay+j];
  87. }
  88. }
  89. fileA.close();
  90.  
  91. //MATRIX B
  92. ifstream fileB("B.txt");
  93. fileB >> Bx >> By;
  94. double *arrayB = new double[Bx*By];
  95.  
  96. for (int j = 0; j < Bx ; j++) {
  97. for (int k = 0; k < By ; k++) {
  98. fileB >> arrayB[j*By+k];
  99. }
  100. }
  101. fileB.close();
  102.  
  103. //MATRIXMULTIPLIKATION + DATEIAUSGABE
  104. ofstream fileC("dC.txt");
  105. double res=0;
  106.  
  107. for (int i = 0; i < Ax ; i++) {
  108. for (int k = 0; k < By ; k++) {
  109. for (int j = 0; j < Ay ; j++) {
  110. res += arrayA[i*Ay+j] * arrayB[j*By+k];
  111. }
  112. fileC << res << " ";
  113. res=0;
  114. }
  115. fileC << "\n";
  116. }
  117. fileC.close();
  118. delete [] arrayA; //da wie oben beschrieben nur ein einzelner Speicherblock pro Array reserviert wird,
  119. delete [] arrayB; //muss auch pro Array nur ein Block wieder freigegeben werden
  120.  
  121. //LAUFZEITBERECHNUNG
  122. laufzeit = (double)(clock() - start) / CLOCKS_PER_SEC;
  123. cout << "Laufzeit: " << laufzeit << " Sekunden" << endl;
  124.  
  125.  
  126. }
  127.  
  128. int main() {
  129. int typ;
  130.  
  131. cout << "\n\nWAEHLEN SIE ( 0 | 1 ): \n"
  132. << "0 - Matrixmultiplikation mit statischen Arrays \n"
  133. << "1 - Matrixmultiplikation mit dynamischen Arrays \n";
  134. cin >> typ;
  135.  
  136. if (typ == 0) {s_array();}
  137. if (typ == 1) {d_array();}
  138.  
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement