SHARE
TWEET

Untitled

a guest Feb 17th, 2017 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Program to compute mean, standard deviation and standard
  3. // error of the mean electronic charge. Data is read from file.
  4.  
  5. #include<iostream>
  6. #include<iomanip>
  7. #include<fstream>
  8. #include<cmath>
  9. #include<string>
  10. #include<sstream>
  11.  
  12. using namespace std;
  13.  
  14. // Functions to compute mean and standard deviation
  15.  
  16. double calcmean(double *data){
  17.     double sum{0};
  18.     int length = sizeof(data);
  19.     for (int i = 0; i < length; i++){
  20.         sum += data[i];
  21.     }
  22.     double mean = sum / (length);
  23.     return mean;
  24. }
  25. double calcstddev(double *data){
  26.     double sum{ 0 };
  27.     int length = sizeof(data);
  28.     double sumdev2{ 0 };
  29.  
  30.     for (int i = 0; i < length; i++){
  31.         sum += data[i];
  32.     }
  33.     double mean = sum / length;
  34.     for (int i = 0;i < length; i++){
  35.         sumdev2 += pow(data[i] - mean, 2);
  36.     }
  37.     double stddev = pow(sumdev2 / (length - 1), 0.5);
  38.     return stddev;
  39. }
  40. double strtodbl(string s) {
  41.     double d;
  42.     stringstream ss(s);
  43.     ss >> d;
  44.     return d;
  45. }
  46. bool linetest(string input){
  47.     stringstream ss(input);
  48.     double d;
  49.     if (!(ss >> d)){
  50.         return 0;
  51.     }
  52.     return 1;
  53. }
  54.  
  55. // Main function
  56.  
  57. int main()
  58. {
  59.     int datapoints{0};
  60.     string filename;
  61.     /* Ask user to enter number of data points
  62.     cout << "How many data points does your file contain? "; cin >> datapoints;
  63.     while (cin.fail() || datapoints != floor(datapoints) || datapoints < 0){
  64.         cout << "How many data points does your file contain? "; cin.clear(); cin.ignore() >> datapoints;
  65.     }*/
  66.     // Ask user to enter filename
  67.     cout << "What is the name of your file? "; cin >> filename;
  68.  
  69.     // Open file and check if successful
  70.     fstream myfile(filename);
  71.     if (!myfile.good()) {
  72.         cout << "Error: File could not be opened." << endl;
  73.     }
  74.  
  75.     // Allocate memory for data
  76.     double *data;
  77.     data = new double[datapoints];
  78.  
  79.     // Read data from file, ignoring any additional bad data
  80.     string line;
  81.     while (getline(myfile, line)){
  82.         cout << line << endl;
  83.         if (linetest(line)){
  84.             datapoints++;
  85.         }
  86.     }
  87.     cout << "The file contains " << datapoints << " measurements." << endl;
  88.     /*myfile.clear();
  89.     myfile.seekg((0, ios::beg));
  90.     int i{0};
  91.     while (getline(myfile, line)){
  92.         if (linetest(line)){
  93.                 data[i] = strtodbl(line);
  94.                 i++;
  95.             }
  96.         else{
  97.             cout << "Error encountered: " << line << " is not a valid input" << endl;
  98.         }
  99.     }
  100.    
  101.     // Close file
  102.     myfile.close();
  103.  
  104.  
  105.      //Compute mean
  106.     double mean = calcmean(data);
  107.  
  108.     // Compute standard deviation
  109.     double stddev = calcstddev(data);
  110.  
  111.     // Compute standard error of mean
  112.     double stderrmean = calcstddev(data) / pow(datapoints, 0.5);
  113.  
  114.     cout << "The mean value is: " << mean << endl;
  115.     cout << "standard error on the mean is: " << stddev << endl;
  116.     cout << "The standard deviation is: " << stderrmean << endl;
  117.  
  118.     // Free memory*/
  119.     delete[] data;
  120.    
  121.     return 0;
  122. }
RAW Paste Data
Top