0

I'm trying to sort 6 numbers input by the user using a bubble sort algorithm. My program has to be modular so I've written the program as a separate function but I keep getting two errors and I have no idea how to correct them.

Error 1. Lvalue required in function sort_Nums(int *)

Error 2. Expression syntax in function sort_Nums(int *)

Here's my code:

void sort_Nums(int*picked_nums)
{
    int i,step,temp;

    for(step=0; step<SIZE-1; ++step)
        for(i=0; i<SIZE-step-1; ++i)
        {
            if(*(picked_nums+i) > *(picked_nums+i)+1)
            {
                temp=*(picked_nums+i);
                *(picked_nums+i) = *(picked_nums+i)+1;
                *(picked_nums+i)+1 = temp;
            }
        }

        printf("The numbers you chose in order are:\n");
        for(i=0; i=<SIZE; ++i)
        {
            printf("%d\n", *(picked_nums+i));
        }   

        printf("Press any key to return to main menu");
        getchar();
    }

Thanks in advance and I am sorry if this is a stupid question and its just a syntax error or something but I've been coding for 9 hours almost now and this assignment is due tomorrow so this is a kind of last resort thing.

5
  • for(i=0;i=<10;++i) really? Commented Mar 10, 2018 at 17:23
  • You should start using brackets. By the way I mean This =< Commented Mar 10, 2018 at 17:26
  • This =>> *(picked_nums + (i + 1 ) is what you need, I think. Commented Mar 10, 2018 at 17:28
  • I normally do but i was basing the algorithm off notes from college and my algorithms lecturer codes strangely Commented Mar 10, 2018 at 17:30
  • Play with this DEMO Commented Mar 10, 2018 at 17:34

2 Answers 2

1

Hi your if condition if(*(picked_nums+i)>*(picked_nums+i)+1) is wrong it should be if(*(picked_nums+i)>*(picked_nums+i+1)). As per bubble short logic you need to compare with next element in the array. Like

if (arr[1] > arr[2] )

and you are doing

if (arr[1] > arr[1] + 1 ) which is wrong.

Suppose arr[1] = 4 and arr[2]=3

with the below if condition it should swipe

if (arr[1] > arr[1+1] ) because it is if (4 > 3) which is true

But your if condition is doing like below

if (arr[1] > arr[1] + 1) which is equal to if (4 > 3+1 ) which is false

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

1 Comment

Thanks! This fixed the errors and the code works other than an additional 1 in the output at the end of the sorted numbers Edit: fixed by changing the for(i=0;i<=SIZE;++i) to for(i=0;i<SIZE;++i)
0

Here you are adding 1 to the value, not to the pointer, and the second line causes the Lvalue error:

*(picked_nums+i)=*(picked_nums+i)+1;
*(picked_nums+i)+1=temp;

Corrected code would be like this:

*(picked_nums+i)=*(picked_nums+i+1);
*(picked_nums+i+1)=temp;

1 Comment

I'd recommend using array syntax Why would you recommend that if the OP says explicit that he need that Pointer notation? ==>> How do i sort an array in a function using pointer notation Have you even read the OPs title?

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.