Home > database >  checking elements in an array in s (through pointers) in C
checking elements in an array in s (through pointers) in C

Time:12-02

I am studying the pointers and this question became interesting. I want it to be like this: we have two arrays of integers. Determine the value and number of the largest element of the first array that is not part of the second but I don't know how to make the second part of the code that will check if the largest number is not included in the second array

#include <stdio.h>

int main()
{
    long array[100], * maximum, size, c, location = 1;

    printf("Enter the number of elements in array\n");
    scanf_s("%ld", &size);

    printf("Enter %ld integers\n", size);

    for (c = 0; c < size; c  )
        scanf_s("%ld", &array[c]);

    maximum = array;
    *maximum = *array;

    for (c = 1; c < size; c  )
    {
        if (*(array   c) > *maximum)
        {
            *maximum = *(array   c);
            location = c   1;
        }
    }

    printf("Maximum element is present at location number %ld and it's value is %ld.\n", location, *maximum);
    return 0;
}

CodePudding user response:

I think you are doing this in the wrong order. If you find the maximum first and that is in the second array, you need to find the maximum again. You should first check each number in the first array against the second. If it is in the second, change the value to LONG_MIN. Then the maximum will be the right answer. Basically something like this:

#include <limits.h>

int i = 0;
for (; i < n;   i) {
    int j = 0;
    for (; j < n;   j) {
        /* in both arrays? */
        if (arr2[i] == arr1[j]) {
            arr1[j] = LONG_MIN;
            break;
        }
    }
}

At this point any numbers in arr1 that are in arr2 will be set to LONG_MIN. Now just calculate max like you already did.

Edit: changed INT_MIN to LONG_MIN. I didn't notice you were doing long int arrays.

CodePudding user response:

For starters your incomplete code already is wring. It changes the source array

maximum = array;

//...

*maximum = *(array   c);

More precisely it changes the first element of the array. It shall not do that.

As for your question then the code will look more clear and readable if to implement the algorithm as separate functions.

Here is a demonstration program.

#include <stdio.h>

int find( const int a[], size_t n, int value )
{
    const int *p = a;

    while (p != a   n && *p != value)   p;

    return p != a   n;
}

int * max_exclusive_element( const int a1[], size_t n1, const int a2[], size_t n2 )
{
    const int  *max = a1;

    while (max < a1   n1 && find( a2, n2, *max ) )   max;

    if (max < a1   n1)
    {
        for ( const int *p = max;   p < a1   n1; )
        {
            if (!find( a2, n2, *p ) && *max < *p )
            {
                max = p;
            }
        }
    }

    return ( int * )( max == a1   n1 ? NULL : max );
}

int main( void )
{
    int a1[] = { 1, 3, 5, 6, 7, 8, 9 };
    const size_t N1 = sizeof( a1 ) / sizeof( *a1 );
    int a2[] = { 1, 3, 5, 7, 9 };
    const size_t N2 = sizeof( a2 ) / sizeof( *a2 );

    int *max = max_exclusive_element( a1, N1, a2, N2 );

    if (max != NULL)
    {
        printf( "Maximum element is present at location number %td and it's value is %d.\n", 
            max - a1, *max );
    }
}

The program output is

Maximum element is present at location number 5 and it's value is 8.

The function find determines whether a given value is present in an array. And the second function max_exclusive_element finds the maximum element according to the requirement.

As you can see all loops use pointers.

If you need to use arrays with elements of the type long int then it will be not hard to change the presented code.

  • Related