Thread: Two Array Overlap Problem

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    8

    Two Array Overlap Problem

    Hello,

    For some reason my array is getting push over to other array. Please see the output. I couldnt figure out why it doing that.

    I have two functions and when output the array inside the function it fine. However, when I tried to output it through main() after I called the other function its overlap.

    Code:
    void setYearTotals (struct NameRecord records[], int size, int yearRangeTotal[]);
    void setNameYearTotals (char theName[], struct NameRecord records[], int size, int nameTotal[]);
    
    
    int main (){        char name[31];
            char upstr[31];
            FILE* fp;
            int a;
            int currSize;
            int yearRangeTotal[17];
            int TotalName[17];
            double perHun[17];
            printf ("Welcome to the Name Popularity Checker\n");
            printf ("======================================\n");
            printf ("Please Enter a name: ");
            scanf (" %[A-Za-z]", &name);
            allCaps(name);
            printf ("\nNumber of Babies named %s per 100,000 births\n", name);
            currSize = getRawData(fp,records,currSize);
            printf("%d\n",currSize);
            setYearTotals (records,currSize,yearRangeTotal);
            for (a=0 ; a < 18 ; ++a){
            printf("Range Total BEFORE %d: %d\n", a, yearRangeTotal[0]);
            }
            setNameYearTotals (name,records,currSize,TotalName);
            for (a=0 ; a < 18 ; ++a){
                   printf("Range Total After %d: %d\n", a, yearRangeTotal[a]);
            }
    
    
    }

    OUTPUT:
    Range Total BEFORE 0: 340706
    Range Total BEFORE 1: 327332
    Range Total BEFORE 2: 303722
    Range Total BEFORE 3: 304979
    Range Total BEFORE 4: 372039
    Range Total BEFORE 5: 500894
    Range Total BEFORE 6: 610618
    Range Total BEFORE 7: 716708
    Range Total BEFORE 8: 716545
    Range Total BEFORE 9: 605169
    Range Total BEFORE 10: 578307
    Range Total BEFORE 11: 557275
    Range Total BEFORE 12: 583300
    Range Total BEFORE 13: 636420
    Range Total BEFORE 14: 650440
    Range Total BEFORE 15: 563901
    Range Total BEFORE 16: 548938
    Range Total BEFORE 17: 563272
    NAME TOTAL IN FUN 0: 50
    NAME TOTAL IN FUN 1: 60
    NAME TOTAL IN FUN 2: 44
    NAME TOTAL IN FUN 3: 13
    NAME TOTAL IN FUN 4: 0
    NAME TOTAL IN FUN 5: 5
    NAME TOTAL IN FUN 6: 62
    NAME TOTAL IN FUN 7: 252
    NAME TOTAL IN FUN 8: 333
    NAME TOTAL IN FUN 9: 338
    NAME TOTAL IN FUN 10: 234
    NAME TOTAL IN FUN 11: 155
    NAME TOTAL IN FUN 12: 77
    NAME TOTAL IN FUN 13: 57
    NAME TOTAL IN FUN 14: 61
    NAME TOTAL IN FUN 15: 42
    NAME TOTAL IN FUN 16: 28
    NAME TOTAL IN FUN 17: 20
    Range Total After 0: 20
    Range Total After 1: 327332
    Range Total After 2: 303722
    Range Total After 3: 304979
    Range Total After 4: 372039
    Range Total After 5: 500894
    Range Total After 6: 610618
    Range Total After 7: 716708
    Range Total After 8: 716545
    Range Total After 9: 605169
    Range Total After 10: 578307
    Range Total After 11: 557275
    Range Total After 12: 583300
    Range Total After 13: 636420
    Range Total After 14: 650440
    Range Total After 15: 563901
    Range Total After 16: 548938
    Range Total After 17: 563272



  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There is no element number 17 in a 17 element array. The elements are numbered from 0 to 16. So either don't write to element 17 or declare the array with 18 elements.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    I dont understand what you are trying to say. Sorry. Are you saying there no data inside the 17 array? But as you can see from out put there is data from 0 - 17 for both arrays.
    the yearRangeTotal array of element 0 only change after I called the function " setNameYearTotals (name,records,currSize,TotalName);".

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    C will attempt to put data after the end of an array if you ask it to, and it might even work (with a particular system). But it is still a serious error. In your case, since the two arrays are contiguous in memory, it causes the out-or-range element to appear in the other array.


    Since C arrays are zero-based, asking for 17 elements gives you access to elements numbered from 0 to 16 (which is 17 elements). If you only ask for 17 elements then it is an error to attempt to access the element at offset 17.


    To fix your problem, you need to set aside room for 18 elements, not 17.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    Could you give me an example?
    The range after suppose to have 340706 not 20. Just like the
    Range Total BEFORE 0: 340706
    Range Total After 0: 20

    IT support be:
    Range Total After 0: 340706
    Last edited by Phuoc Phan; 11-27-2013 at 06:27 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Counting to 17 is tough. Let's try counting to four instead.

    Code:
    int array[4]; //I have four elements
    array[0] = 1; //This is an element (one so far)
    array[1] = 3; //This is an element (two so far)
    array[2] = 5; //This is an element (three so far)
    array[3] = 7; //This is an element (four so far and therefore the last one)
    array[4] = PAIN; //This is NOT an element (we are out of elements)

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What part of:

    To fix your problem, you need to set aside room for 18 elements, not 17.
    are you unsure about?

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    That's my function that generate the two files. I tried making it 18 element but it didn't solve the problem.
    Code:
    void setNameYearTotals (char theName[], struct NameRecord records[], int size, int nameTotal[]){
    int e;
    int b,c;
    int x;
    int year = 1920;
    int total;
    
    
    for (e=0 ; e < 18 ; ++e){
            total = 0;
      for (b=0; b < 5 ; ++b){
            year++;
            for ( x = 0; records[x].year != -1; x++){
             if ( year == records[x].year){
                    if ( strcmp(theName,records[x].name) == 0){
                    total = total + records[x].frequency;
                    }
             }
            }
     }
    nameTotal[e] = total;
    printf("NAME TOTAL IN FUN %d: %d\n",e, nameTotal[e]);
    }
    }
    
    
    
    
    
    
    
    
    void setYearTotals (struct NameRecord records[], int size, int yearRangeTotal[]){
    int a;
    int b,c;
    int x;
    int year = 1920;
    int pop;
    //char str[31];
    //printf("%s\n",records[size].name);
    
    
    for (a=0 ; a < 18 ; ++a){
     pop = 0;
      for (b=0; b < 5 ; ++b) {
            year++;;
            for ( x = 0; records[x].year != -1; x++){
            //printf("%s\n",records[x].name);
             if ( year == records[x].year){
                    pop = pop + records[x].frequency;
             }
            }
     //printf("Year in loop: %d\n",year);
    
    
     }
    yearRangeTotal[a] = pop;
    printf("Year-Range%d: %d\n",a,yearRangeTotal[a]);
    //printf("year: %d\n",year);
    }
    }

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    DO NOT USE MAGIC NUMBERS!
    Magic number (programming) - Wikipedia, the free encyclopedia

    FYI: An 17 member array also DOES NOT have an element numbered 18 in addiction to NOT having one number 17 in C!

    Edit: Shown the correct way to fix your issue below.

    Code:
    #define SIZE_TOTAL_NAME 18
    Code:
    int TotalName[SIZE_TOTAL_NAME];
    Code:
    for (e=0 ; e < SIZE_TOTAL_NAME ; ++e){
    Code:
    nameTotal[e] = total;
    Tim S.
    Last edited by stahta01; 11-27-2013 at 09:26 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    Thank guys. And thank you the tips stahta01. My teacher didn't teach us that.
    I didnt know what you guy mean before. I got it working. Sorry.
    Last edited by Phuoc Phan; 11-27-2013 at 09:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Overlap and memcpy function.
    By Mr.Lnx in forum C Programming
    Replies: 5
    Last Post: 07-21-2013, 03:10 PM
  2. Replies: 77
    Last Post: 10-15-2011, 04:45 PM
  3. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  4. Memory overlap in function call
    By wots_guge in forum C Programming
    Replies: 5
    Last Post: 06-06-2006, 03:22 AM
  5. making a absolute overlapped window, even overlap games
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2004, 08:53 AM

Tags for this Thread