-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
78 lines (68 loc) · 1.78 KB
/
main.cpp
File metadata and controls
78 lines (68 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <windows.h>
using namespace std;
int ArraySize;
int* Array;
int Min = 0, Max = 0, avg;
HANDLE hMinMax, hAverage;
void min_max() {
for (int i = 1; i < ArraySize; i++)
{
if (Array[i] > Array[Max])
Max = i;
Sleep(7);
if (Array[i] < Array[Min])
Min = i;
Sleep(7);
}
cout << "Min and max values are: " << Array[Min] << " " << Array[Max] << endl;
}
void average() {
int sum = 0;
for (int i = 0; i < ArraySize; i++)
{
sum += Array[i];
Sleep(12);
}
avg = sum / ArraySize;
cout << "Average value is " << avg << endl;
}
int main() {
cout << "enter array size and elements: \n";
cin >> ArraySize;
Array = new int[ArraySize];
for (int i = 0; i < ArraySize; i++)
cin >> Array[i];
HANDLE hThread;
DWORD IDThread;
hMinMax = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)min_max, NULL, 0, &IDThread);
if (hMinMax == NULL)
return GetLastError();
hAverage = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)average, NULL, 0, &IDThread);
if (hMinMax == NULL)
return GetLastError();
// Wait for threads to finish
WaitForSingleObject(hMinMax, INFINITE);
WaitForSingleObject(hAverage, INFINITE);
int min = Array[Min];
int max = Array[Max];
// Replace min and max with average
for (int i = 0; i < ArraySize; i++)
{
if (Array[i] == min || Array[i] == max)
Array[i] = avg;
}
//Array[Max] = avg;
//Array[Min] = avg;
// Output result
cout << "New array: \n";
for (int i = 0; i < ArraySize; i++) {
cout << Array[i] << " ";
}
cout << endl;
// Clean up
delete[] Array;
CloseHandle(hMinMax);
CloseHandle(hAverage);
return 0;
}