Hi
I have been stuck trying to work this but something is wrong.
If any one can give me a hand i'd really appreciate it.
Printable View
Hi
I have been stuck trying to work this but something is wrong.
If any one can give me a hand i'd really appreciate it.
>for (i = 0, sorted = 1; i < n-1 ; i++)
This can be write like this too, no?
for (i=0, sorted=1; i < n ; ++i );
hmmm
This works fine.. he prints well all the elements of the array,Code:#include <stdio.h>
#define MAX 10 //array max size
int main(void)
{
int nArray[MAX] = {1,2,3,4,5,6,7,8,9,10};
int nI;
for(nI = 0; nI < MAX; ++nI)
printf("%d\n",nArray[nI]);
getchar(); //wait for the user to press a char
return 0;
}
same with nI++.
About your comparation, the two programs printed until 9.
for (i = 0, sorted = 1; i < n-1 ; i++)
I dont think that is needed here n-1, using the < operator (not the <=) this will alone go until n-1, I tried looping an array of 10 elements with your code, and he counted until 8 (0 - 8). but he should go until 9 (0-9) so I did change the n-1 to i<n and this works fine..
I think what is needed is too write a correct bubblesort. Bubblesort is a nested loop. You are counting form size of array down, and then swapping from 0 to the count. It is not a for in a while but a for in a for.
Now you explained why, thanks.
Because, in my book they don't write it like that ;)
I think you got this part of the code fixed but when getting input from the user that will go into an array, you do not use the address of operator (&). Since an array is a form of a pointer it is not needed.