1

I am trying to write a program that sorts an array (input by user) using pointers.
This is the code I wrote:

#include <stdio.h>
void main()
{
    int a[100],i,n,j,t;
    printf("Enter number of elements:\n");
    scanf("%d",&n);
    printf("Enter array:\n");
    for (i=0; i<n; i++)
    {
        scanf("%d",a+i);
    }
    for (i=0; i<(n-1); i++)
    {
        for (j=i+1; i<n; i++)
        {
            if (*(a+i)>*(a+j))
            {
                t=*(a+i);
                *(a+i)=*(a+j);
                *(a+j)=t;
            }
        }
    }
    printf("Sorted array (ascending) is:\n");
    for (i=0; i<n; i++)
    {
        printf("%d ",*(a+i));
    }
    printf("\n");
}

The compiler is not showing any errors.

Input:
5
1 5 4 2 8
Output:
1 8 4 2 5

I'm learning by myself and I got the idea that pointers are similar to arrays. I know how to do the same task with arrays but when I try here to substitute them with pointers it doesn't work.

What am I doing wrong here?

11
  • 1
    You are increasing i in both for loops; you should increase j instead of i; Commented Aug 28, 2017 at 20:57
  • 1
    Should I delete the question, since I see the mistake is pretty stupid (basically a typo)? I can't believe I missed it. Commented Aug 28, 2017 at 21:09
  • 1
    @Plexus I answered in the comments exactly for allowing you to do that since it was evidently a very minor error, but that's no longer possible (for you, at least) because there are positive-scored answers. Instead, you might want to revert your own edit fixing part of the question, since both answers quote the original source code and that might be confusing. Commented Aug 28, 2017 at 21:30
  • 1
    @PatrickTrentin yeah I figured that out, but everything was happening so fast, 2 answers came in like a couple of minutes after it, there were comments as well and I was also trying the program, so I didn't quite manage to do everything in time Commented Aug 28, 2017 at 21:38
  • 1
    That's fine, and welcome on StackOverflow (: Commented Aug 28, 2017 at 21:40

2 Answers 2

2

the problem is in your second loop while you are sorting:

for (i=0; i<(n-1); i++)
    {
        for (j=i+1; i<n; i++)
        {
            if (*(a+i)>*(a+j))
            {
                t=*(a+i);
                *(a+i)=*(a+j);
                *(a+j)=t;
            }
        }
    }

you need to change the counter names to j :

for (i=0; i<(n-1); i++)
    {
        for (j=i+1; j<n; j++)
        {
            if (*(a+i)>*(a+j))
            {
                t=*(a+i);
                *(a+i)=*(a+j);
                *(a+j)=t;
            }
        }
    }

this line has the problem :

for (j=i+1; j<n; j++)
Sign up to request clarification or add additional context in comments.

3 Comments

it is always a possible danger for every programmer :D if you find my answer true, mark it as true answer
Thanks, it is true, but now I'm so embarrassed for asking it
be proud of yourself, someone who scare of asking question have to be embarrased,not you , good luck ;)
2

I think it is because your j variable is not increased properly and wrong comparison is used (as suggested by Patrick). I think it should be as follow:

for(j=i+1; j<n; j++)

Comments

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.