#include <stdio.h> /* printf, scanf, puts, NULL */
#include <iostream> /* cin and cout functions */
#include <cmath> /*for power function etc.. */
#include <string> /*to let us use strings as inputs */
#include <sstream> /*allows conversion of strings to floats */
#include <fstream> /*has all the file input functions */
#include <cmath>
using namespace std; /*saves some typing*/
int main()
{
string Myline; //this will contain the data read from the file
ifstream myfile("StatNum2.txt"); //associate file with the input stream, note file must be in project directory with cpp file.
//need to put in correct filename in place of Filename.***
int n = 0;
float numPoints = 0.0;
int Points[200];
// Normal comment
/* Multi-line comment */
if (myfile.is_open())
{
cout << "File is open" << endl;
while (!myfile.eof()) //note the ! this means this loops reads through until the file closes
{
getline(myfile, Myline); //this reads a single line from myfile into Myline
stringstream convert(Myline);
if (!(convert >> Points[n])) //uses stringstream to convert Myline (which is a string) into a number and put it into an index of Points
{
Points[n] = 0;
}
cout << n;
cout << ' ';
cout << Points[n] << endl;
n++;
}
myfile.close();
numPoints = n;
cout << "Number of integers: " << numPoints << endl;
}
else
{
cout << "Could not open file" << endl;
}
int sum = 0.0;
for (int i = 0; i < numPoints; i = i + 1)
{
// Code here will be repeated as long as 'i' is less than 100.
sum = sum + Points[i];
}
cout << "Sum: " << sum << endl;
float average = sum / numPoints;
cout << "Average: " << average << endl;
int x = 0;
float sqdiff = 0.0;
for (int x = 0; x < numPoints; x = x + 1);
{
sqdiff = (Points[x] - average)*(Points[x] - average);
}
cout << "difference of squares:" << sqdiff << endl;
float stddev = sqrt(sqdiff/n);
cout << "Standard Deviation:" << stddev << endl;
我正在尝试找到标准偏差但是从第65行上面的代码遇到了一些问题。代码产生了错误的结果或说Point [x]未定义,因此不确定如何修复
任何帮助将不胜感激,谢谢