Guest User

Untitled

a guest
Apr 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6. const int MAX = 10000;
  7. struct prArray
  8. {
  9. int pStart, pEnd, pTest, pCount;
  10. int tArray[MAX];
  11. };
  12.  
  13. int check_prime(int);
  14. DWORD WINAPI primer(LPVOID lpParam);
  15.  
  16. int main()
  17. {
  18. int one, two, diff, temp, end1, start2;
  19.  
  20. cout << " START:\n";
  21. cin >> one;
  22. cin >> two;
  23. prArray ash;
  24. prArray jeff;
  25. diff = (two - one);
  26. temp = diff / 2;
  27. end1 = one + temp;
  28. start2 = end1 + 1;
  29. ash.pStart = one;
  30. ash.pEnd = one + temp;
  31. ash.pCount = ash.pStart;
  32. jeff.pStart = start2;
  33. jeff.pEnd = two;
  34. jeff.pCount = jeff.pStart;
  35. DWORD myThreadID1;
  36. HANDLE myHandle = CreateThread(
  37. NULL,
  38. 0,
  39. primer,
  40. &ash,
  41. 0,
  42. &myThreadID1);
  43. DWORD myThreadID2;
  44. HANDLE myHandle2 = CreateThread(
  45. 0,
  46. 0,
  47. primer,
  48. &jeff,
  49. 0,
  50. &myThreadID2);
  51.  
  52.  
  53. CloseHandle(myHandle);
  54. CloseHandle(myHandle2);
  55. cout << endl << endl;
  56. cout << " BACK TO main()\n";
  57. cout << "\n THREAD1: \n";
  58. cout << "Thread1 starting point: " << ash.pStart << endl;
  59. cout << "Thread1 ending point: " << ash.pEnd << endl;
  60. cout << "Test: " << ash.pTest << endl;
  61. int i1;
  62. for (i1 = 0; i1 < MAX; i1++)
  63. {
  64. if (ash.tArray[i1] == 0)
  65. {
  66. break;
  67. }
  68. else
  69. {
  70. cout << ash.tArray[i1] << " ";
  71. }
  72. }
  73.  
  74. //begin second thread (jeff)
  75. cout << endl << endl;
  76. cout << " THREAD2: " << endl;
  77. cout << "Thread2 starting point: " << jeff.pStart << endl;
  78. cout << "Thread2 ending point: " << jeff.pEnd << endl;
  79. cout << "Test: " << jeff.pTest << endl;
  80. int js;
  81. for (js = 0; js < MAX; js++)
  82. {
  83. if (jeff.tArray[js] == 0)
  84. {
  85. break;
  86. }
  87. else
  88. {
  89. cout << jeff.tArray[js] << " ";
  90. }
  91. }
  92. cout << "\n first = " << i1 << endl;
  93. cout << " second = " << js << endl;
  94. cout << "Thread 1 counter = " << ash.pCount << endl;
  95. cout << "Thread 2 counter = " << jeff.pCount << endl;
  96. cout << "\n END PROGRAM" << endl;
  97. system("pause");
  98. return 0;
  99. }
  100. int check_prime(int a)
  101. {
  102. int c;
  103. if ( a == 2 || a == 3 || a == 5 || a ==7) //check if the number is 2
  104. {
  105. return 1;
  106. }
  107. else if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0 || a % 7 == 0)
  108. {
  109. return 0;
  110. }
  111. else
  112. {
  113. for (c = 11; c <= a - 1; c+=2)
  114. {
  115. if (a%c == 0)
  116. return 0;
  117. }
  118. if ( c == a)
  119. return 1;
  120. }
  121. }
  122. DWORD WINAPI primer(LPVOID lpParam)
  123. {
  124. prArray& dan = (prArray)lpParam;
  125. dan.pTest = (dan.pEnd - dan.pStart);
  126. for (int i = 0; i < MAX; i++)
  127. {
  128. dan.tArray[i] = 0;
  129. }
  130. int b = 0;
  131. for (int lp = dan.pStart; lp <= dan.pEnd; lp++, dan.pCount++)
  132. {
  133. int result3 = check_prime(lp);
  134. if (result3 == 1)
  135. {
  136. dan.tArray[b] = lp;
  137. b++;
  138. }
  139. }
  140. }
Add Comment
Please, Sign In to add comment