Hey,
I have been working on a program (in C) for the past couple of weeks, and I am stuck with some errors that none of my research (searching forums, books, websites, ect.) as helped me figure out.
simply put, the program reads data in from a file, then does some calculations (mean, standard deviation, mean and standard deviation of blocks of data, and correlation coefficient).
the problems I am running into are:
- when writing the information read from the file into the struct, the loop is successful 11 times, on the 12th, the program crashes. the data can be read from the file to a data point successfully, but again when writing from that to the struct, it crashes on the 12t set of values.
- if a number 11 or smaller is selected, the program will get to the end, then crash on the return 0 call.
- I am getting warnings for the calls of stdev and printMean (error message - warning: passing argument 2 of 'stdev' as 'float' rather than 'double' due to prototype.)
my code is in four modules, and one data file. the files are:
driver.c
MDAnalysis.c
MDAnalysis.h
MDAnalysis.PRJ
dissipate.dat
the file contains 1800 sets of data, which are a time, and two values: dhdt and a drate
my simplified code is as follows (I have cut out functions that are working correctly, as to cut down the post):
MDAnalysis.PRJ:
Code:
driver.c
MDAnalysis.c
MDAnalysis.h:
Code:
#ifndef _MDAnalysis_h
#define _MDAnalysis_h
typedef struct
{
float time;
float dhdt;
float drate;
float diff;
int points;
} simData;
void printScreen();
int enterPoints();
simData *simDataMemory(simData *, int);
void readData(simData *);
void computeStats(simData *);
float mean(int, float[]);
float stdev(int, float, float[]);
void printMeans(float, float, float, float);
void blockAverages(simData *);
void diffAverages(simData *);
void correlCoef(simData *);
#endif
driver.c:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "MDAnalysis.h"
int main(void)
{
int pointsMain;
simData data;
printScreen();
pointsMain=enterPoints();
data = *simDataMemory(&data, pointsMain);
data.points=pointsMain;
readData(&data);
computeStats(&data);
blockAverages(&data);
diffAverages(&data);
correlCoef(&data);
free(&data);
return 0; /*program crashes here*/
}
MDAnalysis.c:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include "MDAnalysis.h"
void printScreen()
{/*Prints some info to screen*/}
int enterPoints()
{/*Gets user to enter how many data points to be read from file*/}
simData *simDataMemory(simData* dataMemory, int pointsMemory)
{
/*allocate memory to struct*/
simData *dataMemory2 = (simData*)malloc(pointsMemory * sizeof(dataMemory));
if (dataMemory2 == NULL)
{
printf("Error allocating memory");
exit(1);
}
return dataMemory2;
}
void readData(simData* dataRead)
{
/*populate struct with data from file*/
FILE* dataFile;
int i;
int pointsRead=dataRead[0].points;
dataFile=fopen("dissipate.dat", "r");
if(dataFile==NULL)
{
printf("Error opening file.");
exit(1);
}
for(i=0; i<pointsRead; i++)
{
fscanf(dataFile, "%f %f %f", &dataRead[i].time, &dataRead[i].dhdt, &dataRead[i].drate); /*this is the part that crashes on it's 12th run*/
}
fclose(dataFile);
return;
}
void computeStats(simData* dataSim)
{
/*calls functions to calculate mean and standard deviation, then prints results*/
float dhdtMean;
float dhdtSD;
float drateMean;
float drateSD;
int pointsStats = dataSim[0].points;
float dhdtStatPoints[pointsStats];
float drateStatPoints[pointsStats];
int i;
for(i=0; i<pointsStats; i++)
{
dhdtStatPoints[i] = dataSim[i].dhdt;
}
for(i=0; i<pointsStats; i++)
{
drateStatPoints[i] = dataSim[i].drate;
}
dhdtMean = mean(pointsStats, dhdtStatPoints);
dhdtSD = stdev(pointsStats, dhdtMean, dhdtStatPoints);
drateMean = mean(pointsStats, drateStatPoints);
drateSD = stdev(pointsStats, drateMean, drateStatPoints);
printMeans((float)dhdtMean, (float)dhdtSD, (float)drateMean, (float)drateSD);
return;
}
float mean(int pointsMean, float valuesMean[])
{
/*calculates mean*/
int i;
float temp;
float meanMean;
i=0;
temp=0;
meanMean=0;
for(i=0; i<pointsMean; i++)
{
temp+=valuesMean[i];
}
meanMean=temp/pointsMean;
return meanMean;
}
float stdev(int pointsStdev, float meanStdev, float valuesStdev[])
{
/*calculates standard deviation*/
int i;
float tempA;
float tempB;
float tempC;
float tempD;
float stdevStdev;
tempA=0;
tempB=0;
tempC=0;
tempD=0;
stdevStdev=0;
for(i=0; i<pointsStdev; i++)
{
tempA=meanStdev-valuesStdev[i];
tempB=pow(tempA, 2.0);
tempC+=tempB;
}
tempD=tempC/(pointsStdev-1);
stdevStdev=sqrt(tempD);
return stdevStdev;
}
void printMeans(float dhdtMeanPrint, float dhdtStdevPrint, float drateMeanPrint, float drateStdevPrint)
{/*prints calculated values*/}
void blockAverages(simData* dataSim)
{/*splits data into defined number of bloacks*/}
void diffAverages(simData* dataSim)
{/*finds mean and standard deviation of the difference between dhdt and drate values*/}
void correlCoef(simData* dataSim)
{/*finds correlation coefficient*/}
any thoughts would be greatly appreciated, and let me know if any more information is needed.
Thank you in advance,
Martin