-1

您好,有人可以帮忙吗,我的 SD 和均值数字非常奇怪,我不知道为什么。我觉得我可能以错误的方式访问数组?有一个结构,但也有一个独立的数组,这是我正在使用的。我的中位数很好,但其他一切都是非常奇怪的输出。

#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 %s\n", 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  %lf\n", 
                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  %lf\n", 
                 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);

}
4

1 回答 1

4
  1. 在尝试集成新代码之前, 您应该单独开发和测试新代码。
  2. 您发布的示例既不是最小的也不是完整的。
  3. 您的代码格式不正确。
  4. 您的变量存在时间过长。
  5. 你永远不会初始化 `sum`。
于 2013-03-26T18:10:23.983 回答