help debugging program.

This is a discussion on help debugging program. within the C Programming forums, part of the General Programming Boards category; Hey, I have been working on a program (in C) for the past couple of weeks, and I am stuck ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Melbourne Australia
    Posts
    2

    help debugging program.

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > simData *dataMemory2 = (simData*)malloc(pointsMemory * sizeof(dataMemory));
    Well this isn't going to be enough memory to begin with.

    The first parameter is useless.
    What you should have is
    simData *dataMemory2 = malloc( pointsMemory * sizeof(*dataMemory2) );
    Note: there is no cast on the return result of malloc.

    > data = *simDataMemory(&data, pointsMain);
    And this will not make data magically bigger. It's still only one data element in size.

    What you should have here is something like
    Code:
        simData *data = NULL;
        data = *simDataMemory(data, pointsMain);  // passing data here does nothing
        data->points=pointsMain;
        readData(data);
        // and so on
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Code:
    simData data;
    your data variable should be a POINTER, as that's what you're getting back from simDataMemory.

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    simData data;
    
    ...
    
    data = *simDataMemory(&data, pointsMain);
    I'm fairly certain that this is where your problems really start. simDataMemory() is supposed to return a pointer to a whole block of memory.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Melbourne Australia
    Posts
    2
    that got it working

    still getting the passing argument 2 of 'stdev' as 'float' rather than 'double' due to prototype error message (only with all warnings turned on), but I think that might just be my compiler.

    thank you again,

    Martin

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Quote Originally Posted by martin249 View Post
    still getting the passing argument 2 of 'stdev' as 'float' rather than 'double' due to prototype error message (only with all warnings turned on), but I think that might just be my compiler.
    I believe your warning is because C and C++ automatically promote floats to doubles in the call to the function, then demotes them to float on return. Nowadays you should be using doubles instead of floats anyway, except for situations where space is at an absolute premium.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging strings program help plz
    By allen9190 in forum C Programming
    Replies: 1
    Last Post: 11-08-2009, 08:17 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21