我需要运行它,这样我就可以分析我实验室的一些数据。但是,当程序运行并且我输入“n”的值时,它只会崩溃。有关如何纠正此问题的任何提示?
谢谢,代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
FILE *magfield;
FILE *means;
FILE *variances;
double *mean;
double *variance;
double field[12000];
double time[12000];
double sum=0, squares=0;
int i, j=0, k=0, l=0, n=0;
magfield=fopen("C:\\Users\\Owner\\Documents\\Homework\\ILab\\magneticfield.txt","r");
means=fopen("C:\\Users\\Owner\\Documents\\Homework\\ILab\\means.txt","w");
variances=fopen("C:\\Users\\Owner\\Documents\\Homework\\ILab\\variances.txt","w");
for (i=0;i<12000;i++)
{
fscanf(magfield,"%f %f", &time[i], &field[i]);
//printf("%f %f\n", time[i], field[i]);
}
printf("How many data points would you like to be analyzed at a time?\n");
scanf("%i", &n);
mean=(double*)calloc(n,sizeof(double));
variance=(double*)calloc(n,sizeof(double));
for (i=0;i<12000;i+=n)
{
for(j=i;j<n;j++)
{
sum+=field[j];
squares+=field[j]*field[j];
}
mean[k]=sum/n;
variance[k]=squares/n-(mean[k]*mean[k]);
fprintf(means,"%f\n", mean[k]);
fprintf(variances,"%f\n", variance[k]);
sum=0;
squares=0;
k++;
}
free(mean);
free(variance);
printf("Press enter to continue...\n");
getch();
return 0;
}