Trouble running array through function

I'm pulling in data from a file and putting it into an array. I have that working fine, but from there I'm having issues compiling when I try to run it through a bubble_sort function. Can anyone help find what I'm doing wrong? Thank you!

The specific errors I get are:

demo.cpp: In function ‘int main()’:
demo.cpp:40:9: error: expected primary-expression before ‘]’ token
demo.cpp:40:32: error: expected primary-expression before ‘]’ token

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
  #include <iostream>
using namespace std;

int bubble_sort(int scores[]){
	int flag = 1;
	int temp;
	int size = 22;

	while(flag != 0){
		flag = 0;

		for(int x = 0; x < size; x += 1){
			if(scores[x] > scores[x + 1]){
				temp = scores[x];
				scores[x] = scores[x + 1];
				scores[x + 1] = temp;
				flag = 1;
			}
		} // end for loop

	} // end while loop

} // end bubble function

int main(){

	// Setup variables and array
	int load_data;
	int scores[22];
	int x = 0;

	// Read data into array
	while(!cin.eof()){
		cin >> load_data;
		scores[x] = load_data;
		++x;
	}


	scores[] = bubble_sort(scores[]);



	// Demo output
	for(int z = 0; z < 22; z++){
		cout << scores[z] << endl;
	}
}
 
scores[] = bubble_sort(scores[]);


I think arrays don't work like that ( At least not to my knowledge I've never attempted ).

Your int bubble_sort also doesn't return a value.

EDIT:

After further observation I think I see what you are trying to do, maybe try passing that array via a pointer/reference along with a size and loop through it to sort it? That way the scoures[] should auto update but heres hoping. :)
Last edited on
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
#include <iostream>
using namespace std;

void bubble_sort(int scores[])  //$$
{
	int flag = 1;
	int temp;
	int size = 5;

	while(flag != 0){
		flag = 0;

		for(int x = 0; x < size; x += 1){
			if(scores[x] > scores[x + 1]){
				temp = scores[x];
				scores[x] = scores[x + 1];
				scores[x + 1] = temp;
				flag = 1;
			}
		} // end for loop

	} // end while loop

} // end bubble function

int main(){	
	int scores[5] = {3,2,5,1,4};	
	bubble_sort(scores);  //$$
	// Demo output
	for(int z = 0; z < 5; z++){
		cout << scores[z] << endl;
	}
}
Thanks anup30 for the help! I tried running that and didn't get any output but I'm working on it.

Edit: Never mind, I named it "test" and running that through SSH was a problem. Thanks for the help!
Last edited on
Topic archived. No new replies allowed.