Reputation: 3327
I'm having a hard time seeing why my code doesn't work. The part that's a problem is when I'm passing values from my text file into my grade variable. I don't see why this is wrong. Everything else with my code is fine except for that, but I included it all anyway.
string fileName;
cout << "Program that takes data from file and calculate\nmean and standard deviation and put it in file out.txt\n";
cout << "Enter the input file name ";
cin >> fileName;
double grade;
ifstream inData;
inData.open(fileName.c_str());
// Declare vector<double> vecx
vector<double> vecx;
// read data from input file to vector vecx,
while(inData >> grade)
{
vecx.push_back(grade);
}
inData.close();
// keep track how many elements you read from file
// When you done with reading from file, close stream inData
And here is the full code in case you were interested.
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
string fileName;
cout << "Program that takes data from file and calculate\nmean and standard deviation and put it in file out.txt\n";
cout << "Enter the input file name ";
cin >> fileName;
double grade;
ifstream inData;
inData.open(fileName.c_str());
// Declare vector<double> vecx
vector<double> vecx;
// read data from input file to vector vecx,
while(inData >> grade)
{
vecx.push_back(grade);
}
inData.close();
// keep track how many elements you read from file
// When you done with reading from file, close stream inData
// read element by element in vector vecx and calculate sum;
double sum=0;
double average;
for (int i=0; i < vecx.size(); i++)
sum=sum+vecx[i];
average=sum/(vecx.size());
// sum divide by number of elements to find mean(average)
//again read element by element and calculate
//square of difference between element and mean
//calculate sum of squares of differences
//divide by number of elements and
//take square root - you got the standard deviation
sum=0;
double variance, stdev;
for (int i=0; i < vecx.size(); i++)
sum=sum+(vecx[i]-average*vecx[i]-average);
variance=sum/(vecx.size());
stdev=sqrt(variance);
//open output stream
ofstream outData;
outData.open("out.txt");
//output mean and standard deviation
cout << "Average is " << average << endl;
cout << "Standard deviation is " << stdev << endl;
//close stream
outData.close();
}
I feel like an idiot for not seeing why this isn't working...I should be able to figure this out but I haven't.
Upvotes: 0
Views: 7072
Reputation: 1013
You are never writing data out to your file. Use your ofstream outData
instead of std::cout
. Otherwise, you are only sending your output to the console.
//open output stream
ofstream outData;
outData.open("out.txt");
//output mean and standard deviation
outData << "Average is " << average << endl;
outData << "Standard deviation is " << stdev << endl;
//close stream
outData.close();
Upvotes: 5
Reputation: 4521
C++ follows the normal mathematical orders of precedent.
This line:
sum=sum+(vecx[i]-average*vecx[i]-average);
looks incorrect. Multiplication happens before subtraction so it's calculating this:
sum=sum+((vecx[i]-(average*vecx[i]))-average);
but presumably you meant this:
sum=sum+((vecx[i]-average)*(vecx[i]-average));
You might be interested in seeing the full list of precedence ordering.
Upvotes: 9