What parameters to set as main loop

Viewed 77

Okay, so I have a homework question, which goes as follows: From the array make pairs (b, c) where b is lowest even number, and c is biggest. Write numbers in output, remove them from the array and continue the loop until there are no more even numbers, or the array is empty.

EDIT: If there is only 1 even number, output it as the biggest and lowest number at the same time, so that the final array has no even numbers.

Here is what I did for now:

int a[100], b, c, n;
b = 9999999;
c = -9999999;

printf("Input array length: ");
scanf("%d", &n);
printf("Input elements of array: ");

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

    for (int i = 0; i < n; i++) {
        if (a[i] < b && a[i] % 2 == 0)
            b = a[i];
    }

    for (int i = 0; i < n; i++) {
        if (a[i] > c && a[i] % 2 == 0)
            c = a[i];
    }

    printf("\n%d %d\n", b, c);

return 0;

I set absurd values for b and c on start so that I 99% sure there will be numbers from the array that is lower/higher from those values (I tried to set them as NULL but that didn't). So my question is how to set the main loop that will circle until there are no even numbers or array is empty? Also is there another way I can initialize b and c.

Also, for removing even numbers, I was thinking about doing something like this:

for (int i = pos; i < n; i++) 
    a[i] = a[i+1];
n--;

Where pos is an index of the even element.

4 Answers

So, this problem is much much easier if you simply sort the array.

I would replace your current for loops with the following logic:

1) Sort the array from lowest to highest.

2) Increment through the array from both ends and pop out the highest and lowest values if and only if they are even.

3) Eventually your increments will meet in the middle and the loop can end when the two counters meet.

This is just a simple sort followed by a one for loop. Once they are popped you can set the values you popped to 0.

You can not resize an array but you can track its size of actual elements.

I suppose that if in the array there is only one even element then there is no pair of the minimum and maximum even elements in the array. So in this case nothing is "removed" from the array and the process stops. However you can change the approach and "remove" even a single even element.

And the request of the assignment to remove elements means that the order of the elements in the array must be kept. You may not sort the array. That is when you are asked to remove an element from an array then this does not mean that you are to sort the array.:)

Here is a demonstrative program.

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

struct Pair
{
    size_t min;
    size_t max;
};

struct Pair minmax_element( const int a[], size_t n )
{
    struct Pair p = { n, n };

    for ( size_t i = 0; i < n; i++ )
    {
        if ( a[i] % 2 == 0 )
        {
            if ( p.min == n || a[i] < a[p.min] ) p.min = i;
            if ( p.max == n || a[p.max] < a[i] ) p.max = i;
        }           
    }

    return p;
}

int main(void) 
{
    enum { N = 20 };
    int a[N];

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i < N; i++ ) a[i] = rand() % N;

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

    putchar( '\n' );

    int success = 1;
    size_t n = N;

    while ( success )
    {
        struct Pair p = minmax_element( a, n );

        success = p.min != n && p.max != n;

        if ( success )
        {
            printf( "minimum even number = %d, maximum even number = %d\n",
                    a[p.min], a[p.max] );

            if ( p.max < p.min )
            {
                size_t tmp = p.min;
                p.min = p.max;
                p.max = tmp;
            }

            memmove( a + p.max, a + p.max + 1, ( n - p.max - 1 ) * sizeof( int ) );
            --n;
            memmove( a + p.min, a + p.min + 1, ( n - p.min - 1 ) * sizeof( int ) );
            --n;
        }
    }

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

    putchar( '\n' );

    return 0;
}

Its output might look like

8 3 6 16 3 4 9 8 1 4 15 9 16 12 3 7 10 19 15 15 
minimum even number = 4, maximum even number = 16
minimum even number = 4, maximum even number = 16
minimum even number = 6, maximum even number = 12
minimum even number = 8, maximum even number = 10
3 3 9 8 1 15 9 3 7 19 15 15 

As it is seen in the result array there is one even element with the value 8 because the array does not have any more even element to make a pair.

Edit: Taking into account your comment

Oh sorry, I forgot to mention that if there is only 1 element (in this example 8), you simply output it 2 times at the and, as the lowest and biggest number, so that the final output has no even numbers.

the demonstrative program can look the following way

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

struct Pair
{
    size_t min;
    size_t max;
};

struct Pair minmax_element( const int a[], size_t n )
{
    struct Pair p = { n, n };

    for ( size_t i = 0; i < n; i++ )
    {
        if ( a[i] % 2 == 0 )
        {
            if ( p.min == n || a[i] < a[p.min] ) p.min = i;
            if ( p.max == n || a[p.max] < a[i] ) p.max = i;
        }           
    }

    return p;
}

int main(void) 
{
    enum { N = 20 };
    int a[N];

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i < N; i++ ) a[i] = rand() % N;

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

    putchar( '\n' );

    int success = 1;
    size_t n = N;

    while ( success )
    {
        struct Pair p = minmax_element( a, n );

        success = p.min != n;

        if ( success )
        {
            if ( p.max == n ) p.max = p.min;

            printf( "minimum even number = %d, maximum even number = %d\n",
                    a[p.min], a[p.max] );

            if ( p.max < p.min )
            {
                size_t tmp = p.min;
                p.min = p.max;
                p.max = tmp;
            }

            memmove( a + p.max, a + p.max + 1, ( n - p.max - 1 ) * sizeof( int ) );
            --n;

            if ( p.min != p.max )
            {
                memmove( a + p.min, a + p.min + 1, ( n - p.min - 1 ) * sizeof( int ) );
            --  n;
            }
        }
    }

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

    putchar( '\n' );

    return 0;
}

Its output might look like

3 10 0 12 16 0 13 11 15 16 6 8 11 10 11 12 4 14 4 3 
minimum even number = 0, maximum even number = 16
minimum even number = 0, maximum even number = 16
minimum even number = 4, maximum even number = 14
minimum even number = 4, maximum even number = 12
minimum even number = 6, maximum even number = 12
minimum even number = 8, maximum even number = 10
minimum even number = 10, maximum even number = 10
3 13 11 15 11 11 3 

You need to set up an encompassing while loop that continues running so long as there are even elements in the array. You can break from this loop if you find no even elements in your iteration. Also it seems at the moment you haven't implemented deletion from the array; this can be done in several ways, but in my opinion the easiest is just to maintain a second "checked" array that stores 1's or 0's to signify if an element has been previously taken. As follows:

    int seen[n];
    for (int i = 0; i < n; i++){
        seen[i] = 0;
    }

    while (1){
        int flag = 0, ind1 = 0, ind2 = 0;
        b = 0;
        c = 0;
        for (int i = 0; i < n; i++) {
            if ((!flag || a[i] < b) && a[i] % 2 == 0 && !seen[i]){
                flag = 1;
                b = a[i];
                ind1 = i;
            }
        }

        flag = 0;
        for (int i = 0; i < n; i++) {
            if ((!flag || a[i] > c) && a[i] % 2 == 0 && !seen[i] && i != ind1){
                flag = 1;
                c = a[i];
                ind2 = i;
            }
        }
        if (!flag){
            printf("No more even elements (or only one remaining) in the array.\n");
            break;
        }

        seen[ind1] = 1;
        seen[ind2] = 1; // "removing" them from the array

        printf("\n%d %d\n", b, c);
    }

Flag is set to 0 at the end of the second for loop if it's unable to find an even number that has yet to be taken, and breaks upon this condition.

This also solves the problem of initializing b and c. Your current approach to that is probably fine in most cases, but in this solution you simply set them to the first even value you find in the array (that hasn't been previously taken), since the minimum will be less than or equal to it, and the maximum greater than or equal to it.

As a side note, another (easier) way to "remove values" is to set them to odd numbers, since then you'll just pass over them in iteration. I decided not to go with this approach in interests of preserving the initial array, if it's what your teacher prefers.

The least I can think of is you can use a trick (not all teachers allow this, because it is kinda nonsense and considered as not doing the homework).

Make a variable odd[n] (not recommended), if you are worried about the memory used, you can simply do a for loop from 0 to n-1 to find how many odd numbers so you can make it like odd[n_odd].

Then you do a for loop from 0 to n-1, if the number is odd, assign it to odd[]. So you just need to print just the odd numbers. If it's necessary to find max & min even numbers and delete the pairs, you can ignore my comment (This is an idea instead of an answer). Otherwise, Vlad's answer is the correct one.

Related