Home > Back-end >  How to print an arrays, that have the same elements which are not in order
How to print an arrays, that have the same elements which are not in order

Time:12-14

I have int 2-5 arrays (user decision how much), each of which has its own elements. I need to compare them to print their names.

For example, I have 3 arrays, First contains: 3, 41, 315, 2. Second contains: 5, 31, 315. The Third one contains: 315, 41, 3, 2, 2, 41. How can I compare their elements, to print names of the arrays that have the same elements, like this: ("FIRST THIRD"). (They can be in or out of order, and they can repeat);**

I tried to play with indexes of arrays, if array1[0] == array2[0], then compare other elements, but it will take a while to write this all, and it will take a lot of space, maybe there is a cleaner fix for this?

    #define N 6
    int arr1[N] = { 3, 41, 315, 2 }, arr2[N] = { 5, 31, 315 }, arr3[N] = {315, 41, 3, 2, 2, 41};
    
    for (int h = 0; h <= N; h  )
if (arr1[j] == arr2[h])
        {
            // compare other elements of arrays one by one adding indexes of arrays 
        }
        }

I want it to output :

First Third

CodePudding user response:

Performance isn't great, but you can define a function contained_in that checks to see if everything in one array is in another.

int contained_in(int *arr1, size_t n1, int *arr2, size_t n2) {
    for (size_t i = 0; i < n1; i  ) {
        int contained = 0;
        for (size_t j = 0; j < n2 && !contained; j  ) {
            if (arr1[i] == arr2[j]) {
                contained = 1;
            }
        }

        if (!contained) return 0;
    }

    return 1;
}

If you check contained_in(arrascii1, N, arrascii2, N) and contained_in(arrascii2, N, arrascii1, N) then you'll know that both contain the same values.

CodePudding user response:

Instead of defining the arrays arr1, arr2 and arr3 as separate arrays, it would probably be easier to define an array of arrays, for example like this:

#define NUM_ARRAYS 3
#define N 6

int arrays[NUM_ARRAYS][N] = {
    {  3,  41, 315, 2 },
    {  5,  31, 315 },
    { 315, 41,   3, 2, 2, 41}
};

That way you can use the indexes of both dimensions in loops, which will make the coding easier:

#include <stdio.h>
#include <stdbool.h>

#define NUM_ARRAYS 3
#define N 6

//This function returns true if the right array contains all
//elements of the left array, and vice versa. Otherwise it returns false.
bool compare_arrays( int left[], int right[] )
{
    //check whether right array contains all elements in the
    //left array
    for ( int i = 0; i < N; i   )
    {
        for ( int j = 0; ; j   )
        {
            if ( j == N )
                return false;

            if ( left[i] == right[j] )
                break;
        }
    }

    //check whether left array contains all elements in the
    //right array
    for ( int i = 0; i < N; i   )
    {
        for ( int j = 0; ; j   )
        {
            if ( j == N )
                return false;

            if ( right[i] == left[j] )
                break;
        }
    }

    return true;
}

int main( void )
{
    const char *map[] = {
        "First", "Second", "Third", "Fourth", "Fifth",
        "Sixth", "Sevent", "Eighth", "Ninth", "Tenth"
    };

    int arrays[NUM_ARRAYS][N] = {
        {   3, 41, 315, 2 },
        {   5, 31, 315 },
        { 315, 41,   3, 2, 2, 41 }
    };

    for ( int i = 0; i < NUM_ARRAYS; i   )
    {
        for ( int j = i   1; j < NUM_ARRAYS; j   )
        {
            if ( compare_arrays( arrays[i], arrays[j] ) )
                printf( "%s %s\n", map[i], map[j] );
        }
    }
}

However, this program has no output. That is because the array arr1 also contains the value 0 in its last two elements, which do not exist in arr3. Therefore, the comparison of arr1 and arr3 fails.

The reason why the value of the last two elements is 0 is because if at least one element of the array is initialized, then all elements that are not explicitly initialized are implicitly initialized to 0.

For the reasons stated above, it would be appropriate to have an additional variable for every array, which specifies the number of valid elements in the array. Instead of using separate variables, it would probably be better to group them with the array in a struct:

struct array_with_length
{
    int length;
    int array[N];
};

That way, you can limit the loops to the number of valid elements:

#include <stdio.h>
#include <stdbool.h>

#define NUM_ARRAYS 3
#define N 6

struct array_with_length
{
    int length;
    int array[N];
};

//This function returns true if the right array contains all
//elements of the left array, and vice versa. Otherwise it returns false.
bool compare_arrays(
    const struct array_with_length *left,
    const struct array_with_length *right
)
{
    //check whether right array contains all elements in the
    //left array
    for ( int i = 0; i < left->length; i   )
    {
        for ( int j = 0; ; j   )
        {
            if ( j == right->length )
                return false;

            if ( left->array[i] == right->array[j] )
                break;
        }
    }

    //check whether left array contains all elements in the
    //right array
    for ( int i = 0; i < right->length; i   )
    {
        for ( int j = 0; ; j   )
        {
            if ( j == left->length )
                return false;

            if ( right->array[i] == left->array[j] )
                break;
        }
    }

    return true;
}

int main( void )
{
    const char *map[] = {
        "First", "Second", "Third", "Fourth", "Fifth",
        "Sixth", "Sevent", "Eighth", "Ninth", "Tenth"
    };

    struct array_with_length arrays[NUM_ARRAYS] = {
        {
            4, //number of elements in array
            {   3, 41, 315, 2 }, //array content
        },

        {
            3, //number of elements in array
            {   5, 31, 315 }, //array content
        },

        {
            6, //number of elements in array
            { 315, 41, 3, 2, 2, 41 } //array content
        }
    };

    for ( int i = 0; i < NUM_ARRAYS; i   )
    {
        for ( int j = i   1; j < NUM_ARRAYS; j   )
        {
            if ( compare_arrays( &arrays[i], &arrays[j] ) )
                printf( "%s %s\n", map[i], map[j] );
        }
    }
}

This program has the following output:

First Third

This is the output that you said that you wanted.

  • Related