Derive various elements from two arrays

I am trying to infer various elements from two arrays. Therefore, if I have an array A:, {9, 0, 1}and B - {0, 8, 1}, I need to output an element that is included in the first set, but not included in the second: 9I can’t think how I should compare all the elements from the first array with the second.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a[10],b[10],c,n,i,j;

    printf("enter a number: ");
    scanf("%d",&n);
    for(i=0;i<n;i++){
    printf("Enter a[%d]: ",i+1);
    scanf("%d",&a[i]);
    }
    printf("\n");
    for(j=0;j<n;j++){
    printf("Enter b[%d]: ",j+1);
    scanf("%d",&b[j]);
    }

    for (i = 0; i < n; i++) {
            printf("%d ", a[i]); }

            printf("\n");

    for (i = 0; i < n; i++) {
            printf("%d ", b[i]); }
            printf("\n");

return 0;
}

I would like to show my thoughts, but I think this is stupid:

 for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            if(a[i]!= b[j]){
                c=a[i];
            }
        }
    printf("%d ",c);
    }
+4
source share
4 answers

If you want a more efficient solution suggested by @Sumeet Singh, you can sort the second array with qsort, then find similar elements from the first array with bsearch(binary search).

- O (N ^ 2) , n, .

, , :

#include <stdio.h>
#include <stdlib.h>

#define NNUMBERS 10

void get_array_input(int array1[], int array2[], size_t *n);
void search_elements(int array1[], int array2[], size_t n);
void print_arrays(int array[], size_t n);
int cmp_func(const void *a, const void *b);

int main(void) {
    int array1[NNUMBERS], array2[NNUMBERS];
    size_t n;

    /* input from user */
    get_array_input(array1, array2, &n);

    printf("\nFirst array: ");
    print_arrays(array1, n);

    printf("\nSecond array: ");
    print_arrays(array2, n);

    /* sorting the second array */
    qsort(array2, n, sizeof(*array2), cmp_func);

    printf("\nSorted Second array: ");
    print_arrays(array2, n);

    /* the search begins */
    search_elements(array1, array2, n);

    return 0;
}

void get_array_input(int array1[], int array2[], size_t *n) {
    size_t i;

    printf("Enter n: ");
    if (scanf("%zu", n) != 1) {
        printf("Invalid n value.\n");
        exit(EXIT_FAILURE);
    }

    for (i = 0; i < *n; i++) {
        printf("Enter array1[%zu]: ", i);
        if (scanf("%d", &array1[i]) != 1) {
            printf("Invalud array value.\n");
            exit(EXIT_FAILURE);
        }
    }

    for (i = 0; i < *n; i++) {
        printf("Enter array2[%zu]: ", i);
        if (scanf("%d", &array2[i]) != 1) {
            printf("Invalud array value.\n");
            exit(EXIT_FAILURE);
        }
    }
}

void search_elements(int array1[], int array2[], size_t n) {
    size_t i;
    void *key;

    printf("\nElements in first array which are not in second array: ");
    for (i = 0; i < n; i++) {
        key = bsearch(&array1[i], array2, n, sizeof(*array2), cmp_func);
        if (!key) {
            printf("%d ", array1[i]); /* not found, so print it */
        }
    }
    printf("\n");
}

void print_arrays(int array[], size_t n) {
    size_t i;

    for (i = 0; i < n; i++) {
        printf("%d ", array[i]);
    }
    printf("\n");
}

/* cmp function needed for qsort and bsearch */
/* many ways to write these */
int cmp_func(const void *a, const void *b) {
    const int *num1 = (const int *)a;
    const int *num2 = (const int *)b;

    if (*num1 > *num2) {
        return +1;
    } else if (*num1 < *num2) {
        return -1;
    }
    return 0;
}

Input:

Enter n: 3
Enter array1[0]: 9
Enter array1[1]: 0
Enter array1[2]: 1
Enter array2[0]: 0
Enter array2[1]: 8
Enter array2[2]: 1

:

First array: 9 0 1

Second array: 0 8 1

Sorted Second array: 0 1 8

Elements in first array which are not in second array: 9
+2

, . .

1. .

2. , , , .

O (m log n), m - , n - .

+4

. .

a[i], b[j], . - (, unique=1). , , , , a[i] "" a. , , , , a[i] b, . a[i] == b[j], , unique=0.

, a[i] b, . , b .

, , a.

+1

:

#include <stdio.h>

int main(void){
        int a[10],b[10],c,n,i,j;
        int counter=0;
        printf("enter a number: ");
        scanf("%d",&n);
        for(i=0;i<n;i++){
                printf("Enter a[%d]: \n",i+1);
                scanf("%d",&a[i]);
        }
        printf("\n");
        for(j=0;j<n;j++){
                printf("Enter b[%d]: \n",j+1);
                scanf("%d",&b[j]);
        }
        for(i=0;i<n;i++){
                counter=0;
                for(j=0;j<n;j++){
                        if(a[i]!=b[j]){
                                counter++;
                        }
                }
                if(counter == n){
                        printf("%d ",a[i]);
                }
        }
        return 0;
}

Explain this code a bit: In the last nested for loop, the outer loop takes one element from array a. The inner loop receives each element of array b to compare it with the received element from array a. If none of the elements of array b is equal to the received element, the counter will be equal to n (array size). Then we can print this element taken from a (this means that between the selected element and array b there are no matches of all elements.

0
source

Source: https://habr.com/ru/post/1666388/


All Articles