Sort Function Using Pointers

Hi, I am working on a lab assignment where we had to write a sort function that has three int pointer parameters, and inside the sort function, put the largest number in n1, the next largest number in n2, and the smallest number in n3.


I have an error that I describe in a comment of my code but, the & in the function call gives me a compile error, and when I tried taking away the &'s, the numbers sort do not sort correctly. Whenever a number is swapped in the sort it gets replaced with the larger number that was being swapped, like so:

2 4 9
9 4 9

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
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;
void sort(int& num1, int& num2, int& num3);

int main()
{
	int num1, num2, num3;

	cin >> num1 >> num2 >> num3;
	sort(&num1, &num2, &num3); //error cannot change int* to int & ???
//it is also saying the initial value of reference to non-const must be an lvalue

	cout << num1 << "  " << num2 << "  " << num3 << " \n ";

}
void sort(int& num1, int& num2, int& num3) {
	//largest -> smallest
	int* first; 
	
	first = &num1;

	int* second; 
	
	second = &num2;

	int* third; 
	
	third = &num3;

	if (num2 > num1 && num2 > num3&& num1 > num3) {
		// num2, num1, num3
		*first = num2;
		*second = num1;

	}
	else if (num1 < num2 && num2 < num3 && num1 < num3) {
		//num3, num2, num1;
		*first = num3;
		*third = num1;

	}
	else if (num1 < num2 && num2 > num3&& num1 < num3) {
		//num2, num3, num1;
		*first = num2;
		*second = num3;
		*third = num1;
	}
	else if (num1 > num2 && num2 < num3 && num1 > num3) {
		//num1, num3, num2;
		*second = num3;
		*third = num2;
	}
	else if (num1 > num2 && num2 < num3 && num1 < num3) {
		//num3 ,num1, num2;
		*first = num3;
		*second = num1;
		*third = num2;

	}
	else {
		*first = num1;
		*second = num2;
		*third = num3;
	}

}



Any help with this is much appreciated!
the & in the function call gives me a compile error, and when I tried taking away the &'s, the numbers sort do not sort correctly.


void sort(int& num1, int& num2, int& num3);
Why are you trying to use pass by reference, or pass by value when the objective is to play with pointers?

Wouldn't passing by pointer be the better choice?
void sort(int* num1, int* num2, int* num3);



I am passing it this way because if I were to pass by pointer I'd be assigning a pointer a value that is another pointer in the function like this:
1
2
3
		*first = *num3;
		*third = *num1;


Which does not assign an actual value to the pointer.
Which does not assign an actual value to the pointer.

What?

Why do you want to assign the value of the pointer?

Why not just change the value the pointer is pointing to?


that is another pointer in the function like this:

Do you realize that *first is a value, not a pointer? The actual pointer would be first.

Try running this code and see what happens.
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>

void sort(int* num1, int* num2, int* num3);
void do_swap(int *num1, int *num2);

int main()
{
	int num1 = 433, num2 = 4, num3 = 100;

	//cin >> num1 >> num2 >> num3;
	sort(&num1, &num2, &num3);

	std::cout << num1 << "  " << num2 << "  " << num3 << " \n ";

}

void do_swap(int *num1, int *num2)
{
    if(*num1 > *num2)
    {
        int temp = *num1;
        *num1 = *num2;
        *num2 = temp;
    }

}

void sort(int* num1, int* num2, int* num3) {

    do_swap(num1, num2);
    do_swap(num2, num3);
}



Why not just change the value the pointer is pointing to?


^^This is what I meant by "assigning a value" I am extremely new with pointers so I was unsure of the wording. I did not realize that *first was not a pointer either. Thank you for explaining these issues to me. In my original code, I was supposed to use the "swap(int& num1, int& num2, int& num3)" function but was unsure of how it worked. The sort function in my original code was a past sort function that I modified to use pointers although I now understand how I was misusing them. I ran your code with "cin" values and it worked just fine as well.

Thank you for all your help it is greatly appreciated!
Topic archived. No new replies allowed.