bubble_sort.cpp(46) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
Since you are passing an array (a pointer), you are actually modifying the array you are passing. Therefore, you call bubblesort() on the array, then print out the array normally.
Okay, what you need to do is instead of passing a copy of the array you need to pass a pointer to the array. That way you'll directly modify the array. Then you don't need your void function to return any data, the program will already have access to it.
If you declare int n as const will it work?
On my compiler it works fine as:
int n = 10;
int data[n];
IF its inside a function. Outside spews an error about declaring a dynamic variable...
Also you really shouldn't have two variables named data, that can cause scope issues.
i was just looking at how to use arrays with functions, but dont have that much time right now, and also, when i compile the modified code i get 1 error:
bubble sort.cpp(46) : error C2664: 'BubbleSort' : cannot convert parameter 1 from 'int [10]' to 'int *[]'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
oh, sorry... I was being really absent minded.
Arrays are already pointers as they are (sort of), so you don't need to reference or dereference them at all.
Get rid of the * and the & and it should compile fine.
Also, your bubblesort doesn't look to me like it'd actually work, that is assuming you're trying to push the highest numbers to the end... I'd write it like this:
void BubbleSort(int data[], int n)
{
for(int i = 0; i < (n - 1); i++)
{
for (int j = 0; j < (n - 1); ++j)
{
if (data[j] > data[j+1])
{
int tempvalue = data[j];
data[j] = data[j+1];
data[j+1] = tempvalue;
}
else
++j;
}
}
}
if you don't understand how mine works I can try to explain it for you...
and whay cant i in the beginnning of main() type data[n] instead of data[10]
Because arrays should have literal constants inside the braces when created. This however depends on your compiler.
@malachi
On my compiler it works fine as:
int n = 10;
int data[n];
that's because your IDE is probably using gcc 3.4.4 and up it's an extension to C you can read it here. It's not standard, but if I'm not mistaken I think I remember reading somewhere that it may be part of the standard in the future. Standard right now is C90, and the one with the extension is C99.
Then it'd be interesting to note that I'm using Dev-C++ 4.9.9.2 with MinGW/GCC 3.4.2 at current. I did not, however, realize that this is indeed an extension. ^.^
void BubbleSort(int data[], int arrayLen)
{
// predetermined number of passes
SET passes to 100;
FOR i = 0 to number of passes
FOR j = 0 to arrayLen
IF data[j] > data[j+1] THEN
int temp = data[j];
data[j] = data[j+1];
data[j+1]= temp;
ENDIF
ENDFOR
ENDFOR
}