#include <thread>
#include <iostream>
#include <mutex>
std::once_flag flag;
const size_t arraySize = 1000;
int array[arraySize];
void fillWithRandomNumbers()
{
std::srand(time(0));
for (size_t index = 0; index < arraySize; ++index)
array[index] = (rand() % 999 + 1);
}
void countEvenAndOddNumbers(int counter[2])
{
std::call_once(flag, fillWithRandomNumbers);
counter[0] = 0;
counter[1] = 0;
for (size_t index = 0; index < arraySize; ++index)
++counter[array[index] & 1];
}
int summ(int num)
{
int sum = 0;
while (num != 0)
{
sum += num % 10;
num /= 10;
}
return sum;
}
void findDivisibleByThree(int* result, int* counter)
{
std::call_once(flag, fillWithRandomNumbers);
*counter = 0;
for (size_t index = 0; index < arraySize; ++index)
{
if ((summ(index) % 3) == 0)
{
result[*counter] = array[index];
++*counter;
}
}
}
void countDivisibleByFiveOrSeven(int* count)
{
std::call_once(flag, fillWithRandomNumbers);
*count = 0;
for (size_t index = 0; index < arraySize; ++index)
{
if ((((array[index] % 5) == 0)) || ((array[index] % 7) == 0))
++*count;
}
}
int main()
{
int array[arraySize];
int counter[2];
int count = 0;
int cou = 0;
std::thread t1(countEvenAndOddNumbers, counter);
std::thread t2(findDivisibleByThree , array, &cou);
std::thread t3(countDivisibleByFiveOrSeven, &count);
t1.join();
t2.join();
t3.join();
std::cout << "Even- " << counter[0] << "\n";
std::cout << "Odd- " << counter[1] << "\n";
std::cout << "EntireThree- " << cou << "\n";
std::cout << "EntireFiveOrSeven- " << count << "\n";
return 0;
}