Variable Field Swap declared void??

Can anyone tell me what I am doing wrong here, this was simply meant to show the bubble sorting method, however it fails to compile with a Variable Field Swap Declared Void Message.

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

void bubblesort(int *);
void swap(int *, int *);
int comp = 0;

int main(){
    int numbers[10] = {23,22,12,23,37,69,3,89,4,99};
    
    cout << "Original Data" << endl;
    for(int i = 0; i < 10; i++){
            cout << "\t" << numbers[i];
            }
bubblesort(numbers);
    cout << "Data items in ascending order" << endl;
    for(int i = 0; i < 10; i++){
            cout << "\t" << numbers[i];
            }
}

void bubblesort(int *numbers){
   for (int comp=1; comp < 10; comp++) {

       for (int i=0; i < (10-comp); i++) {
           if (numbers[i] > numbers[i+1]) {
           swap(&numbers[i], &numbers[i+1]);
           }
       }
   }
          }

void swap(*number1,*number2){
     int *temp = number1;
     number1 = number2;
     number2 = temp;
     return 1;
     }
1
2
3
4
5
6
void swap(int *number1,int *number2)
{
	int temp = *number1;
	*number1 = *number2;
	*number2 = temp;
}

you should use pointers to change the value instead of change the pointers.
@iggi

You declared the swap function return void, and, in the function, you return 1,
it does not work, delete the line it works
and use as what @yang said.


1
2
3
4
5
6
7
void swap(*number1,*number2){
     int *temp = number1;
     number1 = number2;
     number2 = temp;
     return 1;
     }
One other thing, swap() is a standard algorithm which is implemented as a template
function. Instead of writing the swap() function yourself, just delete it, and #include
<algorithm> and it should work correctly.
Return 1 was my bad, I originally had no return, however I had changed the function to int and tested it that way, but with no avail. I was specifically directed to code my own swap function using pass by reference by pointers.
Return 1 was my bad, I originally had no return, however I had changed the function to int and tested it that way, but with no avail. I was specifically directed to code my own swap function using pass by reference by pointers.
in your function,you just swap two pointers. At the begining number1 pointed to number[i] and number2 pointed to number[i+1], after the swap, number1 pointed to number[i+1] and number2 pointed to number[i], but the value of number[i] and number[i+1] are not changed.
Topic archived. No new replies allowed.