3

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

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 6
    If you sort the array first, this will be a lot easier to keep track of. – Govind Parmar Mar 04 '20 at 16:02
  • "I tried thinking of using a variable to store the values in", good. And I see you actually managed already to do that, the variable is called `array`. Now for the next step, write a loop (similar to the one you already have) which prints all the values, not worrying yet about checking for uniqueness. Then, according to the compromise described here https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions report about your next thoughts. – Yunnosch Mar 04 '20 at 16:03
  • 1
    Is this a homework assignment? A simple approach would be to have two nested loops, the outer loop going from `0` to `9`, and an inner loop (`for (int j = 0; j < i; j++)`) which check the array up to the index `i`. For example, if `i` is `2`, then the inner loop will only check indices `0` and `1`. If you break out of this loop before reaching `j == i`, don't print the number. For larger arrays, the proper approach would be to use a data structure which would quickly tell you if the number has already been entered (a hash table or a tree). – vgru Mar 04 '20 at 16:05
  • 1
    @GovindParmar True. But a) it seems the desired output is not sorted (admittedly depending on how you read "printable values") b) I estimate that sorting is some 2 chapters ahead in the course. – Yunnosch Mar 04 '20 at 16:06
  • 1
    How would you do this on paper? Hint: all your numbers are written down on piece of paper A. Take the first number from piece of paper A, check if it's already written down on piece of paper B, if not then write it down on piece of paper B. Then take the second number from piece of paper A, check if it's already written down on piece of paper B, if not then write it down on piece of paper B and so on. – Jabberwocky Mar 04 '20 at 16:21
  • I'm actually not sure if I understood the question. What should be the output for this input array: `1,3,4,3,1,5,7,7,8`? Should it be `1,3,4,5,7,8`? – Jabberwocky Mar 04 '20 at 16:25
  • 1
    @Jabberwocky yes, it should be 1,3,4,5,7,8. Printing the values without repeating them twice or more if the user entered the same number multiple times, essentially. – StefanGarofalo Mar 04 '20 at 16:28

3 Answers3

5

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;
}
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
3

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.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
2

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 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335