A simulation of a factory production line is to be written in C++. There are 3 workplaces named station1, station2, station 3, where components of the widget are produced Station 1 needs the screwdriver to complete its part, and updates parts[0] of the parts list array. Station 2 needs both the screwdriver and saw to complete its part, and updates parts[1]. Station 3 needs the saw and hammer to construct its part, and updates parts[2]. For this problem, it is your job to efficiently utilize the mutexes (hammer, screwdriver, and saw) to correctly coordinate the production of the parts. Be aware that we must be careful to avoid deadlock and your solution should not reduce the problem to sequential execution. This program is due no later than 5:30 PM on 10-12-2020. No late submissions will be accepted. This program will carefully be examined for comments, efficiency, and accuracy. Name the program factory.cpp and place it in your pgm directory.
Input: From command line:
The input for this program will be the total number of parts each thread must produce. We are assuming that each thread will produce the same number of parts.
./a.out <number>
Output: To console.
Contents of parts array are:
parts[0] is: <number>
parts[1] is: <number>
parts[2] is: <number>
Answer :
include <bits/stdc++.h>
using
namespace
std;
// Function to find minimum cost
// to be incurred
int
findMinCost(
int
arr[],
int
n,
int
choc_cost) {
// To reach first station,
initial
// chocolates required
int
choc_buy =
arr[0];
int
curr_choc =
0;
// Start traversing
for
(
int
i = 0; i < n - 1; i++)
{
// Find no. of
chocolates
// lose or
gain
int
choc
= arr[i] - arr[i + 1];
// Add into
curr_coc
curr_choc +=
choc;
// if no. of
chocolates becomes
// negative that
means we have
// to buy that no. of
chocolates
if
(curr_choc < 0) {
choc_buy
+=
abs
(curr_choc);
curr_choc
= 0;
}
}
// Return cost
required
return
choc_buy *
choc_cost;
}
// Drivers code
int
main() {
int
arr[] = {10, 6,
11, 4, 7, 1};
int
n =
sizeof
(arr) /
sizeof
(arr[0]);
// Price of each candy
int
p =
5;
cout << findMinCost(arr, n,
p);
return
0;
}
Get Answers For Free
Most questions answered within 1 hours.