示例#1
0
文件: ue01.c 项目: ninaa123/ue01_bsp3
int main(int argc, char** argv) 
{
    // DECLARATIONS
    
    int numParticipants=argc-1;
    
    int *answers=(int *)malloc(NQUESTIONS*sizeof(int)); 

    if(answers==0)
    {
        fputs("Malloc (answers) returned null!\n",stderr);
        return -1;
    }
    
    // CRUDE ARGUMENT CHECKING
    if(argc<2) fputs("Error: No argument supplied!\n", stderr);
    
    
    for(int i=0;i<NQUESTIONS;++i)
    {
        answers[i]=(myrand()%4+1);
    }
    
    ///////////////////////////////////////////////////////////////////
    // 3a) how many right answered question a single participant have? 
    ///////////////////////////////////////////////////////////////////
    FILE *f;
    int *r=(int *)malloc(numParticipants*sizeof(int)); 
    if(r==0)
    {
        fputs("Malloc (r) returned null!\n",stderr);
        return -1;
    }
    
    f=fopen("rightanswers_pp.txt","w");
    fprintf(f,"number of right answers per participant:\n");
    for(int i=0;i<numParticipants;++i) 
    {
        r[i]=numberRightAnswersPerson(argv[i+1], answers);
        fprintf(f,"participant %d : %d \n", i+1, r[i]); //write right answer per participant in file
    }
    fclose(f);        
        
    ///////////////////////////////////////////////////////////////////
    // 3c)1) average value, deviation value, median
    ///////////////////////////////////////////////////////////////////
    
    //average
    
    double rAvg=calcAvg(r,numParticipants);
    
    printf("average how many right answered question per person %lf\n",rAvg);
    
    //standard deviation
    printf("standard deviation: %lf\n",calcStdDev(rAvg,r,numParticipants));
        
    //median
    printf("median: %lf\n",calcMedian(r,numParticipants));
    printf("\n");
    
    ///////////////////////////////////////////////////
    // 3b) calculate right answers of all per question
    ///////////////////////////////////////////////////
    
    FILE *f2;
    
    int sumRightAnswers[NQUESTIONS];
    for(int i=0;i<NQUESTIONS;++i)
    {
        sumRightAnswers[i]=0;
    }
    
    f2=fopen("rightanswers_all.txt","w");
    fprintf(f2,"number of all right answers per question:\n");
    for(int i=0;i<numParticipants; ++i) 
    {
        if (numberRightAnswersAllPersons(argv[i+1],sumRightAnswers, answers)!=0)
        {
            perror("Error reading file");
        }
    }
    
    for(int i=0;i<NQUESTIONS;++i)
    {
        fprintf(f2,"%c : %d\n",'A'+i,sumRightAnswers[i]);
    }
    fclose(f2);
    
    //average how many persons answered a single question right

    double av_all=calcAvg(sumRightAnswers,NQUESTIONS);
    printf("average how many persons answered a single question right: %lf\n",av_all);
    
    //standard deviation
    printf("standard deviation: %lf\n",calcStdDev(av_all,sumRightAnswers,NQUESTIONS));
    
    //median
    printf("median: %lf\n",calcMedian(sumRightAnswers,NQUESTIONS));
    
    if(answers!=0)
    {
        free(answers);
        answers=0;
    }
    
    if(r!=0)
    {
        free(r); 
        r=0;
    }

    return EXIT_SUCCESS;
}
int AutoCorr::countPeaks(const float* data, float &stdDev, int len, int peaks[])
{
    const float epsilon = 0.0001;
    int result = 0;

    if (len < 2)
    {
        return 0;
    }

    int lastPeak = -1;

    bool curr_up = true;

    //Count borders, too
    if (data[0] > data[1])
    {
        peaks[result] = 0;
        result++;
        lastPeak = 0;
        curr_up = false;
    }

    int* distances = new int[len];

    //Search for peaks
    for (int i = 1; i < len - 1; i++)
    {
        bool next_up = curr_up;
//		if (data[i] > data[i-1])
        if (data[i] - data[i-1] > epsilon)
        {
            next_up = true;
        }
//		if (data[i] < data[i-1])
        if (data[i] - data[i-1] < -epsilon)
        {
            next_up = false;
        }
        if (next_up == false && curr_up == true)
        {
            //peak detected
            if (lastPeak != -1)
            {
                distances[result-1] = lastPeak - i;
            }
            peaks[result] = i - 1;
            result++;
            lastPeak = i - 1;
        }
        curr_up = next_up;
    }

    if (data[len-1] > data[len-2])
    {
        if (lastPeak != -1)
        {
            distances[result-1] = lastPeak - (len - 1);
        }
        peaks[result] = len - 1;
        result++;
    }

    stdDev = calcStdDev(distances, result - 1);

    return result;
}