Given 10 input numbers and stored in an array, how do I check that the next value is different than the last one?

Viewed 513

It's the first time I'm working with arrays so I'm trying to do some exercises on my school book. The assignment goes by:

Store 10 numbers in an array, print each one of them without printing the same number twice.

Examples:

array values: 1, 2, 4, 6, 3, 7, 8, 44, 2,1

printable values: 1, 2, 4, 6, 3, 7, 8, 44

I managed to make a for loop to store each one of the 10 values in the array, but I cant figure out how to check the values stored between one another.

my code so far.

int main()
{
    int array[10], values[10], i;

    printf("insert 10 integers:");
    for(i=0; i<10; i++)
    {
        printf("\n");
        scanf("%d", &array[i]);
    }

    return 0;
}

I tried thinking of using a variable to store the values in, while the actual ones in the array changes, so that I can do a selection and check if the new value is equal/different than the variable. But to do so I would have to include the variable in the loop, which would make it all useless because the new variable will always be equal to the new input.

EDIT 1:

int main()
{
    int array[10], values[10], i, a=1;

    printf("insert 10 integers:");
    for(i=0; i<10; i++)
    {
        printf("\n");
        scanf("%d", &array[i]);
        a=i-1;
        if(array[i]!=array[a])
        {
            values[i]=array[i];
        }
    }
    for(i=0; i<10; i++)
    {
        printf("\n%d", values[i]);
    }
    return 0;
}

I wrote this new portion of code that is actually printing the values of the array without printing again the ones equal to one another. To do so I thought of using a new index, 'a' that will always be less by 1 than 'i', the first index. Except that i still prints 10 numbers and substitutes the removed values with random values

3 Answers

I like Govind Parmar's suggestion.

Another approach might be to keep a list of "duplicates* AS YOU PRINT THEM.

For example (pseudo code):

#define ARRAY_SIZE 10

void get_values(int[] array, int n_elements) {     
    printf("insert %d integers:", n_elements);
    for(int i=0; i<n_elements i++) {
        printf("\n");
        scanf("%d", &array[i]);
    }
}

bool check_printed (int[] array, int idx) {
    int value = array[idx];
    for (int i=0; i < idx-1; i++) {
        if (array[i] == value) {
            return TRUE;
        }
    }
    return FALSE;
}

int main() {
    int array[ARRAY_SIZE];    
    get_values(array, ARRAY_SIZE);    
    for(i=0; i<ARRAY_SIZE; i++) {
        if (!check_printed(array[i]) {
          printf("\n%d", array[i]);
        }
    }
    return 0;
}

As long as the number of element are small, you don't need to worry about performance. Therefore you can simply loop through all elements before the current element to see if the current value has already been printed.

In other words - if you currently wants to know whether or not to print the element at index 3 then look if the element in index 0, 1 or 2 have the same value.

This can be implemented several ways - here is one:

for(int i=0; i<10; i++)
{
    // Flag to tell if current element shall be printed.
    //       Set flag to 1, i.e. assume current element shall be printed
    int doPrint = 1;

    // Now check for duplicates in the elements before the i'th element
    for(int j=0; j<i; j++)
    {
        if (values[i] == values[j])
        {
            // Duplicate found. Don't print. Clear flag.
            doPrint = 0;
        }
    }

    // Print if doPrint-flag is still 1
    if (doPrint) printf("%d ", values[i]);
}
printf("\n");

This implementation have complexity O(n^2) and therefore it isn't well suited for huge arrays but as long as you only have 10 elements, it's fine and simple.

It is very easy.

For starters if you are required just to output an array when do not change it. To output an array and to change it are two different things.

Also it will be resource consuming to declare an auxiliary array of the same size to output the original array.

What you need are two nested loops and nothing more.

And do not use magic numbers like 10 in your programs. Use named constants.

Here is a demonstrative program.

#include <stdio.h>

int main(void) 
{
    int array[] = { 1, 2, 4, 6, 3, 7, 8, 44, 2, 1 };
    const size_t N = sizeof( array ) / sizeof( *array );

    for ( size_t i = 0; i < N; i++ )
    {
        size_t j = 0;
        while ( j != i && array[j] != array[i] ) ++j;

        if ( j == i ) printf( "%d ", array[i] );
    }

    putchar( '\n' );

    return 0;
}

Its output is

1 2 4 6 3 7 8 44 
Related