Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <cstdlib>
  5. #include <ctime>
  6. using namespace std;
  7.  
  8. const int n = 10;
  9. int max[2];
  10.  
  11. DWORD WINAPI ThreadProc1(LPVOID lpParam)
  12. {
  13. int max1 = 0;
  14. int* mas = (int*)lpParam;
  15. for (int i = 0; i < n/2; i++)
  16. {
  17. if (mas[i] > max1)
  18. {
  19. max1 = mas[i];
  20. }
  21. }
  22. max[0] = max1;
  23. return 0;
  24. }
  25.  
  26. DWORD WINAPI ThreadProc2(LPVOID lpParam)
  27. {
  28. int max2 = 0;
  29. int* mas = (int*)lpParam;
  30. for (int i = n / 2; i < n; i++)
  31. {
  32. if (mas[i] > max2)
  33. {
  34. max2 = mas[i];
  35. }
  36. }
  37. max[1] = max2;
  38. return 0;
  39. }
  40.  
  41. void main()
  42. {
  43. int mas[n],maximum=0;
  44.  
  45. srand(time(NULL));
  46. for (int i = 0; i < n; i++)
  47. {
  48. mas[i] = rand() % 100;
  49. }
  50.  
  51. for (int i = 0; i < n; i++)
  52. {
  53. cout << mas[i] << " ";
  54. }
  55. cout << endl;
  56.  
  57. HANDLE th1 = CreateThread(NULL, 0, ThreadProc1,(LPVOID)mas, 0, NULL);
  58. HANDLE th2 = CreateThread(NULL, 0, ThreadProc2, (LPVOID)mas, 0, NULL);
  59.  
  60. WaitForSingleObject(th1,INFINITE);
  61. WaitForSingleObject(th2, INFINITE);
  62.  
  63. cout << "Maximum[0] = " << max[0] << endl;
  64. cout << "Maximum[1] = " << max[1] << endl;
  65.  
  66. if (max[0] > max[1])
  67. {
  68. maximum = max[0];
  69. }
  70. else
  71. {
  72. maximum = max[1];
  73. }
  74. cout << "Maximum = " << maximum << endl;
  75.  
  76. system("pause");
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement