common.h :
Code:
#ifndef COMMON_H
#define COMMON_H
#define LDATFILE "eight.bi.op.left.m0.03000.m0.03000.PxP"
#define RDATFILE "eight.bi.op.right.m0.03000.m0.03000.PxP"
#define T_SUP 96 //supremum of t (L value)
#define STERR(avg,avgsqr,n) sqrt(((avgsqr)-((avg)*(avg)))/((double)n-1.0))
#endif
typedef struct data
{
int t;
double Re;
double Im;
} Data;
extern void errmsg(int);
main.c :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "common.h"
int main(){
int i,j,n;
char str[50];
double* Lavg,* Lavgsqr,* Lsterr;
double* Ravg,* Ravgsqr,* Rsterr;
FILE* Lfp,* Rfp,* ofp;
//Count the number of the data
Lfp=fopen(LDATFILE,"r");
Rfp=fopen(RDATFILE,"r");
if(Lfp==NULL||Rfp==NULL) {errmsg(1); return 1;} //if no such file in directory
n=0;
while(!feof(Lfp)){
fscanf(Lfp,"%s",str);
if(!strcmp(str,"TYPE:")) n++;
}
fclose(Lfp);
fclose(Rfp);
//Assign memory to the data
Dat.......... Ldata,** Rdata;
Ldata=(Dat..........)malloc(n*sizeof(Data*));
Rdata=(Dat..........)malloc(n*sizeof(Data*));
if(Ldata==NULL||Rdata==NULL){errmsg(2); return 2;} //if memory allocation failed
for(i=0;i<n;i++){
Ldata[i]=(Data*)malloc(T_SUP*sizeof(Data));
Rdata[i]=(Data*)malloc(T_SUP*sizeof(Data));
if(Ldata[i]==NULL||Rdata[i]==NULL){errmsg(2); return 2;}
}
//Write the data to the assigned memory
Lfp=fopen(LDATFILE,"r");
Rfp=fopen(RDATFILE,"r");
for(i=0;i<n;i++){
fgets(str,50,Lfp);
fgets(str,50,Rfp);
for(j=0;j<T_SUP;j++){
fscanf(Lfp,"%d",&Ldata[i][j].t);
fscanf(Lfp,"%lf",&Ldata[i][j].Re);
fscanf(Lfp,"%lf",&Ldata[i][j].Im);
fscanf(Rfp,"%d",&Rdata[i][j].t);
fscanf(Rfp,"%lf",&Rdata[i][j].Re);
fscanf(Rfp,"%lf",&Rdata[i][j].Im);
}
fseek(Lfp,1,SEEK_CUR);
fseek(Rfp,1,SEEK_CUR);
}
fclose(Lfp);
fclose(Rfp);
//Assign memory to statistical variables
Lavg=(double*)malloc(T_SUP*sizeof(double));
Lavgsqr=(double*)malloc(T_SUP*sizeof(double));
Lsterr=(double*)malloc(T_SUP*sizeof(double));
if(Lavg==NULL||Lavgsqr==NULL||Lsterr==NULL) {errmsg(2); return 2;}
Ravg=(double*)malloc(T_SUP*sizeof(double));
Ravgsqr=(double*)malloc(T_SUP*sizeof(double));
Rsterr=(double*)malloc(T_SUP*sizeof(double));
if(Ravg==NULL||Ravgsqr==NULL||Rsterr==NULL) {errmsg(2); return 2;}
//Statistical analysis
for(j=0;j<T_SUP;j++){
for(i=0;i<n;i++){
Lavg[j]+=Ldata[i][j].Re;
Ravg[j]+=Rdata[i][j].Re;
Lavgsqr[j]+=(Ldata[i][j].Re)*(Ldata[i][j].Re);
Ravgsqr[j]+=(Rdata[i][j].Re)*(Rdata[i][j].Re);
}
Lavg[j]/=(double)n;
Ravg[j]/=(double)n;
Lavgsqr[j]/=(double)n;
Ravgsqr[j]/=(double)n;
Lsterr[j]=STERR(Lavg[j],Lavgsqr[j],n);
Rsterr[j]=STERR(Ravg[j],Ravgsqr[j],n);
}
ofp=fopen("temp.dat","w");
for(j=0;j<T_SUP;j++){
fprintf(ofp,"%.14E %.14E\n",Lavg[j],Lavgsqr[j]);
}
return 0;
}
this is my main code..