2

I am trying to remove the negative numbers from array with the following code. Unfortunately, not getting the results. It just prints the first element over and over. Can someone please let me know where am I going wrong?

#include <stdio.h>
void removenegative(int a[],int *p, int *q);
int main()
{
    int a[] = {2, 3, -5, -7, 6, 9};
    int i;
    int *p, *q;
    p = a;
    q = a+6-1;
    removenegative(a, p,q);
    for(i=0;i<6;i++)
        {
            printf("%2d", *p);
        }
    printf("\n");
}
void removenegative(int a[],int *p, int *q)
{
    int *x;
    x= &a[0];
    while (p<=q)
        {
            if (*p>=0)
                {
                    *x = *p;
                    x++;
                }
            p++;
        }
    for( ; x<=q; x++)
        {
            *x = -1;
        }
}

5 Answers 5

4
for(i=0;i<6;i++)
    {
        printf("%2d", *p);
    }

You're not changing p!

Sign up to request clarification or add additional context in comments.

3 Comments

got beaten by 5 secs. ;-)
@0xF1 Yours was a bit better so have a +1 :)
same 2 you! and merry christmas!! ;-)
4

You are printing only one value:

printf("%2d", *p);

do this before for loop:

p = a;

and add p++ inside loop;

Comments

4

It just prints the first element over and over

Of course it does so...

    for(i=0;i<6;i++)
    {
        printf("%2d", *p);
    }

You always print *p, which is a[0]

Comments

2

Change:

printf("%2d", *p);

to:

printf("%2d", a[i]);

You're looping i, but not using it.

Comments

1

Your code is correct:

void removenegative(int a[],int *p, int *q)
{
    int *x;
    x= &a[0]; // let x point to the first thing in a

    while (p<=q) // continue while p points to an address before q
        {
            if (*p>=0)  // if the thing pointed to by p is greater than zero...
                {
                    *x = *p;  // copy from p to x, increment x
                    x++;
                }
            p++;  // increment p
        }
    for( ; x<=q; x++) // while x is less than q...
        {
            *x = -1;  // fill in -1
        }
}

So x is an incrementing write pointer, while p scans the array and q acts as an end-of-array marker.

As I've been beaten to saying while typing this, your output routine is incorrect.

6 Comments

On the contrary, when his question is "I am trying to remove the negative numbers from array with the following code. Unfortunately, not getting the results.", showing that he actually is getting the results, because the code he thinks is wrong does exactly what he thinks, is an answer to the question.
Seems like a strange way to point out that the bug is in code X is by providing an annotated listing of code Y.
The poster posted one segment of code, believing the act of removing negative numbers to be broken. It isn't. I explained that it isn't. To me it seems strange that every other "answer" here should blankly ignore more than half the code posted, not commenting on it at all. Either the code the question includes was irrelevant and shouldn't have been included in the first place — in which case talk to the poster about it — or it was relevant and a comprehensive answer should comment on it.
You've been here more than twice as long as I have. Surely you've noticed that much of the time posters are totally confused about where the problem is in their code. So their questions are full of irrelevant code, because they don't know what needs to be posted. It's generally better to post more code than less, because if he'd just posted the code he thought had the problem he wouldn't have posted the printing code, and we'd still be wondering what the problem is. He posted the whole thing, we showed him where the bug is, that is the answer.
I don't see how you can possibly claim that your post of an annoted listing of everything other than the buggy part is an answer to his question. What it is is a detailed comment that's too long and complex to fit in the comment fields, so you put it in the answer section.
|

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.