Advertisement
CzarnyBarszcz

Untitled

Mar 29th, 2020
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cstdio>
  6. using namespace std;
  7.  
  8. //Michaล‚ Szuleta
  9. void utworz(double **&M,const int m,const int n)
  10. {
  11. M = new double*[m];
  12. for(int i=0;i<m;i++)
  13. {
  14. M[i]=new double [n];
  15. }
  16. for (int i=0;i<m;i++)
  17. {
  18. for(int j=0;j<n;j++)
  19. {
  20. M[i][j]=0;
  21. }
  22. }
  23. }
  24. void zapisz(double **M,const unsigned int m,const unsigned int n)
  25. {
  26. cout<<m<<"x"<<n<<endl;
  27. cout<<endl;
  28. for(unsigned int i=0;i<m;i++)
  29. {
  30. for(unsigned int j=0;j<n;j++)
  31. {
  32. cout<<M[i][j]<<" ";
  33. }
  34. cout<<endl;
  35. }
  36. cout<<endl;
  37. }
  38. void usun(double **M,const int m)
  39. {
  40. for(int i=0;i<m;i++)
  41. {
  42. delete [] M[m];
  43. }
  44. delete [] M;
  45. }
  46. bool czytaj(double **&M,unsigned int &m,unsigned int &n,double **&N,unsigned int &p,unsigned &q)
  47. {
  48. cout<<"Macierz A!"<<endl;
  49. cout<<"Liczba wierszy :";
  50. cin>>m;
  51. cout<<"Liczba kolumn :";
  52. cin>>n;
  53. utworz(M,m,n);
  54. for(unsigned int i=0;i<m;i++)
  55. {
  56. for(unsigned int j=0;j<n;j++)
  57. {
  58. cin>>M[i][j];
  59. }
  60. }
  61. cout<<"Macierz B!"<<endl;
  62. cout<<"Liczba wierszy :";
  63. cin>>p;
  64. cout<<"Liczba kolumn :";
  65. cin>>q;
  66. utworz(N,p,q);
  67. for(unsigned int i=0;i<p;i++)
  68. {
  69. for(unsigned int j=0;j<q;j++)
  70. {
  71. cin>>N[i][j];
  72. }
  73. }
  74.  
  75. return true;
  76. }
  77. bool czytaj_losowo(double **&M,unsigned int &m,unsigned int &n,double **&N,unsigned int &p,unsigned &q)
  78. {
  79. cout<<"Macierz A!"<<endl;
  80. cout<<"Liczba wierszy :";
  81. cin>>m;
  82. cout<<"Liczba kolumn :";
  83. cin>>n;
  84. for(unsigned int i=0;i<m;i++)
  85. {
  86. for(unsigned int j=0;j<n;j++)
  87. {
  88. M[i][j]=rand()%10+1;
  89. }
  90. }
  91. cout<<"Macierz B!"<<endl;
  92. cout<<"Liczba wierszy :";
  93. cin>>p;
  94. cout<<"Liczba kolumn :";
  95. cin>>q;
  96. utworz(M,m,n);
  97. utworz(N,p,q);
  98. for(unsigned int i=0;i<p;i++)
  99. {
  100. for(unsigned int j=0;j<q;j++)
  101. {
  102. N[i][j]=rand()%10+1;
  103. }
  104. }
  105.  
  106. return true;
  107. }
  108. bool suma(double **M,unsigned int m,unsigned int n,double **N,unsigned int p,unsigned int q,double **&C)
  109. {
  110. utworz(C,m,n);
  111. for(unsigned int i=0;i<n;i++)
  112. {
  113. for(unsigned int j=0;j<n;j++)
  114. {
  115. C[i][j]=M[i][j]+N[i][j];
  116. }
  117. }
  118. return true;
  119. }
  120.  
  121. int main()
  122. {
  123. double **C = 0;
  124. srand(time(NULL));
  125. double **A = 0, **B = 0;
  126. unsigned int n = 2, m = 3, p = 3, q = 2;
  127. /* if (czytaj_losowo(A, m, n, B, p, q)) {
  128. zapisz(A, m, n);
  129. zapisz(B, p, q);
  130. usun(A,m);
  131. usun(B,q);
  132. }*/
  133. if (czytaj(A, m, n, B, p, q)) {
  134. zapisz(A, m, n);
  135. zapisz(B, p, q);
  136. if(!suma(A, m, n, B, p, q, C)){
  137. cerr << "Macierze maja nieprawidlowe wymiary - suma niemozliwa" << endl;
  138. }
  139. else{
  140. cout << "suma macierzy" << endl;
  141. zapisz(C, m, n);
  142. usun(C,m);
  143.  
  144. }
  145. /*
  146. if(!roznica(A, m, n, B, p, q, C)){
  147. cerr << "Macierze maja nieprawidlowe wymiary - roznica niemozliwa" << endl;
  148. }
  149. else{
  150. cout << "roznica macierzy" << endl;
  151. zapisz(C, m, n);
  152. usun(A,m);
  153. usun(B,q);
  154. usun(C,m);
  155. }*/
  156. }
  157. getch();
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement