-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 に答える