Thread: Parsing comma separated integers and determining numbers 1-64, and all numbers unique

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    38

    Parsing comma separated integers and determining numbers 1-64, and all numbers unique

    I am having an issue with this piece of code. For some reason the 1st integer brought back is always 0.
    The input file looks like this, which is stored in
    Code:
    lineBuffer
    IP=6,51,64,50,29,21,37,35,34,63,48,42,8,13,52,46,1 7,9,54,25,61,26,28,45,15,43,44,18,49,20,32,10,40,3 1,30,57,56,60,4,12,53,47,5,22,58,27,38,23,3,11,19, 1,2,55,33,14,24,39,36,59,62,41,16,7

    Code:
        char *readChar[64];
        int charCount = 0;
        char *p;
        int i, j;
        int countArray[64] = {0};
    
    
    lineBuffer = lineBuffer + 3;
                for (p = strtok(lineBuffer, ","); p; p = strtok(NULL, ",")) {
                    if (charCount > 64) {
                        printError(3);
                    }
                    readChar[charCount++] = p;
                }
    
    
                if (charCount < 64) {
                    printError(3);
                }
    
    
                fprintf(stdout, "%i\n", atoi(readChar[0]));
    
    
    
    
                for (i = 0; i < charCount; i++) {
                    countArray[atoi(readChar[i])]++;
                }
                
    
    
                for (i = 0; i < charCount; i++) {
                    fprintf(stdout, "%i occures %i\n", atoi(readChar[i]), countArray[i]);
                }
    
    
    
    
                for (i = 0; i < charCount; ++i) {
                    if (atoi(readChar[i]) > 64) {
                        printError(5);
                    }
                    for (j = i + 1; j < charCount; ++j) {
                        if (atoi(readChar[i]) == atoi(readChar[j])) {
                            printError(4);
                        }
                    }
                }
    For some reason, no matter what I get the following

    Code:
    0 occures 0
    51 occures 1
    64 occures 1
    50 occures 1
    29 occures 1
    21 occures 1
    37 occures 1
    35 occures 1
    34 occures 1
    63 occures 1
    48 occures 1
    All of the other numbers seem to come through just fine. Can anyone help with this? I would greatly appreciate it. Thank you in advance!

  2. #2
    Registered User
    Join Date
    Feb 2016
    Posts
    38
    Just to clarify, the line
    Code:
     fprintf(stdout, "%i\n", atoi(readChar[0]));
    is just to test to make sure that in fact index 0 is the number 6. It does print 6, I just didn't include it in the printout at the bottom here.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    38
    Also, the reason I included both a check for duplicates, and a check for occurrences is for later in the code.

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    38
    I have narrows down the problem to the following piece
    Code:
    for (i = 0; i < charCount; i++) {
    				countArray[atoi(readChar[i])]++;
    			}

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    In the string you display there is a space between the digits of the numbers 17 and 31. That may not be in your actual code, but check it out.

    The error you're experiencing is due to writing beyond the end of the countArray. You only allocated 64 elements, with indices 0 to 63, but you're accessing element offset 64 in your code. That just happens to coincide with the first element of the readChar array (apparently), and so it's overwritten.

  6. #6
    Registered User
    Join Date
    Feb 2016
    Posts
    38
    That was exactly it for the digit, but it is still showing as 0 for the count. Not sure what the problem is.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Are you sure that you didn't want something like this instead?
    Code:
    for (i = 0; i < charCount; ++i)
    {
       if (countArray[i] > 0)
          fprintf(stdout, "%i occurs %i times\n", i, countArray[i]);
    }
    I mean, it doesn't make sense to use atoi() over and over when you already have the data. As far as you know, it could be the source of the problem. Whenever atoi(arg); makes a mistake, it returns 0. And keep in mind that countArray[0] should be 0. It isn't in your data set.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in comma separated numbers from a data file
    By littlemslexii in forum C Programming
    Replies: 8
    Last Post: 04-22-2013, 09:41 AM
  2. Replies: 11
    Last Post: 11-27-2012, 05:45 AM
  3. A program to seperate numbers by comma
    By 7heavens in forum C Programming
    Replies: 1
    Last Post: 10-07-2009, 03:18 AM
  4. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  5. Replies: 4
    Last Post: 03-03-2003, 03:52 PM