Home > Software design >  Find the smallest number in an array that is not in another array
Find the smallest number in an array that is not in another array

Time:10-19

currently i am trying to solve this task: Two arrays of five integers each are given. Find the lowest number in the first array that is not in the second array.

It seems to me that if the user enters such integers in the first array: 0 1 2 3 4

And the integers of the second array: 0 2 3 4 5

The lowest integer, according to the condition of the task, will be 1, because it is not in the second array. So here is my code:

#include <stdio.h>
#include <locale.h>
int main() {
    setlocale(LC_ALL, "Rus");
    int arr1[5]; //initialize arrays
    int arr2[5];
    printf("Enter integers\n");
    for (int i = 0; i < 5; i  ) {
        int element;
        scanf_s("%d", &element); 
        arr1[i] = element;
    }
    printf("Enter integers\n");
    for (int i = 0; i < 5; i  ) {
        int element;
        scanf_s("%d", &element); 
        arr2[i] = element;
    }
    int min1 = arr1[0];
    int min2 = arr2[0];
    for (int i = 0; i < 5; i  ) // algorithm for finding the minimum number of an array 1
    {
        if (min1 > arr1[i])
        {

            min1 = arr1[i];
        }
        if (min2 > arr2[i])
        {
            min2 = arr2[i];
        }
    }
}

Well, the code is very clear, but here's how to make this check, if the first array input 0 1 2 3 4 and the second 0 2 3 4 5 then how to remove this zero, I do not understand, tried lots of things, my head hurts. Help please!!!

CodePudding user response:

There are some issues ...

  1. We don't care about the min value for arr2--only for arr1
  2. We must scan all arr2 values for a match to the current/candidate value of arr1
  3. There are some special cases we must handle

Normally, if we're just looking for the min value in arr1 (e.g. arr2 is not a factor), we can do [as you've done]:

int min1 = arr1[0];

And, we could start indexing into arr1 from 1 in the for loop.

But, this fails if:

  1. arr1[0] is the min value in arr1 and that value is in arr2
  2. arr1 and arr2 have identical values [even if they are in a different order].

So, we need an extra [boolean] value to denote whether the min1 value is valid. And, we must start indexing in the for loop from 0.

Here is the refactored code. It is annotated:

#include <stdio.h>
#include <locale.h>

#define A1MAX   5
#define A2MAX   5

int
main()
{
    setlocale(LC_ALL, "Rus");

    // define arrays
    int arr1[A1MAX];
    int arr2[A2MAX];

    printf("Enter integers\n");
    for (int i = 0; i < A1MAX; i  ) {
        if (scanf("%d", &arr1[i]) != 1) {
            printf("missing arr1[%d]\n",i);
            return 2;
        }
    }

    printf("Enter integers\n");
    for (int i = 0; i < A2MAX; i  ) {
        if (scanf("%d", &arr2[i]) != 1) {
            printf("missing arr2[%d]\n",i);
            return 3;
        }
    }

    int nomin = 1;
    int min1 = arr1[0];

    // check all values in arr1
    for (int i = 0; i < A1MAX; i  ) {
        // current value we're going to test
        int val = arr1[i];

        // check value if it's a _new_ minimum or we do _not_ yet have a minimum
        if ((val < min1) || nomin) {
            // scan all elements of arr2, looking for a match to the current
            // arr1 value
            int match = 0;
            for (int j = 0; j < A2MAX; j  ) {
                match = (val == arr2[j]);
                if (match)
                    break;
            }

            // if the current value is _not_ in arr2, we have a new minimum
            if (! match) {
                min1 = val;
                nomin = 0;
            }
        }
    }

    if (nomin)
        printf("there are no elements in arr1 that are not in arr2\n");
    else
        printf("the minimum element in arr1 not in arr2 is: %d\n",min1);

    return nomin;
}

CodePudding user response:

Things get complicated with code tries to maintain multiple indexes into multiple arrays... Things are simplified if you re-use code and break out functions (that can test user input, too)...

#include <stdio.h>

void fill( int arr[], size_t sz ) { // Get user input (with checking)
    printf("Enter integers\n");
    for( size_t i = 0; i < sz; i   )
        if( scanf( "%d", &arr[i] ) != 1 ) {
            fprintf( stderr, "scanf failure\n" );
            exit(1);
        }
}

// Search for a value in an array. Return index if found, or size if not found
size_t hunt( int val, int arr[], size_t sz ) {
    for( size_t i = 0; i < sz; i   )
        if( val == arr[i] )
            return i;
    return sz;
}

int main() {
#if 0 // Normal with user entry
    int arr1[5], arr2[5];
    size_t a1sz = sizeof arr1/sizeof arr1[0];
    size_t a2sz = sizeof arr2/sizeof arr2[0];

    fill( arr1, a1sz );
    fill( arr2, a2sz );
#else // less tedious with compile time init of data
    int arr1[] = { 0, 1, 2, 3, 4 };
    int arr2[] = { 0, 2, 3, 4, 5 };
    size_t a1sz = sizeof arr1/sizeof arr1[0];
    size_t a2sz = sizeof arr2/sizeof arr2[0];
#endif

    size_t gotOne = 0;
    for( size_t i = 0; i < a1sz; i   ) {
        // don't bother testing values if got a candidate and value is larger
        if( gotOne && arr1[i] >= arr1[ gotOne ] ) continue;

        // following is TRUE when not found...
        if( hunt( arr1[i], arr2, a2sz ) == a2sz )
            gotOne = i   1;
    }

    if( gotOne )
        printf( "Smallest in arr1 not in arr2 = %u\n", arr1[ gotOne - 1 ] );
    else
        puts( "No such value matching criteria" );

    return 0;
}
Smallest in arr1 not in arr2 = 1
  • Related