0
    #include <stdlib.h>
    
    int main(int argc, char** argv) {
        int value;
        int array[5] = {1, 1, 1, 2, 3};
        int size = 5;
        scanf("%d", &value);
        for (int i = 0; i < size; i++) {
            if (value == array[i]) {
                for (int j = i; j < size; j++) {
                    array[j] = array[j + 1];
                    size--;
                }
            }
        }
        for (int i = 0; i < size; i++) {
            printf("%d ", array[i]);
        }
    }

Example I have array: 1, 1, 1, 2, 3 and user inputs value = 1, my code must delete all number value = 1 in array. But when my code run this case, the output is: 1, 2 which should be is: 2, 3.

7
  • 3
    Have you stepped through in a debugger? Have you worked this out on paper first to be sure your system works? Normally this is done with two pointers, not just one. Commented Jul 13, 2020 at 2:02
  • 1
    Please don't change your question so completely it doesn't resemble the original. Commented Jul 13, 2020 at 2:41
  • 1
    no, it same the original,I rewrite the question to make it easier for you to understand Commented Jul 13, 2020 at 2:44
  • 1
    The double for loop is the bad algorithm. If you would do even the correctly-working double-for algorithm with playing cards, what it does is spread the cards in a line on the floor, move your finger over the cards and if it is pointing to a card of sought face value, you would remove that card, then move all the remaining cards one slot to the left one by one... then continue from the card immediately right of the card you were pointing to... but there is no need to move the tail repeatedly! Just move the card to the left if it does not have the sought value. Commented Jul 13, 2020 at 3:31
  • 1
    I get that, but do try and preserve the basic structure so the answers match up to something. Commented Jul 13, 2020 at 3:42

4 Answers 4

2

There's really not much to this code when you do it using a simple "copy if" algorithm:

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

void removeAllOccurrences(int value, int *array, int *size) {
  int* end = &array[*size];
  int* copy = array;

  while (array < end) {
    if (*array != value && copy != array) {
      *copy = *array;
      --*size;
      ++copy;
    }

    ++array;
  }
}

Note I've switched value to an argument as that makes it a lot easier to set up test cases and avoids having to figure out what that get_int() thing does:

int main(int argc, char **argv) {
  int array[] = { 1, 2, 1, 2, 3, 1, 4, 1 };
  int len = sizeof(array) / sizeof(int);

  removeAllOccurrences(1, array, &len);

  for (int i = 0; i < len; ++i) {
    printf("%d ", array[i]);
  }

  printf("\n");

  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

2
void Remove_val(int array[], int* sz, int val)
{
    int* add = array;
    int newsize = 0;

    for (int index = 0; index < *sz; index++)
    {
        if (array[index] != val)
        {
            *add++ = array[index];
            newsize++;
        }
    }

    *sz = newsize;
}

Comments

1

Inside the inner loop

    for (int i = 0; i < size; i++) {
        if (value == array[i]) {
            for (int j = i; j < size; j++) {
                array[j] = array[j + 1];
                size--;
            }
        }
    }

there is "removed" only one element equal to value. Its position is occupied by the next element that also can be equal to value. But in the outer loop the index i is being incremented. So the next element that is at the position of the previous value of i will not be removed.

Moreover there is an attempt to access memory beyond the array due to this statement

array[j] = array[j + 1];
                 ^^^^^

when j is equal to size- 1

Also it is inefficient to move all elements to the left when the removed value in the array is found.

You could write a separate function that does the task. The function could return the number of actual elements in the array after removing the specified value.

Here is a demonstrative program.

#include <stdio.h>

size_t remove_if_equal( int a[], size_t n, int value )
{
    size_t i = 0;
    
    for ( size_t j = 0; j < n; j++ )
    {
        if ( !( a[j] == value ) )
        {
            if ( i != j ) a[i] = a[j];
            i++;
        }
    }
    
    return i;
}

int main(void) 
{
    int a[] = { 1, 1, 1, 2, 3 };
    const size_t N = sizeof( a ) / sizeof( *a );
    
    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );

    size_t n = remove_if_equal( a, N, 1 );
    
    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );
    
    return 0;
}

The program output is

1 1 1 2 3 
2 3 

Comments

-1
    #include <stdlib.h>
    
    int main(int argc, char** argv) {
        int value;
        int array[5] = {1, 1, 1, 2, 3};
        int size = 5;
        scanf("%d", &value);
        for (int i = 0; i < size; i++) {
            if (value == array[i]) {
                for (int j = i; j < size-1; j++) {
                    array[j] = array[j + 1];                    
                }
                  size--;
                  i--;
            }
        }
        for (int i = 0; i < size; i++) {
            printf("%d ", array[i]);
        }
    }

I solved it. The output of this code is: 2, 3

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.