Okay, this forum has been a big help, but I'm banging my head on the wall now. I need any help you can offer-just point me in the right direction. Here is my assignment and what I have so far (including a bunch of commented out things I have tried!) Yes, I know I'm not doing what is assigned yet, but I think I am getting there. I don't know how to use pointers here. Help? Write a function that accepts three floating point values as parameters and sorts them from largest to smallest. The program must use pointers or references and the function will affect (sort) the three variables entered by the user in the calling program (main).
Hint: Look at the three numbers as two pairs: sort the first pair, then the second, then the first again if necessary...
#include <iostream>
float min(float, float, float);
float maximum(float, float, float);
//float mid(float, float, float);
//float sort3(float, float, float);
//float sort2(float, float);
float a, b, c, *pA=0, *pB=0, *pC=0, smallest, largest, middle;
int main()
{
using namespace std;
cout << " This program will sort 3 input numbers from largest to smallest.\n";
cout << "Enter a first number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
cout << "Enter a third number: ";
cin >> c;
cout << "Max number is: " << maximum(a,b,c) << endl;
cout << "Min number is: " << min(a,b,c) << endl;
//cout << "Middle number is: " << mid << endl;
return 0;
}
float maximum( float x, float y, float z )
{
float max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max; //max is largest value
}
float min( float x, float y, float z )
{
float min = x;
if ( y < min )
min = y;
if ( z < min )
min = z;
return min; //min is smallest value
}
//Sort3(a,b,c);
////After calling this function your a, b, and c will be ordered a >= b >= c
//
//void Sort3(float &i, int &j, int &k);
//
////REM: the caller passes in the 3 unsorted values through i,j,k
////The function passes back those 3 values sorted i >= j >= k
//float i,j,k;
//{
//Sort2(i, j);
//Sort2(i, k);
//Sort2(j, k);
//}
//
//void Sort2(float &m, float &n);
////{
//////REM: the caller passes in the 2 unsorted values through m,n
//////The function passes back those 2 values sorted m >= n}
//float tmp
//
//if (m < n)
//
// tmp= m;
// m= n;
// n= tmp;
//}
Your sorting code seems ok. Except that you pass j and k as integers. And that you declare floats i, j, k even though they are function arguments. And that there are ; after your function declarations (when you should be defining them).
Uncomment your code, then try compiling it. If it doesn't compile, tell us the error message. If it doesn't work, tell us the input and the output.
Also, use [code] tags
Okay, I fixed the errors and it compiles now. Apparently, the code is okay, but I still need to output the numbers in order largest to smallest and use pointers in there. Not sure where to go next. Here's the latest code:
#include <iostream>
float min(float, float, float);
float maximum(float, float, float);
//float mid(float, float, float);
float sort3(float, float, float);
float sort2(float, float);
float a, b, c, *pA=0, *pB=0, *pC=0, smallest, largest, middle;
int main()
{
using namespace std;
cout << " This program will sort 3 input numbers from largest to smallest.\n";
cout << "Enter a first number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
cout << "Enter a third number: ";
cin >> c;
cout << "Max number is: " << maximum(a,b,c) << endl;
cout << "Min number is: " << min(a,b,c) << endl;
//cout << "Middle number is: " << mid << endl;
cout << a << b << c << endl;
return 0;
}
float maximum( float x, float y, float z )
{
float max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max; //max is largest value
}
float min( float x, float y, float z )
{
float min = x;
if ( y < min )
min = y;
if ( z < min )
min = z;
return min; //min is smallest value
}
//Sort3(a,b,c);
////After calling this function your a, b, and c will be ordered a >= b >= c
//
void Sort2(float &m, float &n)
{
////REM: the caller passes in the 2 unsorted values through m,n
////The function passes back those 2 values sorted m >= n}
float tmp;
////REM: the caller passes in the 3 unsorted values through i,j,k
////The function passes back those 3 values sorted i >= j >= k
//float i,j,k;
{
Sort2(i, j);
Sort2(i, k);
Sort2(j, k);
}
Okay, that makes sense. But now if I run it I get 1>c:\users\laura\desktop\practice\practice\practice.cpp(23) : error C3861: 'Sort3': identifier not found
And -duh! I don't know what I was thinking about the poiners & references. Somehow I was thinking I had to do both...
Okay, I did that. I thought the way I had it before (I was using float instead of void) was okay, but I fixed it. Now it compiles but gives me a run-time error #3 saying the variable tmp is being used without being initialized. I tried putting it with a b and c as a float, but it didn't like that either....you are soooo helping me. Hopefully it is over soon!
#include <iostream>
float min(float, float, float);
float maximum(float, float, float);
void Sort3(float &i, float &j, float &k);
float sort2(float, float);
float a, b, c;
float tmp=0;
int main()
{
usingnamespace std;
cout << " This program will sort 3 input numbers from largest to smallest.\n";
cout << "Enter a first number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
cout << "Enter a third number: ";
cin >> c;
cout << "Max number is: " << maximum(a,b,c) << endl;
cout << "Min number is: " << min(a,b,c) << endl;
Sort3(a, b, c);
cout << "Sorted: " << a << " " << b << " " << c;
return 0;
}
float maximum( float x, float y, float z)
{
float max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max; //max is largest value
}
float min( float x, float y, float z )
{
float min = x;
if ( y < min )
min = y;
if ( z < min )
min = z;
return min; //min is smallest value
}
#include <iostream>
float min(float, float, float);
float maximum(float, float, float);
void Sort3(float &i, float &j, float &k);
float sort2(float, float);
float a, b, c;
//float tmp=0;
int main()
{
usingnamespace std;
float tmp=0;
cout << " This program will sort 3 input numbers from largest to smallest.\n";
cout << "Enter a first number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
cout << "Enter a third number: ";
cin >> c;
cout << "Max number is: " << maximum(a,b,c) << endl;
cout << "Min number is: " << min(a,b,c) << endl;
Sort3(a,b,c);
cout << "Sorted: " << a << " " << b << " " << c;
return 0;
}
float maximum( float x, float y, float z)
{
float max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max; //max is largest value
}
float min( float x, float y, float z )
{
float min = x;
if ( y < min )
min = y;
if ( z < min )
min = z;
return min; //min is smallest value
}
void Sort2(float &m, float &n)
{
float tmp;
if (m < n)
tmp= m;
m= n;
n= tmp;
}
void Sort3(float &i, float &j, float &k)
{
Sort2(i, j);
Sort2(i, k);
Sort2(j, k);
}