Reputation: 15
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.
Upvotes: 0
Views: 119
Reputation: 1660
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;
Upvotes: 0
Reputation: 5591
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
Upvotes: 1