Create a program to be used in a coin sorting machine. The machine takes U.S. pe
ID: 3831787 • Letter: C
Question
Create a program to be used in a coin sorting machine. The machine takes U.S. pennies, nickels, dimes, and quarters. The base specifications for these are as follows: nickel dime quarter penny diameter 19.05 mm 21.21 mm 17.91 mm 24.26 mm weight 2.5 g 5.0 g 2.268 g 5.670 g The machine is equipped with sensors that measure the diameter and weight of each coin in order to identify and count each type of coin. Since coins can be subject to wear and tear, each coin diameter and width is allowed a tolerance: +-2%. Your program must: a) declare constants for all constant values (base diameters, base widths, tolerance percentage) b) calculate and store the acceptable high and lowvalues for each coin for diameter and width c) at the start, output the acceptable high and low values d) open and read coin data from the given data file and classify the coin based on the coin data, which is stored as a diameter and a weight, one coin on a line (partial file shown here) 17.7084 2.27567 18.2629 2.28056 18.0828 2.27956 19.4268 2.46291 19.4004 2.5018 21.3491 5.01808 17.6017 2.22427 21.2601 4.9028 18.7941 2.46094 23.7941 5.67778 21.4238 4.93848 17.8968 2.268 e) store the number of coins of each type as each is classified f) output the number and value of valid coins and the number of invalid coins, sample output shown belowExplanation / Answer
Here is the code for you:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
bool isValidCoin(double coinDia, double coinWeight, double acceptable_coin_diameter_low, double acceptable_coin_diameter_high, double acceptable_coin_weight_low, double acceptable_coin_weight_high)
{
if(coinDia >= acceptable_coin_diameter_low && coinDia <= acceptable_coin_diameter_high && coinWeight >= acceptable_coin_weight_low && coinWeight <= acceptable_coin_weight_high)
return true;
return false;
}
int main()
{
const double base_penny_diameter = 19.05;
const double base_nickel_diameter = 21.21;
const double base_dime_diameter = 17.91;
const double base_quarter_diameter = 24.26;
const double base_penny_weight = 2.5;
const double base_nickel_weight = 5.0;
const double base_dime_weight = 2.268;
const double base_quarter_weight = 5.670;
const double tolerance_percent = 2;
double acceptable_penny_diameter_high = base_penny_diameter * (100 + tolerance_percent)/100;
double acceptable_nickel_diameter_high = base_nickel_diameter * (100 + tolerance_percent)/100;
double acceptable_dime_diameter_high = base_dime_diameter * (100 + tolerance_percent)/100;
double acceptable_quarter_diameter_high = base_quarter_diameter * (100 + tolerance_percent)/100;
double acceptable_penny_diameter_low = base_penny_diameter * (100 - tolerance_percent)/100;
double acceptable_nickel_diameter_low = base_nickel_diameter * (100 - tolerance_percent)/100;
double acceptable_dime_diameter_low = base_dime_diameter * (100 - tolerance_percent)/100;
double acceptable_quarter_diameter_low = base_quarter_diameter * (100 - tolerance_percent)/100;
double acceptable_penny_weight_high = base_penny_weight * (100 + tolerance_percent)/100;
double acceptable_nickel_weight_high = base_nickel_weight * (100 + tolerance_percent)/100;
double acceptable_dime_weight_high = base_dime_weight * (100 + tolerance_percent)/100;
double acceptable_quarter_weight_high = base_quarter_weight * (100 + tolerance_percent)/100;
double acceptable_penny_weight_low = base_penny_weight * (100 - tolerance_percent)/100;
double acceptable_nickel_weight_low = base_nickel_weight * (100 - tolerance_percent)/100;
double acceptable_dime_weight_low = base_dime_weight * (100 - tolerance_percent)/100;
double acceptable_quarter_weight_low = base_quarter_weight * (100 - tolerance_percent)/100;
cout << "Acceptable width of a penny is from " << acceptable_penny_diameter_low << " to " << acceptable_penny_diameter_high << endl;
cout << "Acceptable weight of a penny is from " << acceptable_penny_weight_low << " to " << acceptable_penny_weight_high << endl << endl;
cout << "Acceptable width of a nickel is from " << acceptable_nickel_diameter_low << " to " << acceptable_nickel_diameter_high << endl;
cout << "Acceptable weight of a nickel is from " << acceptable_nickel_weight_low << " to " << acceptable_nickel_weight_high << endl << endl;
cout << "Acceptable width of a dime is from " << acceptable_dime_diameter_low << " to " << acceptable_dime_diameter_high << endl;
cout << "Acceptable weight of a dime is from " << acceptable_dime_weight_low << " to " << acceptable_dime_weight_high << endl << endl;
cout << "Acceptable width of a quarter is from " << acceptable_quarter_diameter_low << " to " << acceptable_quarter_diameter_high << endl;
cout << "Acceptable weight of a quarter is from " << acceptable_quarter_weight_low << " to " << acceptable_quarter_weight_high << endl << endl;
int numOfPennies = 0, numOfNickels = 0, numOfDimes = 0, numOfQuarters = 0, numOfInvalidCoins = 0;
ifstream fin;
fin.open("CoinsDiameterAndWeights.txt");
double coinWeight, coinDiameter;
while(!fin.eof())
{
fin >> coinDiameter >> coinWeight;
if(isValidCoin(coinDiameter, coinWeight, acceptable_penny_diameter_low, acceptable_penny_diameter_high, acceptable_penny_weight_low, acceptable_penny_weight_high))
numOfPennies++;
if(isValidCoin(coinDiameter, coinWeight, acceptable_nickel_diameter_low, acceptable_nickel_diameter_high, acceptable_nickel_weight_low, acceptable_nickel_weight_high))
numOfNickels++;
if(isValidCoin(coinDiameter, coinWeight, acceptable_dime_diameter_low, acceptable_dime_diameter_high, acceptable_dime_weight_low, acceptable_dime_weight_high))
numOfDimes++;
if(isValidCoin(coinDiameter, coinWeight, acceptable_quarter_diameter_low, acceptable_quarter_diameter_high, acceptable_quarter_weight_low, acceptable_quarter_weight_high))
numOfQuarters++;
else
numOfInvalidCoins++;
}
cout << "Number of pennies counted: " << numOfPennies << " worth: " << fixed << setprecision(2) << numOfPennies * 0.01 << endl;
cout << "Number of nickels counted: " << numOfNickels << " worth: " << fixed << setprecision(2) << numOfNickels * 0.05 << endl;
cout << "Number of dimes counted: " << numOfDimes << " worth: " << fixed << setprecision(2) << numOfDimes * 0.10 << endl;
cout << "Number of quarters counted: " << numOfQuarters << " worth: " << fixed << setprecision(2) << numOfQuarters * 0.25 << endl;
cout << "Number of invalid coins counted: " << numOfInvalidCoins << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.