Schreiben Sie ein C-Programm, um Vereinigung und Schnittmenge von zwei Arrays zu finden

Schreiben Sie ein C-Programm, um Vereinigung und Schnittmenge von zwei Arrays zu finden

Schreiben Sie ein C-Programm, um Vereinigung und Schnittmenge von zwei Arrays zu finden. Hier ist ein einfaches Programm zum Finden von Union und Intersection von zwei Arrays in der Programmiersprache C.

Was ist ein Array?

Arrays sind eine Art Datenstruktur, die eine sequenzielle Sammlung fester Größe von Elementen desselben Typs speichern kann. Ein Array wird verwendet, um eine Sammlung von Daten zu speichern, aber es ist oft sinnvoller, sich ein Array als eine Sammlung von Variablen desselben Typs vorzustellen.

Anstatt einzelne Variablen wie Zahl0, Zahl1, … und Zahl99 zu deklarieren, deklarieren Sie eine Array-Variable wie Zahlen und verwenden Zahlen[0], Zahlen[1] und …, Zahlen[99], um einzelne Variablen darzustellen. Auf ein bestimmtes Element in einem Array wird über einen Index zugegriffen.

Alle Arrays bestehen aus zusammenhängenden Speicherstellen. Die niedrigste Adresse entspricht dem ersten Element und die höchste Adresse dem letzten Element.

Hier ist der Quellcode des C-Programms zum Finden von Vereinigung und Schnittmenge von 2 Arrays. Das C-Programm wird erfolgreich kompiliert und (auf Codeblocks) auf einem Windows-System ausgeführt. Die Programmausgabe wird auch unten gezeigt.

QUELLCODE ::

/* C Program to Find Union and Intersection of 2 Arrays */

#include <stdio.h>
#define SIZE 5

void get_value(int arr[]);
void print_value(int arr[], int n);
void function_sort(int arr[]);
int find_intersection(int array1[], int array2[], int intersection_array[]);
int find_union(int array1[], int array2[], int union_array[]);

void main()
{
    int array1[SIZE], array2[SIZE], intersection_array[SIZE], union_array[SIZE*2];
    int num_elements;

    //input elements of Array1
    printf("\n Enter the elements of Array 1: \n");
    get_value(array1);
    printf("\n\n Elements of Array 1: ");
    print_value(array1, SIZE);

    //Sort array 1
    function_sort(array1);
    printf("\n\nSorted elements of Array 1: ");
    print_value(array1, SIZE);

    //input elements of Array2
    printf("\n\nEnter the elements of Array 2: \n");
    get_value(array2);
    printf("\n\n Elements of Array 2: ");
    print_value(array2, SIZE);

    //Sort array 2
    function_sort(array2);
    printf("\n\nSorted elements of Array 2: ");
    print_value(array2, SIZE);

    //Find Intersection
    num_elements = find_intersection(array1, array2, intersection_array);
    printf("\n\n Intersection is: ");
    print_value(intersection_array, num_elements);

    //Find Union
    num_elements = find_union(array1, array2, union_array);
    printf("\n\n Union is: ");
    print_value(union_array, num_elements);
}

void get_value(int arr[])
{
    int i, j;
    for (i = 0; i < SIZE; i++)
    {
        j = i + 1;
        printf("\n Enter element %d: ", j);
        scanf("%d", &arr[i]);
    }
}

void print_value(int arr[], int n)
{
    int i;
    printf("{ ");
    for (i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("}");
}

void function_sort(int arr[])
{
    int i, j, temp, swapping;

    for (i = 1; i < SIZE; i++)
    {
        swapping = 0;
        for (j = 0; j < SIZE-i; j++)
        {
            if (arr[j] > arr[j+1])
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapping = 1;
            }
        }
        if (swapping == 0)
        {
            break;
        }
    }
}

int find_intersection(int array1[], int array2[], int intersection_array[])
{
    int i = 0, j = 0, k = 0;
    while ((i < SIZE) && (j < SIZE))
    {
        if (array1[i] < array2[j])
        {
            i++;
        }
        else if (array1[i] > array2[j])
        {
            j++;
        }
        else
        {
            intersection_array[k] = array1[i];
            i++;
            j++;
            k++;
        }
    }
    return(k);
}

int find_union(int array1[], int array2[], int union_array[])
{
    int i = 0, j = 0, k = 0;
    while ((i < SIZE) && (j < SIZE))
    {
        if (array1[i] < array2[j])
        {
            union_array[k] = array1[i];
            i++;
            k++;
        }
        else if (array1[i] > array2[j])
        {
            union_array[k] = array2[j];
            j++;
            k++;
        }
        else
        {
            union_array[k] = array1[i];
            i++;
            j++;
            k++;
        }
    }
    if (i == SIZE)
    {
        while (j < SIZE)
        {
            union_array[k] = array2[j];
            j++;
            k++;
        }
    }
    else
    {
        while (i < SIZE)
        {
            union_array[k] = array1[i];
            i++;
            k++;
        }
    }
    return(k);
}

AUSGABE ::

/* C Program to Find Union and Intersection of 2 Arrays */

Enter the elements of Array 1:

 Enter element 1: 1

 Enter element 2: 8

 Enter element 3: 7

 Enter element 4: 3

 Enter element 5: 5


 Elements of Array 1: { 1 8 7 3 5 }

Sorted elements of Array 1: { 1 3 5 7 8 }

Enter the elements of Array 2:

 Enter element 1: 4

 Enter element 2: 7

 Enter element 3: 6

 Enter element 4: 1

 Enter element 5: 2


 Elements of Array 2: { 4 7 6 1 2 }

Sorted elements of Array 2: { 1 2 4 6 7 }

 Intersection is: { 1 7 }

 Union is: { 1 2 3 4 5 6 7 8 }

Wenn Sie Fehler oder Fragen im Zusammenhang mit dem oben genannten Programm oder Fragen oder Bewertungen gefunden haben, die Sie uns stellen möchten, können Sie uns kontaktieren über unsere Kontaktseite oder Sie können auch unten im Kommentarbereich einen Kommentar abgeben. Wir werden unser Bestes tun, um Sie in kurzen Abständen zu erreichen.