So, round two. Or eight. I don't know, I don't care. I hate this problem and I hate this pointer shit and I don't want to do it anymore.
I added some logic to my function, so that it would determine which value was the median. Obviously, it would only work for three values, but actual median algorithms are a bit too powerful for this.
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>
#include <cmath>
using namespace std;
int *fun(int *b, int *a, int *c)
{
if (&a > &c && &b > &a)
return a;
else if (&b > &c && &a > &b)
return b;
else if (&c > &b && &a > &c)
return c;
}
int main(void)
{
int a = 2;
int b = 3;
int c = 4;
int* pMedian;
cout << "Address of a =" << &a << endl;
cout << "Address of b =" << &b << endl;
cout << "Address of c =" << &c << endl;
pMedian= fun(&a, &c, &b);
cout << "Address of the Median=" << pMedian<< endl;
cout << "The value of the Median=" << *pMedian<< endl;
return 1;
}
|
The reasoning behind this is that the function is being passed three arguments, all pointers to variables declared inside the main function. The function also has to be labeled with a *, because otherwise the compiler bitches about converting int* to int or vice versa, but I don't understand that part because the function isn't a pointer, it's a function. Anyways, it receives the three variables, then goes about executing the if/else if statements. It then returns the correct if statement.
EXCEPT NOT.
What it does return seems to be dependent on this line:
|
pMedian= fun(&a, &c, &b);
|
Whichever value is passed in the middle slot is the value that is returned. I have swapped everything else around and this is the one that changes it.
Also, the code still doesn't 'return a pointer' as the question demands. Which I still don't get. Everytime I try to tell it to
or *b or anything, it says invalid conversion of int to int*. It will only return a pointer if only pointers are used.
Seriously, I'm so over this. Thank you for helping me, and know that I will never, EVER make use of this knowledge once I pass this question.