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
Note: The program was tested with 5 boxes weight.
CFSB.txt (used for testing)
Due: in class Tuesday, 3/27/2018 (upload electronic copy by 9:00am) Problem: You will write a program...
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 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 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...