带有标准偏差和平均值的c++会产生不好的输出

C++ with standard deviation and mean issues bad output

本文关键字:输出 c++ 标准 平均值      更新时间:2023-10-16

你好,谁能帮帮我,我的SD和mean的数字出来真的很奇怪,我不知道为什么。我感觉我访问数组的方式可能是错误的?有一个结构,但也是一个独立的数组,这就是我正在使用的。我得到了中位数,但其他的输出都很奇怪。

#include<iostream>
#include<fstream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;
#define output_bit 1  // change it to 1/0 if you want(don't want to print/output 
struct Stock{
    char  date[10];
    double open;
    double high;
    double low;
    double close;
    int    volume;
    double adj_close;
};
//debug tip: pause your program
void pauseIT(){
    cout << "Wait, press ENTER to continue,...";
    cin.clear(); //clear errors
    cin.sync();  //clear the buffer
    cin.get();   //wait for a letter
}
void setOutputPrecision(int n){
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(n);   
}
double* sortArray(double *a, int size);
int findMaxIndex(const double *a, int size);
void MedianandMean(double * stock, int size, double& stockMedian, double& stockMean);
void standardDeviation(double *stock, int size, double& sum, double& avg, double& sum2, double& stdDev);
int main(int argc, char** argv) {
    const int n = 249;
    Stock GoogleStock[n]; // a struct array
    char filename[100] = "google_stock_.txt";
    double *google_stock_adj_close=new double[n];
    FILE *fp;
    char header[100];
    // items since the 2nd line in the file
    double open, high, low, close, adj_close;
    int    volume;
    char   date[10];
    //header information
    char date_s[10],open_s[10], high_s[10], low_s[10], 
         close_s[10],  volume_s[10], adj_s[10], adj_close_s[10];

    int i=0;
    // Read a file 
    fp=fopen(filename, "r"); //open a file for read
    // book keeping: make sure it is not a NULL
    if(!fp)
        perror("Double-check your input file");
    // read the header information (first line)
    if(!feof(fp)){
          fscanf(fp, "%s  %s  %s  %s  %s  %s  %s %sn", date_s, open_s, 
                  high_s, low_s, close_s,volume_s, adj_s, adj_close_s);
    }
    // merge the "adj"  and "Close"  to "AdjClose"
        strcat(adj_s, adj_close_s);
        strcpy(adj_close_s, adj_s);
    //read the main information from the 2nd line to the file end: 
    // feof will tell you if the file ends
      while(!feof(fp)){
        fscanf(fp, "%s  %lf  %lf  %lf  %lf  %10d  %lfn", 
                date, &open, &high, &low, &close, &volume, &adj_close);
     //copy information to a structure array
        strcpy(GoogleStock[i].date, date);
        GoogleStock[i].open      = open;
        GoogleStock[i].high      = high;
        GoogleStock[i].low       = low;
        GoogleStock[i].close     = close;
        GoogleStock[i].volume    = volume;
        GoogleStock[i].adj_close = adj_close; 
       //copy the adj close price to an independent array 
        google_stock_adj_close[i]= adj_close;
        i++;
    }
    fclose(fp); //close your program after finishing reading
    cout<<"Finish reading n"<<endl;

      if (output_bit) {
         cout<<"The stock information is written in a structure array: n"<<endl;
         pauseIT();
         for(int i=0;i<n;i++){
            fprintf(stderr, "%s  %lf  %lf  %lf  %lf  %d  %lfn", 
                 GoogleStock[i].date,
                 GoogleStock[i].open, 
                 GoogleStock[i].high, 
                 GoogleStock[i].low, 
                 GoogleStock[i].close, 
                 GoogleStock[i].volume, 
                 GoogleStock[i].adj_close);
          }
         cout<<endl<<endl;
      }
    if(output_bit){
        cout<<"This is the adjusted close price of Google stock"<<endl;
        pauseIT();
        setOutputPrecision(3);  
        for(int i=0;i<n;i++)
        cout<<google_stock_adj_close[i]<<endl;
    }

    double stockMedian,stockMean,stdDev,sum,avg,sum2;
    stockMedian=stockMean=stdDev=0.0;
    MedianandMean((double *) google_stock_adj_close,n, stockMedian, stockMean);
standardDeviation((double *) google_stock_adj_close, n, sum, avg, sum2, stdDev);
    cout<<"The stock median = "<<stockMedian<<endl;
    cout<<"The stock mean = "<<stockMean<<endl;
    cout<<"The stock standard deviation = "<<stdDev<<endl;
    delete []google_stock_adj_close;
    return 0;
}

   int findMaxIndex(const double *a, int size){
         int index=0;
    double max=a[0];
    for (int i=1;i<size;i++){
        if (a[i]>max){
            max=a[i];
            index=i;
        }
    }
    return index;
    }   
  double * sortArray(double *a, int size){
        int index;   
        double *sortedArray=new double[size];
        for(int i=0;i<size;i++){
            index =findMaxIndex(a, size);
            sortedArray[i]=a[index];
            a[index]=LONG_MIN;   
        }
     return sortedArray;            
    }

void MedianandMean(double * stock, int size, double& stockMedian, double& stockMean){
    stockMedian=0.0;
    double *sorted_stock=sortArray(stock,size);
       if(size%2==0)
        stockMedian=sorted_stock[(size-1)/2];
    else
        stockMedian=(sorted_stock[(size-1)/2]+sorted_stock[((size-1)/2)+1])/2;
    //Mean
    stockMean=0.0;
    for(int i=0;i<size;i++)
        stockMean=stockMean+stock[i];
    stockMean=stockMean/size;
}
void standardDeviation(double *stock, int size, double& sum, double& avg, double& sum2, double& stdDev){
     //stanadard deviation
    double temp=0.0;
    for (int i=0; i<size;i++)
    sum=sum+stock[i];
    avg=sum/float(size);
    for(int i=0; i<size; i++)
        sum2 += pow((stock[size]-avg),2);
        temp =sum2/(size-1);
    stdDev= pow(temp, 0.5);
}
  1. 在尝试集成新代码之前,应该单独开发和测试新代码
  2. 你张贴的例子既不最小也不完整。
  3. 你的代码格式不好。
  4. 你的变量存在时间太长。你从来没有初始化' sum '。