Thread: Help with defining constant please...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    4

    Help with defining constant please...

    Hello,

    I'm having and issue with my code.

    The code basically performs several mathematical functions on a set of numbers retrieved from a text file.

    I have 4 text files that can be chosen by the user, each text file has a unique set of "Datapoints".

    In each equation in my code, that Datapoints variable is involved.

    So, for example, I can't simply:

    Code:
    #define DATAPOINTS 10
    Because each text file has a unique value for the Datapoints.

    I've tried so many things and can't get it to work.

    Is there any way to issue the #define command, except later on give the value of Datapoints? Whenever I try that it complains during the compile, telling me that the Datapoints value is undefined.

    I can post my entire code for reference if required.

    Thanks in advance.

    PaulM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does, for example, each data file begin with a number which indicates the number of data points in the file?

    Or is there a reasonably low maximum which can be assumed - say
    #define DATAPOINTS 1000
    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.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    4
    Quote Originally Posted by Salem
    Does, for example, each data file begin with a number which indicates the number of data points in the file?

    Or is there a reasonably low maximum which can be assumed - say
    #define DATAPOINTS 1000
    Each file is basically a set of x and y co-ordinates.

    X being on the left of the file and Y being on the right.

    The "Datapoints" are the total amount of x co-ordinates.

    Each file has a maximum of 10 x co-ordinates.

    My problem is that when I define "Datapoints" as 10 for example, it uses 10 as a value of the calculations for all files, even though only one may have 10 Datapoints, the others dont.

    The amount of Datapoints is static in each file, so they don't change and I unfortunately can't indicate the number of Datapoints in each file.

    Is there a way to maybe add together the total amount of values in the X values column of the text file and then use the output as the #define for the "datapoints"?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So basically, you read pairs of data points untll the end of the file (up to a maximum)

    So keep a separate count of say
    int numDataPoints = 0;

    Which you increment for each pair read from the file.
    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.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    4
    Quote Originally Posted by Salem
    So basically, you read pairs of data points untll the end of the file (up to a maximum)

    So keep a separate count of say
    int numDataPoints = 0;

    Which you increment for each pair read from the file.
    Sorry, I'm a little confused.

    Where would I place the increment? As it requires a value for Datapoints before starting, otherwise it won't compile.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Inside the loop reading data points from the file.

    Post some code.
    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.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    4
    Quote Originally Posted by Salem
    Inside the loop reading data points from the file.

    Post some code.
    It's really hard to explain what I'm trying to say, I'll post the code so you can get a general idea, hopefully it's not too much:

    Code:
    /* ------------------- */
    /* Include Information */
    /* ------------------- */
    
    	#include <stdio.h>
    	#include <math.h>
        #define FILENAME "Misc.txt"
    	#define FILENAME2 "LRSCC.txt"
    	#define FILENAME3 "MGC.txt"
    	#define FILENAME4 "MCTC.txt"*/
    	int NUMOFDATAPOINTS;
    	/*int NUMOFDATAPOINTS;*/
    	char filename[19];
    /*	  #define NUMOFDATAPOINTS 10
    /*  	nclude <koolplotX.h> */
    	
    /* -------- */
    /* Int Main */
    /* -------- */
    
    int main(void)
    
    {
    
    /* ------------------------ */
    /* Declaration of Variables */
    /* ------------------------ */
    
       printf( "Please enter the name of the file you would\nlike to read data from: " );
       scanf( "%s",&filename );
    
       if( (strcmp( filename, "MISC.txt") == 0) || (strcmp( filename, "LRSCC.txt") == 0) || (strcmp( filename, "MGC.txt") == 0) || (strcmp( filename, "MCTC.txt") == 0) )
       		{
       		if ((strcmp( filename, "MISC.txt") == 0))
       		{
    
        	}
       		if ((strcmp( filename, "LRSCC.txt") == 0))
       		{
    
    		}
       		if ((strcmp( filename, "MGC.txt") == 0))
       		{
    ;
    		}
       		if ((strcmp( filename, "MCTC.txt") == 0))
       		{
    
    		}
    		}
       else
       	{
       	printf("Wrong info");
    	}
    
    
    	
    /* --------------------------------- */
    /* Declaration of funct. prototypes. */
    /* --------------------------------- 
    
    	int meanofxa(int positionofclay[]);
    	double meanofya(double distanceofclay[]);
    	double calcofa(double paramatera[]);
    	double calcofb(double paramaterb[]); 
    	
    /* ------------------------------------------------------- */
    /* Opening up our file and declaring the file! (read only) */
    /* ------------------------------------------------------- */
    	FILE *misc;
    	misc = fopen("MISC.txt", "r");
    
    	FILE *lrscc;
    	lrscc = fopen("LRSCC.txt", "r");
    
    	FILE *mgc;
    	mgc = fopen("MGC.txt", "r");
    
    	FILE *mctc;
    	mctc = fopen("MCTC.txt", "r");
    	     
    	
    		int count, positionofclay[NUMOFDATAPOINTS];
    	double distanceofclay[NUMOFDATAPOINTS], distance, paramatera, paramaterb;
    	double differenceofx[NUMOFDATAPOINTS], sqrta, sqrtb, correlationa;
    	double correlation, powa, meanofx, meanofy, differenceofy[NUMOFDATAPOINTS];
    	double sumofallxsquares, sumofallysquares, squareofx[NUMOFDATAPOINTS], squareofy[NUMOFDATAPOINTS];
    	double diffxbydiffy[NUMOFDATAPOINTS], countofdiffxbydiffy=0, lineofbestfit[NUMOFDATAPOINTS];
    	char windowtitle[120];
    	const char * header = "sampleX: ";
    	
    	
    	/* ---------------------------------------------------------- */
    	/* If file isn't there or can't be open, flag user with error */
    	/* ---------------------------------------------------------- */
    
    	if ((misc == NULL) || (lrscc == NULL) || (mgc == NULL) || (mctc == NULL))
    	
    		{
    	
    			printf("Can't open file, there is a problem \n");
    			
    			exit(1);
    		
    		}
    
        
    
    /* ---------------------------------------------------------- */
    /* Time to put all of the data in the misc.txt file to arrays */
    /* ---------------------------------------------------------- */
    /*
    	else 
    	
    		{
    		
    			for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    		
    				{
    				
    					fscanf(misc, "%i%lf",&positionofclay[count], &distanceofclay[count]);
    					
    				}
    				
    		}
    
    /* ------------------------------------------------------------------------------------- */
    /* This section displays the array information on screen (info obtained from "misc.txt") */
    /* ------------------------------------------------------------------------------------- */
    
    
         if( (strcmp( filename, "MISC.txt") == 0) || (strcmp( filename, "LRSCC.txt") == 0) || (strcmp( filename, "MGC.txt") == 0) || (strcmp( filename, "MCTC.txt") == 0) )
    	
       		{
    		if( (strcmp( filename, "MISC.txt") == 0) ) {
    		
    			for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    		
    				{
    				
    					fscanf(misc, "%i%lf",&positionofclay[count], &distanceofclay[count]);
    					
    				}
    	
    			printf("\nMISC.txt contains the following plot information:\n\n");
    	
    			printf("--------------\n");
    	
    				for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    				
    					{
    					
    						printf("%i\t%2.4f\n",positionofclay[count], distanceofclay[count]);
    					
    						fclose(misc);
    					
    					}
    		
    			printf("--------------\n");
    		}
    		
    		else
    			if( (strcmp( filename, "LRSCC.txt") == 0) ) {
    			
    			for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    		
    				{
    				
    					fscanf(lrscc, "%i%lf",&positionofclay[count], &distanceofclay[count]);
    					
    				}
    	
    			printf("\nLRSCC.txt contains the following plot information:\n\n");
    	
    			printf("--------------\n");
    	
    				for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    				
    					{
    					
    						printf("%i\t%2.4f\n",positionofclay[count], distanceofclay[count]);
    					
    						fclose(lrscc);
    					
    					}
    		
    			printf("--------------\n");
    		}	   	   	   
    
    			else
    			if( (strcmp( filename, "MGC.txt") == 0) ) {
    			
    			for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    		
    				{
    				
    					fscanf(mgc, "%i%lf",&positionofclay[count], &distanceofclay[count]);
    					
    				}
    	
    			printf("\nMGC.txt contains the following plot information:\n\n");
    	
    			printf("--------------\n");
    	
    				for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    				
    					{
    					
    						printf("%i\t%2.4f\n",positionofclay[count], distanceofclay[count]);
    					
    						fclose(mgc);
    					
    					}
    		
    			printf("--------------\n");
    		}	   	   	   
    		
    					else
    			if( (strcmp( filename, "MCTC.txt") == 0) ) {
    			
    			for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    		
    				{
    				
    					fscanf(mctc, "%i%lf",&positionofclay[count], &distanceofclay[count]);
    					
    				}
    	
    			printf("\nMCTC.txt contains the following plot information:\n\n");
    	
    			printf("--------------\n");
    	
    				for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    				
    					{
    					
    						printf("%i\t%2.4f\n",positionofclay[count], distanceofclay[count]);
    					
    						fclose(mctc);
    					
    					}
    		
    			printf("--------------\n");
    		}
    		}	   	   	 
    
    	
    	else
    
    		{
    
    		printf("\nYou entered an incorrect filename...it should be misc.txt or MISC.txt\n");
    		
    		exit(1);
    		
    		
    		}
    	
    	
    	
    /* -------------------------------------------------------------------------------------- */
    /* According to the calculations, the following formula must be used to compute paramater */
    /* "a" of the "y = a*x + b" formula:													  */
    /*   																				      */
    /*  			 	  	  N ( X - M(ofx) ) * ( Y - M(ofy) )				 				  */
    /*  			 		  ---------------------------------	   	   	   	   	   	   		  */
    /*   				 	 	  N ( X - M(ofx) ) ^ 2	  	  	  	    					  */
    /*																						  */
    /* So...let's do the mean x and y values first it! 	   	   	   	   	   	    			  */
    /* -------------------------------------------------------------------------------------- */
    
    	meanofx=0;
    		
    		for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    		
    			{
    			
    				meanofx = (double)positionofclay[count] + meanofx;
    				
    			}
    
    			
    	meanofx = meanofx / NUMOFDATAPOINTS;
    
    	meanofy=0;
    		
    		for(count=0; count<=NUMOFDATAPOINTS-1; count++)
    		
    			{
    			
    				meanofy = (double)distanceofclay[count] + meanofy;
    				
    			}
    			
    	meanofy = meanofy / NUMOFDATAPOINTS;
    
    /* ---------------------------------------------- */
    /* Now we can print our obtained values on screen */
    /* ---------------------------------------------- */
    
    	printf("\nAfter using the calculation method, the mean 'x' and 'y' values are:\n\n");
    		printf("MEAN of X: %2.2f\n",meanofx);
    			printf("MEAN of Y: %2.2f\n",meanofy);
    As you can see "NUMOFDATAPOINTS" is used throughout the code, so I need to to be active prior to starting the calculations, that's where I'm stick.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A separate function for reading files would help enormously here
    Code:
    int readFile ( const char *filename, int positions[], double distances[], int max ) {
      int count, result = 0;
      FILE *fp = fopen( filename, "r" );
      if ( fp != NULL ) {
        char buff[BUFSIZ];
        /* read from file until arrays are full or EOF */
        while ( result < max &&
                fgets( buff, sizeof buff, fp ) != NULL ) {
          if ( sscanf( buff, "%i %lf", &positions[result], &distances[result] ) == 2 ) {
            result++;
          } else {
            printf( "Bad format line %s\n", buff );
          }
        }
        /* file completely read, or the arrays are full */
        fclose ( fp );
    
        /* dump the data - better if it were a separate function */
        printf("\n%s contains the following plot information:\n\n", filename );
        printf("--------------\n");
        for(count=0; count<result; count++)
        {
          printf("%i\t%2.4f\n",positions[count], distances[count]);
        }
        printf("--------------\n");
      } else {
        printf( "Can't open %s\n", filename );
      }
      return result;
    }
    Then your main becomes a bit like this
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAXDATAPOINTS 10
    
    int readFile ( const char *filename, int positions[], double distances[], int max );
    
    int main(void)
    {
      int    NUMOFDATAPOINTS;
      char   filename[19];
      int    positionofclay[MAXDATAPOINTS];
      double distanceofclay[MAXDATAPOINTS];
    
      printf( "Please enter the name of the file you would\n"
              "like to read data from: " );
      scanf( "%s",filename );/*!! no & here */
    
      if ( (strcmp( filename, "MISC.txt") == 0) ||
           (strcmp( filename, "LRSCC.txt") == 0) ||
           (strcmp( filename, "MGC.txt") == 0) ||
           (strcmp( filename, "MCTC.txt") == 0) )
      {
        NUMOFDATAPOINTS = readFile ( filename, positionofclay, distanceofclay, MAXDATAPOINTS );
      }
      else
      {
        printf("\nYou entered an incorrect filename...\n"
               "it should be misc.txt or MISC.txt\n");
        exit(1);
      }
    
      return 0;
    }
    Just use NUMOFDATAPOINTS to control your later loops, as you do now.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM