Variable not incrementing correctly.

This is a discussion on Variable not incrementing correctly. within the C Programming forums, part of the General Programming Boards category; In this code, the integer "m" is supposed to be counting the number of times the program encounters a value ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    38

    Variable not incrementing correctly.

    In this code, the integer "m" is supposed to be counting the number of times the program encounters a value less than zero (I'm measuring rainfall; -999.99 is "missing data"), while "n" is supposed to be counting the number of data points the program is generating. We need this output so that we can gauge the amount of error that having to skip over missing data points incurs, but "m" is only ever giving me 0, which I know is not true.

    Can anyone spot my problem, please? I need a fresh set of eyes.

    Code:
    /*********************************************************************
    * This program is designed to calculate a series of autocorrelations *
    * based on a set of data that is called when the program is run.     *
    * The set of data needs to be in "float" format (i.e., nothing but   *
    * numbers), or else the program will generate an error.  The         *
    * resulting file will also be a series of numbers, meant to go into  *
    * some kind of graphing program to plot the original data against    *
    * the values of the autocorrelations for each point.                 *
    *********************************************************************/
    
    /* Autocorrelation Program, Written By Don Ford */
    
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <dirent.h>
    
    int main(int argc, char *argv[]){
        FILE* dataFile;
        FILE* outputFile;
        char string[80];
        char *old_filename;
        char new_filename[256];
        double x[1000];
        int i = 0;
        int j;
        int number_of_values;
        double x_sum = 0.0;
        double x_average;    
        int k;
        int t;
        double sum_of_squares = 0.0;
        double numerator = 0.0;    
        char buffer[80];
        int m;
        int n;
        
        for (j=1; j<argc; j++){
            i = 0;
            m = 0;
            n = 0;
            sum_of_squares = 0.0;
            numerator = 0.0;
            x_sum = 0.0;
            old_filename = argv[j];
            dataFile = fopen(old_filename, "r");
                     if (dataFile == NULL){
                        fputs("File error", stderr);
                        exit(1);
                        }
            printf("Autocorrelating \"%s\" ...\n", old_filename);
    
            while (fgets(string, 80, dataFile) != NULL){
                  if (!isalpha(string[0])){
                     x[i] = strtof(string, NULL);
                     if (x[i] >= 0){
                        i++;
                        }
                     }
                  }          
            fclose(dataFile);
            number_of_values = i;
            
            double values = number_of_values;
            double y[number_of_values];
    
            for (i=0; i<number_of_values; i++){
                if (x[i] >= 0){
                   x_sum += x[i];
                   }
                }
            x_average = (x_sum/values);
            
            /* Now, we actually calculate the autocorrelation! */
            for (t=0; t<number_of_values; t++){
                if (x[t] >= 0){
                   sum_of_squares += ((x[t] - x_average)*(x[t] - x_average));
                   }
                }
            
            for (k=0; k<(number_of_values/2); k++){
                for (t=0; t<(number_of_values - k); t++){
                    if ((x[(t+k)] >= 0) && (x[t] >= 0)){
                       numerator += ((x[(t+k)] - x_average)*(x[t] - x_average));
                       }
                    else
                         m++;
                    n++;                
                    }
                                  
                y[k] = (numerator/sum_of_squares);
                numerator = 0.0;
                }
    
            for (i=0; i<strlen(old_filename), old_filename[i] != '.'; i++){
                new_filename[i] = old_filename[i];
                }
            new_filename[i] = '-';
            new_filename[i+1] = 'A';
            new_filename[i+2] = 'U';
            new_filename[i+3] = 'T';
            new_filename[i+4] = 'O';
            new_filename[i+5] = '.';
            new_filename[i+6] = 't';
            new_filename[i+7] = 'x';
            new_filename[i+8] = 't';
            new_filename[i+9] = '\0';
                   
            outputFile = fopen(new_filename, "wb");
            if (outputFile == NULL){
               fputs("File error", stderr);
               exit(2);
               }
          
            snprintf(buffer, 80, "%d entries skipped out of a total of %d\0",m,n);
            fputs(buffer, outputFile);
            fputs("\n", outputFile);
            snprintf(buffer, 80, "Lag    Autocorrelation\0");
            fputs(buffer, outputFile);
            fputs("\n", outputFile);
            for (i=0; i<(number_of_values/2); i++){
                snprintf(buffer, 80, "%3d      %.8f\0", i, y[i]);
                fputs(buffer, outputFile);
                fputs("\n", outputFile);
                }
            fclose(outputFile);
            printf("File created: \"%s\"\n", new_filename);
            }
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    That can only mean one thing i.e. the m++ branch is never taken. Put debugging printf()'s in your code to see if the branch condition is being met
    Code:
    for (k=0; k<(number_of_values/2); k++) {
        /* print the value of k */
        for (t=0; t<(number_of_values - k); t++) {
            /* print t, x[t] and x[t+k] or whatever seems fit */
            if ((x[(t+k)] >= 0) && (x[t] >= 0)) {
                numerator += ((x[(t+k)] - x_average)*(x[t] - x_average));
            }
            else
                m++;

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Quote Originally Posted by itCbitC View Post
    Put debugging printf()'s in your code to see if the branch condition is being met
    I disapprove of using this approach, perhaps using the debugger ( hopefully you have one ) would be a better idea. If you do not have one, I'd seriously consider getting one, that way you won't have to go down the road of "debugging printf's", making your code alot cleaner and saving time.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    I just figured it out.

    The !isalpha line at the beginning of the code was ignoring all hyphens at the start of strings, so I wasn't even picking those up in the program.

    This has since been fixed. Thanks to all for helping.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Code:
    for (i=0; i<strlen(old_filename), old_filename[i] != '.'; i++){
        new_filename[i] = old_filename[i];
    }
    new_filename[i] = '-';
    new_filename[i+1] = 'A';
    new_filename[i+2] = 'U';
    new_filename[i+3] = 'T';
    new_filename[i+4] = 'O';
    new_filename[i+5] = '.';
    new_filename[i+6] = 't';
    new_filename[i+7] = 'x';
    new_filename[i+8] = 't';
    new_filename[i+9] = '\0';
    Can be simplified to:

    Code:
    char * cptr;
    
    ...
    
    if( (cptr = strchr(old_filename,'.')) != NULL )
        sprintf(new_filename,"%.*s-AUTO.txt",cptr-old_filename,old_filename);
    ...or just about anything other than what you had.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashes I suspect variable n.
    By Witch in forum C Programming
    Replies: 7
    Last Post: 04-01-2010, 03:31 AM
  2. Replies: 12
    Last Post: 05-28-2008, 04:10 AM
  3. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  4. About classes and HINSTANCE variable to Main
    By conright in forum Windows Programming
    Replies: 2
    Last Post: 01-01-2003, 07:00 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

Tags for this Thread


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