Bubble sort

im getting one error:

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)

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

void BubbleSort(int data[], int n)
{
	int SubArrayEnd = n -1;

	while (SubArrayEnd > 0)
	{
	int nextEnd = 0;
    
	for (int j = 0; j < (SubArrayEnd - 1); ++j)
	{
        if (data[j] > data[j+1])
	{
        data[j] = data[j+1]; 
        nextEnd = j;
	}
	else  
	++j;  
	}

	SubArrayEnd = nextEnd;
	}
}


int main()
{
	int n = 10;
	int data[10]; 
        cout << "Enter ten unsorted integers..." << endl;
	for(int cnt = 0; cnt < 10 ; ++cnt)
	{
	cout << "[" << cnt << "] = ";
	cin >> data[cnt];
	}

        cout << endl << "Unsorted List = ";
	for(int cnt = 0; cnt < 10 ; ++cnt)
	{
        cout << data[cnt] << ", ";
	}

	cout << "Sorting..." << endl << "Sorted List = ";
	cout << BubbleSort(data, n) << endl;

}


and whay cant i in the beginnning of main() type data[n] instead of data[10]


ty in advance!
Firstly; the error:

When you call BubbleSort(data, n) it does not return a value, so cout cannot print anything from it, so it returns an error.

You cannot use int data[n] because it is dynamic, & thus you need to declare it like this:
1
2
int n=10;
int* data=new int[n];

Well, in this case it would work, since he used a literal constant instead of a variable.
#include <iostream>
using namespace std;

void BubbleSort(int data[], int n)
{
int SubArrayEnd = n -1;

while (SubArrayEnd > 0)
{
int nextEnd = 0;

for (int j = 0; j < (SubArrayEnd - 1); ++j)
{
if (data[j] > data[j+1])
{
data[j] = data[j+1];
nextEnd = j;
}
else
++j;
}

SubArrayEnd = nextEnd;

}return data;
}
yea i know, but i need to write a function with type void, so is it even possible?

chris: i get 3 more errors with that line but nvm that

roodtree: void doesnt return
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.
i dont quite understand what u mean...
you are actually modifying the array you are passing
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.

Anyway try changing this:

cout << "Sorting..." << endl << "Sorted List = ";
cout << BubbleSort(data, n) << endl;

to this:

cout << "Sorting..." << endl;
BubbleSort(&data, n);
cout << "Sorted List = ";
for(i = 0; i < n; i++)
{
if(i != (n - 1))
cout << data[i] << ", ";
else
cout << data[i] << "." << endl;
}

AND change this:

void BubbleSort(int data[], int n)

to this:

void BubbleSort(int *data[], int n)

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.
Last edited on
ty vm Malachi, and also for that last tip

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

do i need to make const array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;

void BubbleSort(int *data[], int n)
{
	int SubArrayEnd = n -1;

	while (SubArrayEnd > 0)
	{
	int nextEnd = 0;
    
	for (int j = 0; j < (SubArrayEnd - 1); ++j)
	{
        if (data[j] > data[j+1])
	{
        data[j] = data[j+1]; 
        nextEnd = j;
	}
	else  
	++j;  
	}

	SubArrayEnd = nextEnd;
	}
}


int main()
{
	const int n = 10;
	int data2[n];    
        cout << "Enter ten unsorted integers..." << endl;
	for(int cnt = 0; cnt < 10 ; ++cnt)
	{
	cout << "[" << cnt << "] = ";
	cin >> data2[cnt];
	}

        cout << endl << "Unsorted List = ";
	for(int cnt = 0; cnt < 10 ; ++cnt)
	{
        cout << data2[cnt] << ", ";
	}

	cout << "Sorting..." << endl;
        BubbleSort(&data2, n);
        cout << "Sorted List = ";
        for(int i = 0; i < n; i++)
       {
       if(i != (n - 1))
       cout << data2[i] << ", ";
       else
       cout << data2[i] << "." << endl;
      }

}

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...
Last edited on
yea well that got rid of the error, but neither of our bubblesort functions doesnt work as it should :)

and btw gratz, u already posted 30* posts in only 3 days, i think ur gonna be a good "addition" to this community :D
Last edited on
@claimz
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.

http://gcc.gnu.org/onlinedocs/gcc-3.4.5/gcc/Variable-Length.html#Variable-Length

since it's gcc, linux c++ compilers probably support it. Though I'm not really sure o.O"
Last edited on
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. ^.^

Thanks, Claimz.
Last edited on
ok heres a bit modified func:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void BubbleSort(int data[], int n)
{
	int SubArrayEnd = n -1;  

	while (SubArrayEnd > 0)
	{
	int nextEnd = 0;
    
	for (int j = 0; j < (SubArrayEnd); j++)
	{
    if (data[j] > data[j+1])
	{
	int temp=data[j]; 
       data[j]=data[j+1];      
       data[j+1]=temp;   


       nextEnd = j;
	}
	else  
	++j;
	}

	SubArrayEnd = nextEnd;
	}
}


if i type 10,8,7...
i get 1,2,3..

but else its not succesful

dont know what else i can do...

(gonna be absent for a week...)
Last edited on
for a hint It would look like this in pseudocode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
}
Last edited on
Topic archived. No new replies allowed.