Answer the question
In order to leave comments, you need to log in
How to write a formula to calculate the mean without the minimum element?
The task looks like this: Write a program for the console process, which consists of two threads: main and worker. The main thread must do the following: • Create an array of integers whose dimension and elements are entered from the console. • Create a worker thread. • Find the minimum and maximum elements of an array and print them to the console. After each comparison of elements, "sleep" 7 milliseconds. • Wait for the worker thread to complete. • Count the number of elements in the array whose value is greater than the average value of the array elements, and print it to the console. • To finish work.
The worker should do the following: • Find the average of the array elements. After each summation of elements, "sleep" 12 milliseconds. • Finish your work.
It is implemented, but there is one more subtask that I can't handle. Namely: The worker thread must find the value of the average value of the elements of the array, excluding the minimum element.
how to implement this if the minimum value is looked up in int main and the average value is looked up in Worker?
Program code:
#include <windows.h>
#include <iostream>
#include <locale.h>
using namespace std;
int n;
int *arr;
int min;
void Worker(LPVOID W)
{
float sum = 0;
float Srmin = 0; // переменная для сред.значения без минимального элемента
float SrZn; // переменная для ср. значения
int count = 0;
int max = 0;
int min;
for (int i = 0; i < n; i++)
{
sum=sum+arr[i];
{
Srmin = (sum-min)/n; ?????????????????
}
Sleep(12);
}
SrZn= sum/n;
for(int i=0; i<n; i++)
{
if(arr[i]>SrZn)
{
count++;
}
}
cout << "\nСреднее значение элементов без минимального =" << Srmin;
cout << "\nСреднее значение элементов массива =" << SrZn;
cout <<"\nКоличество элементов больше среднего =" << count;
}
int main()
{
setlocale(LC_ALL, "Russian");
HANDLE hThread;
DWORD IDThread;
int inc = 10;
int max = 0;
int min;
cout << "Введите размер масcива : ";
cin >> n;
cout << "Введите элементы массива: ";
int *tmp;
arr = new int[n];
tmp = arr;
for (int i = 0; i < n; i++, *tmp++)
{
cin >> *tmp;
}
cout << "Исходный массив: " << endl;
tmp = arr;
for (int i = 0; i < n; i++, *tmp++)
{
cout << *tmp << endl;
}
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Worker, 0, inc, &IDThread);
if (hThread == NULL)
return GetLastError();
cout << endl;
tmp = arr;
min = *tmp;
for (int i = 0; i < n; i++, *tmp++)
{
if (*tmp < min)
{
min = *tmp;
}
if (*tmp > max)
{
max = *tmp;
}
Sleep(7);
}
cout << "Минимальный элемент = " << min;
cout << "\nМаксимальный элемент = " << max;
// ждем, пока поток Add закончит работу
WaitForSingleObject(hThread, INFINITE);
// закрываем дескриптор потока Add
CloseHandle(hThread);
cout << endl;
}
Answer the question
In order to leave comments, you need to log in
Either find the minimum element in the worker or pass it to the Worker function
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question