Click here to Skip to main content
15,912,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I give an input of 3 elements {1,1,2}. I'm getting output as {1,2}. It is deleting the element only once. Whereas, I want to delete all the occurrences. How do I proceed.

What I have tried:

<pre>#include<stdio.h>
#define max 100
int main()
{
        int pos,a[max],n,i,num,count=0;
        printf("Enter the number of elements : ");
        scanf("%d",&n);
        printf("Enter elements of the array : ");
        for(i=0;i<n;i++)
                scanf("%d",&a[i]);
        printf("Enter the number you want to delete : ");
        scanf("%d",&num);
        for(i=0;i<n;i++)
                if(a[i] == num){
                        pos = i;
                        count++;
                for(i=pos;i<n;i++)
                        a[i] = a[i+1];
                }
//Both works. But both deletes only once. 
/*      for(i=0;i<n;i++){
                if(a[i] == num){
                        a[i] = a[i+1];
                        count ++;
                }
        }
*/
        printf("The new array is : ");
        for(i=0;i<n-count;i++)
                printf("\n%d",a[i]);
        printf("\n");
}
~          
Posted
Updated 16-Nov-18 21:39pm

Start by using two array indexes, both initially set at 0: inp and outp.
inp is where you get a value from, and goes up by one each time round the loop.
outp is where you copy values to, and does not go up each time round the loop.

The loop through the whole array, adding one to inp each time:
C++
for (inp = 0; inp < n; inp++)

Inside the loop, compare the current element with the valeu you want to remove.
If it's the same, do nothing at all!
Otherwise, copy it to the output, and increment outp
C++
data[outp++] = data[inp];
After the loop, the number of elements left is in outp
This works because you are removing items: so you are copying the items you want to keep into place in a "shorter version" of the same array!

Heck, I'll even give you the code:
C++
for (inp = 0; inp < n; inp++)
    {
    if (data[inp] != removeThisValue)
        {
        data[outp++] = data[inp];
        }
    }
for (int i = 0; i < outp; i++)
    {
    printf("%d\n", data[i]);
    }
Have a think about it, and see if you can work out exactly what it does.
 
Share this answer
 
v2
Quote:
It is deleting the element only once. Whereas, I want to delete all the occurrences.

The algorithm is wrong, the problem is that when you detect a match, and move remaining values, you do not check the value that replaced the match.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900