Thread: Array assignment anomaly while tackling Euler Problem 23

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    2

    Array assignment anomaly while tackling Euler Problem 23

    I am trying to do Euler Problem 23.
    The first block of code generates a list of abundant numbers and saves them to an array called abundant, while storing all the integers in an array called numbers.
    The next block zeros the array numbers at the sum of two abudant numbers, leaving a list of non abundant numbers.
    Somewhere in this block the abundant array is becoming corrupted, but as far as I know I am not assigning anything to the abundant array in this block.
    Can someone indicate where this assignment is taking place.
    I've searched the web and several forums but to no avail.
    The first few lines on the printout shows that the fifth element of the abundant array changes from 30 to zero sometime during the first pass.




    Code:
      0        12,   0         12       24
      0        12,   1         18       30
      0        12,   2         20       32
      0        12,   3         24       36
      0        12,   4         30       42
      0        12,   5         36       48
      0        12,   6         40       52
      1        18,   0         12       30
      1        18,   1         18       36
      1        18,   2         20       38
      1        18,   3         24       42
      1        18,   4          0       18
      1        18,   5         36       54
      1        18,   6         40       58
      2        20,   0         12       32
    Code:
    # include <stdio.h>
    # include <time.h> 
    
    
    int MAX=40; //20161;
    
    
    int properdivisors(int n){
        int i,sum=0;
        for (i=1;i<n;i++){
            if(n%i==0){sum+=i;}
        }return sum;
    }
    
    
    int abundantnos()
    {
        int i,j=0,k,l;
        int sum=0;
        int abundant[MAX];
        int numbers[MAX];
        for (i=1;i<=MAX;i++)
        {
            numbers[i]=i;
            if(properdivisors(i)>i)
            {
                abundant[j++]=i;
            }
        }
         /*  j holds the number of abundant numbers found  */
        for(i=0;i<j;i++)
        {
            for(k=0;k<j;k++){
                printf("%3d\t%5d, %3d\t%5d\t%6d\n",i,abundant[i],k,abundant[k],abundant[i]+abundant[k] );
                l=abundant[i]+abundant[k];
                numbers[l]=0;
            }
        }
        for(i=1;i<=MAX;i++)
        {
            if(numbers[i]!=0)
            {
                sum+=numbers[i];
            }
        }
        printf("the sum of all the positive integers which cannot\n"
         "be written as the sum of two abundant numbers is %d\n",sum);
    }
    
    
    int main()
    {
        clock_t begin, end;
        double time_spent;
        begin = clock();
            
        abundantnos();
        
        end = clock();
        time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
            printf(" and took %f seconds",time_spent);
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    In C, array indices go from 0 to one-less-than the size, e.g., int a[3] has valid indices 0, 1, 2. You are accessing outside the array when you access numbers[MAX].

    Even worse:
    Code:
                l = abundant[i] + abundant[k];
                numbers[l] = 0;
    What are the possible values of l here? What are the allowed values of an index for numbers?
    Last edited by john.c; 03-17-2018 at 07:46 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Mar 2018
    Posts
    2
    Quote Originally Posted by john.c View Post
    In C, array indices go from 0 to one-less-than the size, e.g., int a[3] has valid indices 0, 1, 2. You are accessing outside the array when you access numbers[MAX].

    Even worse:
    Code:
                l = abundant[i] + abundant[k];
                numbers[l] = 0;
    What are the possible values of l here? What are the allowed values of an index for numbers?
    Aha a bad choice of variable labels. I have used a lower case L, (l) which appears very much like digit 1 in the code section (shows up much better in the quoted section).
    The table lists the values of i, abundant[i], k , abundant[k] and abundant[i]+abundant[k]
    Well before I am getting anywhere near being outside array limits abundant[4] suddenly becomes zero instead of the previously stored value of 30.

    I have changed the code to read
    Code:
    for (i=1;i<MAX;i++)
    but that does not seem to make any difference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string array assignment problem.
    By merafiq in forum C Programming
    Replies: 1
    Last Post: 09-16-2015, 09:48 PM
  2. string array assignment problem
    By tlinetrader in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2012, 05:50 PM
  3. Tackling large amounts of code?
    By Cell in forum Tech Board
    Replies: 4
    Last Post: 02-22-2009, 08:15 PM
  4. Euler's problem #3
    By tjpanda in forum C++ Programming
    Replies: 3
    Last Post: 02-14-2009, 11:18 AM
  5. Replies: 17
    Last Post: 11-22-2008, 03:40 AM

Tags for this Thread