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 |
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> // Function to simulate quality control check int qualityControlCheck(int batchSize, double defectRate) { int defectiveCount = 0; // Simulate the quality check for each item for (int i = 0; i < batchSize; ++i) { // Randomly determine if an item is defective if (static_cast<double>(std::rand()) / RAND_MAX < defectRate) { ++defectiveCount; } } return defectiveCount; } int main() { // Seed the random number generator std::srand(static_cast<unsigned>(std::time(nullptr))); int batchSize; double defectRate; std::cout << "Enter the batch size: "; std::cin >> batchSize; std::cout << "Enter the defect rate (as a percentage, e.g., 0.05 for 5%): "; std::cin >> defectRate; // Ensure defectRate is between 0 and 1 if (defectRate < 0 || defectRate > 1) { std::cerr << "Error: Defect rate must be between 0 and 1." << std::endl; return 1; } int defectiveCount = qualityControlCheck(batchSize, defectRate); std::cout << "Batch Size: " << batchSize << std::endl; std::cout << "Defect Rate: " << defectRate * 100 << "%" << std::endl; std::cout << "Number of Defective Items: " << defectiveCount << std::endl; std::cout << "Percentage of Defective Items: " << static_cast<double>(defectiveCount) / batchSize * 100 << "%" << std::endl; return 0; } |
Explanation
- Function
qualityControlCheck(int batchSize, double defectRate)
:- Purpose: Simulates the quality control check for a batch of items.
- Parameters:
batchSize
: The number of items in the batch.defectRate
: The probability of an item being defective.
- Implementation:
- Iterates through each item in the batch.
- Uses a random number to determine if an item is defective based on the
defectRate
. - Counts and returns the number of defective items.
- Main Function:
- Setup:
- Seeds the random number generator for simulating the defect checks.
- Prompts the user for the batch size and defect rate.
- Validates that the defect rate is within the range [0, 1].
- Simulation:
- Calls
qualityControlCheck
to determine the number of defective items. - Outputs batch size, defect rate, number of defective items, and percentage of defective items.
- Calls
- Setup:
Usage
- Quality Control Simulation: Demonstrates how to simulate the detection of defective items in a manufacturing batch.
- Statistical Reporting: Provides insights into the quality of the batch based on the defect rate.