Question
This program is supposed to read from a text file and create an array with the size of the first number in the file and
This program is supposed to read from a text file and create an array with the size of the first number in the file and fill it with the other number which is going to be zero and ones
if the value is zero it means the bulb is defective if it's equal to 1 the bulb is working this assignment is made to get familiar to using threads so the recursion of the array splitting has to work that way by the end of the code we want to know how many threads were created and trace back the zeros to find there index on the first array and displaying it
the issue that I'm having is that I can't figure out how to trace does values down when I have to threads running and doing each recursion concurrently can anyone help me out with this
Thank you.
// COEN346-Ass1.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include
using namespace std;
static int TheCounter; static vector
void printVec(vector
void FindDefective(int Arr[] , int size) { bool check = false; int scope = size; TheCounter++;
for (int i = 0; i < scope; i++) { if (Arr[i] == 0) { check = true; break; } } if (check == false && TheCounter == 0) { cout << "All bulbs are working properly" << endl; }
if (scope == 1) { } }
else if (check == true) { int pivot = scope / 2; int *LeftArr = new int[scope / 2]; int *RightArr = new int[scope / 2]; for (int i = 0; i < pivot; i++) { LeftArr[i] = Arr[i]; RightArr[i] = Arr[scope / 2 + i]; } thread t1(FindDefective,LeftArr , scope/2); thread t2(FindDefective,RightArr,scope/2); t1.join(); t2.join(); } }
void BulbArr() { ifstream infile; string fileName; label : cout << "please enter file name (example: bulb.txt) : "; cin >> fileName; infile.open(fileName); if (!infile.fail()) { int size; infile >> size; int *bulb = new int[size]; for (int i = 0; i < size; i++) { infile >> bulb[i]; if (bulb[i] > 1 || bulb[i] < 0) { cout << "input file has wrong values please try again."; goto label; } } thread t3(FindDefective, bulb , size); t3.join();
} } int main() { BulbArr(); printVec(DefectiveBulbs); cout << "The number of threads for this problem was : " << TheCounter << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started