我有一个程序,它逐行读取测试.csv文件并给出标准差&每一行的意思。然后它继续编写一个新的.csv文件,包括每行的SD,平均值和原始值。
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <math.h>
#define rows 100
#define columns 11
using namespace std;
void read(int data[rows][columns]) {
string str;
ifstream ifile;
string filename="testDataIn.csv";
ifile.open(filename);
if (ifile)
{
int i = 0;
while (getline (ifile,str)){
stringstream ss(str);
string temp;
for(int j=0;j<columns;j++){
getline(ss,temp,',');
data[i][j]=atoi(temp.c_str());
}
i++;
}
cout << "Finished reading data." << endl;
}else
{
cout << "Error reading." <<endl;
}
ifile.close();
}
double* avg(int data[rows][columns]){
int total;
double average[rows];
for(int i=0;i<rows;i++){
total=0;
for(int j=0;j<columns;j++){
total+=data[i][j];
}
average[i]=(double)total/columns;
cout<<"The average of row "<<i+1<<" is "<<average[i]<<"."<<endl;
}
return average;
}
double* SD(int data[rows][columns]){
int total;
double sdarray[rows];
double sdnum[columns];
double sdtotal;
for(int i=0;i<rows;i++){
total=0;
sdtotal=0;
for(int j=0;j<columns;j++){
total+=data[i][j];
}
for(int n=0;n<columns;n++){
sdnum[n]=pow((data[i][n]-(double)total/columns),2);
}
for(int m=0;m<columns;m++){
sdtotal+=sdnum[m];
}
sdarray[i]=(double)sqrt(sdtotal/columns);
cout<<"The standard deviation of row "<<i+1<<" is "<<sdarray[i]<<"."<<endl;
}
return sdarray;
}
void write(int data[rows][columns],double* average, double* sdarray){
ofstream myfile;
myfile.open("testDataOut.csv");
if (myfile.is_open()) {
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
myfile<<data[i][j]<<",";
}
myfile<<(double)average[i]<<","<<(double)sdarray[i];
myfile<<"\n";
}
myfile.close();
cout<<"Done writing to file.";
}else{
cout<<"Error writing.";
}
}
int main()
{
int data[rows][columns]={0};
read(data);
write(data,avg(data),SD(data));
return 0;
}
这个程序的唯一问题是每行的标准偏差和平均值,当打印到.csv文件上时,是随机的十进制乱码,除了几行奇怪的例外。 我不知道为什么,我根本无法想出这个。