Hi, I am attempting to write a program that will ask the user to input 10 integers, and then send them to the function that will put them into descending order and replace the original numbers in the array. Then return them to the main to output them to the screen. I am getting confused on how to call the function with the array and how to write the function prototype? Here is my code, i commented out the function because it won't run with it in.
#include<iomanip>
#include<string>
#include<cmath>
usingnamespace std;
int i;
int j;
constint max = 10;
void hi_2_lo( int num[] );
int main()
{
constint max = 10;
int Jethro[max];
for( i = 0; i < max; i++ )
{
cout<<"Please input ten integers: "<<endl<<endl;
while( !(cin>>Jethro[i]) )
{
cin.clear();
cin.ignore( 10000, '\n' );
cout<<"Please input only integers"<<endl;
}
}
cout<<endl<<endl;
hi_2_lo( Jethro );
for( i = 0; i<max; i++ )
{
cout<<Jethro[i]<<endl;
}
system( "pause" );
return 0;
}
void hi_2_lo( int num[] )
{
int y = num[0];
int low;
for( i = 0; i<10; i++ )
{
if( y<num[i] );
low = y;
y = num[i];
num[i] = y;
}
return;
}
Keep in mind, arrays are always passed by reference, not by value. so any modifications to the array within the hi_2_lo() function will be directly changing the array created in main().
The prototype is on line 11. it is exactly the same as the function definition, followed by a ;
I threw in a call at line 34 just to demonstrate how to pass it. This is by no means a finished program.
Oh, ok, that isn't bad at all! I am getting two errors when i run it now, though. One is one line 34 of your code and the other is in line 54 of your code. On 34, it is saying that the identifier can't be found (referring to the 'hi_2_lo') and line 54 says that there is an empty controlled statement found (referring to the ';' ). What do those mean?
hmm... did you remember to put in the prototype? that would explain the problem with line 34.
line 54 is a warning. It's just telling you that the if() statement on line 53 doesn't do anything since you put a ; at the end of it :)
also, make sure the return type for hi_2_lo() is void in both the prototype and definition.
also, also, lines 55 and 56 make circular reference to each other... thus 56 is useless.
also, also, also, your hi_2_lo sorting algorithm is pretty flawed. Here's a sort that's easy to implement complete with an example in c++: http://en.wikipedia.org/wiki/Selection_sort
keep in mind that you want to reverse the check since you're sorting in descending order (so rather than looking for the lowest element to swap, look for the highest)