Scope of appended elements

42 Views Asked by At

I am writing a C program that, for a given array, will split it into two arrays, which will contain elements smaller or greater than a certain user-defined value.

The problem is that whilst my for loop does create the correct lists, which can be seen by inserting a printf statement like so:

if(iList_1[n] <= boarder){
    iList_2small[step_L2] = iList_1[n];
    printf("%i ", iList_2small[step_L2]);
    step_L2 += 1;
}

It seems to me that this list is only within the scope of the first for loop. Because when I try to print the lists after all elements have been identified, the list somehow contains seemingly no elements as indicated by size_t len2 = sizeof(iList_2small)/sizeof(int); returning len2 = 0. Or is simply equal to the original list, as indicated by the fact that I can print out every one of the 11 elements of the original array printf("\n\n%i\n\n", iList_2small[10]); printing 56.

This problem is really confusing me as I according my Python knowledge this should work. However this being C, it doesn't. That's the code:

// Dividing a List into two Lists who's elements are <=X or >X
int main()
{
    int n;
    int boarder;
    int iList_1[] = {1, 23, 15, 90, 78, 234, 987, 2, 0, 13, 56};
    int iList_2small[] = {};
    int iList_3large[] = {};

    //Calculate the number of elements in the array
    size_t len1 = sizeof(iList_1) / sizeof(int);
    printf("The Array iList_1 is %llu long\n", len1);

    //Where to divide the array?
    printf("At what value should the array iList_1 be split? (ONLY INEGERS ALLOWED)\n");
    scanf("%i", &boarder);
    printf("\nConfirming boarder value of: %i \n\n", boarder);

    //Dividing the arrays at the boarder value
    for(n=0; n < len1; n++){
        int step_L2 = 0;
        int step_L3 = 0;

        if(iList_1[n] <= boarder){
            iList_2small[step_L2] = iList_1[n];
            step_L2 += 1;
        }
        else if(iList_1[n] > boarder){
            iList_3large[step_L3] = iList_1[n];
            step_L3 += 1;
        }
    }

    //Computing the length of the sorted arrays
    size_t len2 = sizeof(iList_2small)/sizeof(int);
    size_t len3 = sizeof(iList_3large)/sizeof(int);

    //Only for demonstration purposes
    printf("\nThe length of the second list is \t %llu \t elements", len2);
    printf("\nThe length of the second list is \t %llu \t elements\n", len3);
    printf("\n\n%i\n\n", iList_2small[10]);

    //Outputting the sorted arrays
    for(n = 0; n < 8 ; n++){
        printf("%i ", iList_2small[n]);
    }

    for(n = 0; n < len3; n++){
        printf("%i", iList_3large[n]);
    }
    return 0;
}

What am I doing wrong?

0

There are 0 best solutions below