MaxMin
Jun 1, 2008 at 2:41am UTC
Write a program that inputs three floating point numbers. The program then calls a function MaxMin that determines the smallest and largest of these numbers. The program then passes back the values to the main program which outputs the results. The function should not perform any I/O operations (Use pointers to solve this problem)
here is my code
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
#include <iostream>
using namespace std;
void MaxMin( float *, float *, float *, float *, float * );
int main ()
{
float n1;
float n2;
float n3;
float max = 0;
float min = 0;
cout << "enter three float point numbers: " ;
cin >> n1 >> n2 >> n3;
MaxMin( &n1, &n2, &n3, &max, &min );
cout << "Max is: " << max;
cout << " min is: " << min;
cout << endl;
return 0;
}
void MaxMin( float *n1, float *n2, float *n3, float *max, float *min )
{
max = n1;
if ( n2 > n1 )
max = n2;
else if ( n3 > n1 )
max = n3;
min = n1;
if ( n2 < n1 )
min = n2;
else if ( n3 < n1 )
min = n3;
}
there is something wrong
but i dont know where it is
can u help me whith this program
Jun 1, 2008 at 8:40am UTC
I think the problem is as follows:
All parameters are pointers.
n2 > n1
compares 2 pointers
*n2 > *n1
compares the values pointed to.
The same is true for assignment.
2 solutions:
1)Put a *before every pointer in the function.
2)Make the first 3 parameters floats instead of pointers to floats.
Jun 1, 2008 at 11:55am UTC
Also, your logic short circuits before testing all possibilities.
Topic archived. No new replies allowed.