Advertisement
meta1211

Homework (указатели)

Nov 4th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1.
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <conio.h>
  7. #include <ctime>
  8.  
  9. void fill_array(int **a, int h, int w)
  10. {
  11. for(unsigned int k = 0; k < w; k++)
  12. for (unsigned int j = 0; j < h; j++)
  13. {
  14. a[k][j] = (rand() % 40) + 11;
  15. }
  16. }
  17.  
  18. void print_array(int **a, int h, int w)
  19. {
  20. for (unsigned int k = 0; k < w; k++)
  21. {
  22. printf("\n");
  23. for (unsigned int j = 0; j < h; j++)
  24. {
  25. printf("%d\t", a[k][j]);
  26. }
  27. }
  28. }
  29.  
  30. int main()
  31. {
  32. srand(time(0));
  33. int h, w;
  34. printf("input height and weight of array:\n");
  35. scanf("%d %d", &h, &w);
  36. int **a = new int*[w];
  37. for (unsigned int i = 0; i < w; i++)
  38. {
  39. a[i] = new int[h];
  40. }
  41. fill_array(a, h, w);
  42. print_array(a, h, w);
  43. for (int j = 0; j < h; j++)
  44. delete[]a[j];
  45. system("pause");
  46. return 0;
  47. }
  48. 2.
  49. #define _CRT_SECURE_NO_WARNINGS
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <conio.h>
  54. #include <ctime>
  55.  
  56.  
  57. //Заполнение массива
  58. void fill_array(int a[5][5])
  59. {
  60. for (unsigned int k = 0; k < 5; k++)
  61. for (unsigned int j = 0; j < 5; j++)
  62. {
  63. a[k][j] = (rand() % 80) + 10;
  64. }
  65. }
  66.  
  67.  
  68. //Сортировка по максимальному элементу
  69. void regulation(int a[5][5])
  70. {
  71. for (unsigned int k = 0; k < 5; k++)
  72. {
  73. int* max = &a[k][0];
  74. for (unsigned int j = 0; j < 5; j++)
  75. {
  76. if (a[k][j] > *max)
  77. {
  78. max = &a[k][j];
  79. }
  80. }
  81. int helper = a[k][0];
  82. a[k][0] = *max;
  83. *max = helper;
  84.  
  85. }
  86. }
  87.  
  88.  
  89. //Вывод текущей матрицы
  90. void print_array(int a[5][5])
  91. {
  92. for (unsigned int k = 0; k < 5; k++)
  93. {
  94. printf("\n");
  95. for (unsigned int j = 0; j < 5; j++)
  96. {
  97. printf("%d\t", a[k][j]);
  98. }
  99. }
  100. printf("\n\n");
  101. }
  102.  
  103.  
  104. int main()
  105. {
  106. //Начальные данные
  107. srand(time(0));
  108. int a[5][5];
  109. fill_array(a);
  110. print_array(a);
  111. //Обработка
  112. regulation(a);
  113. print_array(a);
  114. //
  115. system("pause");
  116. return 0;
  117. }
  118. 3.
  119. #define _CRT_SECURE_NO_WARNINGS
  120.  
  121. #include <stdio.h>
  122. #include <stdlib.h>
  123. #include <conio.h>
  124. #include <ctime>
  125.  
  126.  
  127. //Заполнение массива
  128. void fill_array(int **a, int h, int w)
  129. {
  130. for (unsigned int k = 0; k < w; k++)
  131. for (unsigned int j = 0; j < h; j++)
  132. {
  133. a[k][j] = (rand() % 9) + 1;
  134. }
  135. }
  136. //Вывод текущей матрицы
  137. void print_array(int **a, int h, int w)
  138. {
  139. for (unsigned int k = 0; k < w; k++)
  140. {
  141. printf("\n");
  142. for (unsigned int j = 0; j < h; j++)
  143. {
  144. printf("%d\t", a[k][j]);
  145. }
  146. }
  147. printf("\n");
  148. }
  149.  
  150.  
  151.  
  152. void multiply_arrays(int **firstArray, int firstWitdh, int firstHeight, int **secondArray, int secondWitdh, int secondHeight, int **newArray)
  153. {
  154. for (unsigned int i = 0; i < firstWitdh; i++)
  155. for (unsigned k = 0; k < secondHeight; k++)
  156. {
  157. int summ = 0;
  158. for (int j = 0; j < firstHeight; j++)
  159. {
  160. summ += firstArray[i][j] * secondArray[j][k];
  161. }
  162. newArray[i][k] = summ;
  163. }
  164. }
  165. /*
  166. for (int i = 0; i < 4; i++) a[4][3] x b[3][5]
  167. for (int k = 0; k < 5; k++)
  168. {
  169. int summ = 0;
  170. for (int j = 0; j < 3; j++)
  171. {
  172. summ += a[i][j] * b[j][k];
  173. }
  174. c[i][k] = summ;
  175. }
  176. */
  177.  
  178. int main()
  179. {
  180. //Начальные данные
  181. srand(time(0));
  182.  
  183. int w1, h1, w2, h2;
  184.  
  185. printf("input width and heights:\n");
  186. scanf("%d %d %d %d", &w1, &h1, &w2, &h2);
  187. int **a = new int*[w1];
  188. for (unsigned int i = 0; i < w1; i++)
  189. {
  190. a[i] = new int[h1];
  191. }
  192. int **b = new int*[w2];
  193. for (unsigned int i = 0; i < w2; i++)
  194. {
  195. b[i] = new int[h2];
  196. }
  197. int **c = new int*[w1];
  198. for (unsigned int i = 0; i < w1; i++)
  199. {
  200. c[i] = new int[h2];
  201. }
  202.  
  203. fill_array(a, h1, w1);
  204. fill_array(b, h2, w2);
  205. print_array(a, h1, w1);
  206. print_array(b, h2, w2);
  207. //
  208.  
  209. //Обработка
  210.  
  211. multiply_arrays(a, w1, h1, b, w2, h2, c);
  212. print_array(c, h2, w1);
  213. //завершение
  214.  
  215. for (int j = 0; j < w1; j++)
  216. delete[]a[j];
  217. for (int j = 0; j < w2; j++)
  218. delete[]b[j];
  219. for (int j = 0; j < w1; j++)
  220. delete[]c[j];
  221. system("pause");
  222. return 0;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement