Advertisement
CzarnyBarszcz

Untitled

Apr 2nd, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <conio.h>
  5. using namespace std;
  6.  
  7. //Michal Szuleta
  8. void utworz(double **&M,const int m,const int n)
  9. {
  10. M = new double*[m];
  11. for(int i=0;i<m;i++)
  12. {
  13. M[i]=new double [n];
  14. }
  15. for (int i=0;i<m;i++)
  16. {
  17. for(int j=0;j<n;j++)
  18. {
  19. M[i][j]=0;
  20. }
  21. }
  22. }
  23. void zapisz(double **M,const unsigned int m,const unsigned int n)
  24. {
  25. cout<<m<<"x"<<n<<endl;
  26. cout<<endl;
  27. for(unsigned int i=0;i<m;i++)
  28. {
  29. for(unsigned int j=0;j<n;j++)
  30. {
  31. cout<<M[i][j]<<" ";
  32. }
  33. cout<<endl;
  34. }
  35. cout<<endl;
  36. }
  37. void usun(double **M,const int m)
  38. {
  39. for(int i=0;i<m;i++)
  40. {
  41. delete [] M[m];
  42. }
  43. delete [] M;
  44. }
  45. bool czytaj(double **&M,unsigned int &m,unsigned int &n,double **&N,unsigned int &p,unsigned &q)
  46. {
  47. cout<<"Macierz A!"<<endl;
  48. cout<<"Liczba wierszy :";
  49. cin>>m;
  50. cout<<"Liczba kolumn :";
  51. cin>>n;
  52. utworz(M,m,n);
  53. for(unsigned int i=0;i<m;i++)
  54. {
  55. for(unsigned int j=0;j<n;j++)
  56. {
  57. cin>>M[i][j];
  58. }
  59. }
  60. cout<<"Macierz B!"<<endl;
  61. cout<<"Liczba wierszy :";
  62. cin>>p;
  63. cout<<"Liczba kolumn :";
  64. cin>>q;
  65. utworz(N,p,q);
  66. for(unsigned int i=0;i<p;i++)
  67. {
  68. for(unsigned int j=0;j<q;j++)
  69. {
  70. cin>>N[i][j];
  71. }
  72. }
  73.  
  74. return true;
  75. }
  76. bool czytaj_losowo(double **&M,unsigned int &m,unsigned int &n,double **&N,unsigned int &p,unsigned &q)
  77. {
  78. cout<<"Macierz A!"<<endl;
  79. cout<<"Liczba wierszy :";
  80. cin>>m;
  81. cout<<"Liczba kolumn :";
  82. cin>>n;
  83. utworz(M,m,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(N,p,q);
  97. for(unsigned int i=0;i<p;i++)
  98. {
  99. for(unsigned int j=0;j<q;j++)
  100. {
  101. N[i][j]=rand()%10+1;
  102. }
  103. }
  104.  
  105. return true;
  106. }
  107. bool suma(double **M,unsigned int m,unsigned int n,double **N,unsigned int p,unsigned int q,double **&C)
  108. {
  109. if(m==p&&n==q)
  110. {
  111.  
  112. utworz(C,m,n);
  113. for(unsigned int i=0;i<n;i++)
  114. {
  115. for(unsigned int j=0;j<n;j++)
  116. {
  117. C[i][j]=M[i][j]+N[i][j];
  118. }
  119. }
  120. return true;
  121. }
  122. else
  123. return false;
  124. }
  125. bool roznica(double **M,unsigned int m,unsigned int n,double **N,unsigned int p,unsigned int q,double **&C)
  126. {
  127. if(m==p&&n==q)
  128. {
  129.  
  130. utworz(C,m,n);
  131. for(unsigned int i=0;i<n;i++)
  132. {
  133. for(unsigned int j=0;j<n;j++)
  134. {
  135. C[i][j]=M[i][j]-N[i][j];
  136. }
  137. }
  138. return true;
  139. }
  140. else
  141. return false;
  142. }
  143. bool iloczyn(double **M,unsigned int m,unsigned int n,double **N,unsigned int p,unsigned int q,double **&C)
  144. {
  145. if(n==p)
  146. {
  147.  
  148. utworz(C,m,n);
  149. for(unsigned int i=0;i<m;i++)
  150. {
  151. for(unsigned int k=0;k<q;k++)
  152. {
  153. for(unsigned int j=0;j<n;j++)
  154. {
  155. C[i][k]+=M[i][j]*N[j][k];
  156. }
  157. }
  158.  
  159. }
  160. return true;
  161. }
  162. else
  163. return false;
  164. }
  165. void transpose(double **N,unsigned int &p, unsigned &q,double **&C)
  166. {
  167. utworz(C,q,p);
  168. for(unsigned int i=0;i<p;i++)
  169. {
  170. for(unsigned int j=0;j<q;j++)
  171. {
  172. C[j][i]=N[i][j];
  173. }
  174. }
  175. }
  176. int main()
  177. {
  178.  
  179. double **C = 0;
  180. srand(time(NULL));
  181. double **A = 0, **B = 0;
  182. unsigned int n = 2, m = 3, p = 3, q = 2;
  183. if (czytaj_losowo(A, m, n, B, p, q)) {
  184. zapisz(A, m, n);
  185. zapisz(B, p, q);
  186. usun(A,m);
  187. usun(B,q);
  188. }
  189.  
  190.  
  191. if (czytaj(A, m, n, B, p, q)) {
  192. zapisz(A, m, n);
  193. zapisz(B, p, q);
  194. if(!suma(A, m, n, B, p, q, C)){
  195. cerr << "Macierze maja nieprawidlowe wymiary - suma niemozliwa" << endl;
  196. }
  197. else{
  198. cout << "suma macierzy" << endl;
  199. zapisz(C, m, n);
  200. usun(C,m);
  201. }
  202. if(!roznica(A, m, n, B, p, q, C)){
  203. cerr << "Macierze maja nieprawidlowe wymiary - roznica niemozliwa" << endl;
  204. }
  205. else{
  206. cout << "roznica macierzy" << endl;
  207. zapisz(C, m, n);
  208. usun(C,m);
  209. }
  210. if(!iloczyn(A, m, n, B, p, q, C)){
  211. cerr << "Macierze maja nieprawidlowe wymiary - iloczyn niemozliwy" << endl;
  212. }
  213. else{
  214. cout << "iloczyn macierzy" << endl;
  215. zapisz(C, m, q);
  216. usun(C,m);
  217. }
  218. cout << "macierz przed transponowaniem" << endl;
  219. zapisz(B, p, q);
  220. cout << "macierz transponowana" << endl;
  221. transpose(B, p, q, C);
  222. zapisz(C, q, p);
  223. usun(C,m);
  224. }
  225. usun(A,m);
  226. usun(B,p);
  227. getch();
  228. return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement