Question

Due: in class Tuesday, 3/27/2018 (upload electronic copy by 9:00am) Problem: You will write a program to compute some statistics about boxes of a popular breakfast cereal called Chocolate Frosted Sugar Bombs manufactured by the General Junkfoods Corporation. Automated machinery is used at the companys factory to fill individual boxes with cereal. No machine is perfect, so the amount of cereal actually in a box will vary slightly from box to box. The data file CFSB.txt on the class website gives the weight (in ounces) of the last 1,000 boxes of Chocolate Frosted Sugar Bombs produced in the factory. For every box in this sample that weighs less than the advertised 20 ounces, General Junkfoods will incur a $250,000 fine. The statistics you must compute in your program are: . The average weight of the 20 ounce boxes of cereal. The standard deviation of the sample (see below). . The number of boxes below the advertised weight of 20 ounces. . The fine owed by the company for the underweight boxes. . The maximum value of the weights of the 1,000 boxes. The minimum value of the weights of the 1,000 boxes. Input: Your program should ask the user for the name of the file, and then open that file for input. The remainder of the input data will come from that file. You may assume that the file contains 1000 values, each with three digits after the decimal point, corresponding to the weights of the cereal boxes. Processing: Compute the statistics requested above. The standard deviation should be computed as follows: 1. For each number in the data set, subtract the average, and square the result. 2. Compute the average of these squared differences. 3. Take the square root of the result.
media%2F26e%2F26e2fbe0-5e82-475b-b1b6-be
0 0
Add a comment Improve this question Transcribed image text
Answer #1
C++ Program

#include<iostream>

#include<fstream>

#include<cmath>

#include<iomanip>

#include<string>

using namespace std;

const int TOTAL_BOXES = 1000;

/**

* Function to read the file one by one and populate the weight array

* find the max and min weight

*/

void readFile(ifstream &infile, double weight[], double &max, double &min)

{

int i = 0;

double data = 0;

infile.clear();

while(!infile.eof())

{

infile >> weight[i];

if(i == 0)

{

max = weight[i];

min = weight[i];

}

else

{

if(weight[i] > max)

max = weight[i];

if(weight[i] < min)

min = weight[i];

}

i++;

}

}

/**

* Function to return the average weight

*/

double getAverage(double weight[])

{

double sum = 0;

for(int i = 0; i < TOTAL_BOXES; i++)

{

sum += weight[i];

}

return sum / (double)TOTAL_BOXES;

}

/**

* Function to return the fine and number of boxes charged for fine

*/

double getFine(double weight[], int &count)

{

const double PER_BOX_FINE = 250000;

count = 0;

for(int i = 0; i < TOTAL_BOXES; i++)

{

if(weight[i] < 20)

count++;

}

return ((double)count * PER_BOX_FINE);

}

/**

* Function to return the standard deviation

*/

double getStd(double weight[])

{

double mean = getAverage(weight);

double sqrd_diff = 0;

for(int i = 0; i < TOTAL_BOXES; i++)

{

sqrd_diff += pow((weight[i] - mean), 2);

}

sqrd_diff = sqrd_diff / TOTAL_BOXES;

return sqrt(sqrd_diff);

}

// main function

int main()

{

string filename;

double weight[TOTAL_BOXES];

double max= 0;

double min = 0;

double avg = 0;

int count = 0;

double fine = 0;

double std = 0;

ifstream infile;

cout << "Please enter the name of the data file: ";

getline(cin>>ws, filename);

infile.open(filename.c_str());

if(!infile)

{

cout << "Error! File '" << filename << "' was not found" << endl;

return -1;

}

// reading the file for weight array

readFile(infile, weight, max, min);

infile.close();

// getting the average

avg = getAverage(weight);

// getting the fine and count of boxes less than 20 ounces

fine = getFine(weight, count);

// getting the standard deviation

std = getStd(weight);

cout << fixed << setprecision(3); // setting to display 3 places after decimal

cout << endl << "Chocolate Frosted Sugar Bombs statistics:" << endl << endl;

cout << "Average weight: " << avg << endl;

cout << "Standard Deviation: " << std << endl << endl;

cout << "Number of boxes below 20 ounces: " << count << endl;

cout << "Fine for this is: $" << fine << endl << endl;

cout << "Maximum weight of all boxes: " << max << endl;

cout << "Minimum weight of all boxes: " << min << endl << endl;

system("pause");

return 1;

}

OUTPUT

E:AProgram Files\C Please enter the name of the data file: CFSB.txt Chocolate Frosted Sugar Bombs statistics: verage weight:

Note: The program was tested with 5 boxes weight.

CFSB.txt (used for testing)

CFSB.txt - Notepad File Edit Format View Help 20 21 19 20

Add a comment
Know the answer?
Add Answer to:
Due: in class Tuesday, 3/27/2018 (upload electronic copy by 9:00am) Problem: You will write a program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem: You will write a program to compute some statistics based on monthly average temperatures for...

    Problem: You will write a program to compute some statistics based on monthly average temperatures for a given month in each of the years 1901 to 2016. The data for the average August temperatures in the US has been downloaded from the Climate Change Knowledge Portal, and placed in a file named “tempAugData.txt”, available on the class website. The file contains a sequence of 116 values. The temperatures are in order, so that the first one is for 1901, the...

  • Problem: You will write a program a C++ to compute some statistics based on monthly average...

    Problem: You will write a program a C++ to compute some statistics based on monthly average temperatures for a given month in each of the years 1901 to 2016. The data for the average August temperatures in the US has been downloaded from the Climate Change Knowledge Portal, and placed in a file named “tempAugData.txt”, available on the class website. The file contains a sequence of 116 values. The temperatures are in order, so that the first one is for...

  • Topics: list, file input/output (Python) You will write a program that allows the user to read...

    Topics: list, file input/output (Python) You will write a program that allows the user to read grade data from a text file, view computed statistical values based on the data, and to save the computed statistics to a text file. You will use a list to store the data read in, and for computing the statistics. You must use functions. The data: The user has the option to load a data file. The data consists of integer values representing student...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT