[ create a new paste ] login | about

Link: http://codepad.org/W99XawoE    [ raw code | output | fork ]

C++, pasted on Oct 1:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>

int main()
{
  double num;
  int num_values;
  double sum_values;
  double avg;
  double std_deviation;
  ifstream instream;
  
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(4);

  instream.open("infile.dat");
  if(instream.fail())
  {
    cout << "Input file opening failed.\n";
    exit(1);
  }

  while(instream >> num)
  {
     num_values++;

     sum_values += num;
 
     avg = sum_values / num_values;
  }

  cout << "Average = " << avg << endl;

  instream.close();
  instream.open("infile.dat");

  while(instream >> num)
  {
     num_values++;

     sum_values += num;

     avg = sum_values / num_values;

     std_deviation = sqrt(pow(num - avg, 2.0) / num_values);
  }

  cout << "Standard deviation = " << std_deviation << endl;

  instream.close();

  return 0;
}


Output:
1
2
3
4
5
6
cc1plus: warnings being treated as errors
In function 'int main()':
Line 11: warning: 'avg' may be used uninitialized in this function
Line 10: warning: 'sum_values' may be used uninitialized in this function
Line 9: warning: 'num_values' may be used uninitialized in this function
Line 12: warning: 'std_deviation' may be used uninitialized in this function


Create a new paste based on this one


Comments: