When I remove the &'s I get a segmentation fault. (edit: maybe not. It did the first couple times, but I closed my editor and console and reopened them and now it's fine. Still wrong, but runs without the addresses in the if statements.) Also, replacing the &'s with *'s (dereferencing) give... wait for it...
Now it is returning int b no matter what. If I change the values of a,b,c it returns b. If I change the order the values are passed to the function, it returns b. If I change the order the values are input to the function, it returns b. So the logic is flawed.
#include <iostream>
#include <cmath>
usingnamespace std;
int *fun(int *a, int *b, int *c)
{
if (a > c && b > a)
return a;
elseif (b > c && a > b)
return b;
elseif (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, &b, &c);
cout << "Address of the Median=" << pMedian<< endl;
cout << "The value of the Median=" << *pMedian<< endl;
return 1;
}
And one more time, cire, thank you very much. I am filled with rage, but I have to solve this to move on. I keep thinking I understand it and I can technically get the correct output, but then I play with it more and it doesn't output what I need.
In the code above you're comparing the addresses held in the pointer variables. You need to use * to dereference the pointers so you can compare what's pointed to, and you need to correct your faulty logic to determine the median.
Consider what happens when *a == 3, *b == 2, and *c == 4.
*a is the median correct?
But according to the logic in your function, *a may only be the median if:
#include <iostream>
#include <cmath>
usingnamespace std;
int *fun(int *a, int *b, int *c)
{
if (*a > *c )
{
if (*a < *b)
return a;
if (*c > *b)
return c;
return b;
}
if (*b > *c)
{
if (*b < *a)
return b;
if (*c > *a)
return c;
return a;
}
if (*c > *b)
{
if (*c < *a)
return c;
if (*b > *a)
return b;
return a;
}
}
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, &b, &c);
cout << "Address of the Median=" << pMedian<< endl;
cout << "The value of the Median=" << *pMedian<< endl;
return 1;
}
Haha Mac, welcome to pointers man. As a fellow C++ newb I understand your pain, believe me. But once it clicks, it will click HARD. :) Wait until you get to pointers to functions, or better yet, like I had to do with one exercise, create a pointer to an array of pointers to functions that return pointers and take pointers as arguments. Good lord, it was asterisks EVERYWHERE.
Anyway, if it helps you to think of it differently remember you can attach the asterisk to the TYPE instead of the variable name, like this: int* a; This declares a pointer to int and names it a just the same as int *a; Remember the asterisk in the declaration is a TYPE, and that type is pointer, but you also have to include the type the pointer points to, which is int. After you declare a variable you don't include it's type any time you use it. Which is why saying return a; is returning a pointer. Saying return *a; return the value that a pointed to because it include the dereference operator. Honestly, I hoped one of the thing they would do with the C++11 standard was add a unique operator for dereferencing because I think it is what causes most of the confusion with pointers.
Now the reason you have to declare your function as int* fun(int* a, int* b, int* c) in the declaration is because you HAVE to include each argument and return type. Remember, each is type POINTER TO INT. After the declaration, within the function, you no longer refer to each by type and name, but solely by name, IE, a, b and c. And which is why you return a, which is type pointer to int, just like you declared.
I know it's frustrating, and I have seen some pretty good analogies for explaining pointers a bit better. If you still need help wrapping your head around them don't worry. It's prolly one of the harder, but also rather important, points to get about C++ from what I'm told.
BTW, here is the declaration of that pointer I was talking about: constdouble *(*(*pd)[3])(constdouble*, int)
Wouldn't a more general solution be to sort all the values, then you can easily use the array subscript to calculate the median? I am not sure whether anyone mentioned that already - it was too long ago......
One thing about references - they are very similar to pointers, but they are safer because a reference is always associated with a variable - this can help avoid some dangers with pointers. I know your assignment requires pointers.
BTW - you haven't done any pointer arithmetic yet.......
Here is a link to K&R C programming - it has a whole chapeter on pointers & arrays - worth reading.