Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i do not know how to use the user defined functions for the program to run properly

What I have tried:

C++
#include <iostream>
using namespace std;
sort Ascending( ); 
sort Descending( ); 
int main(){
	int num[100],n;
int i,j,man;
cout<<"enter n for the numbers you want to sort"<<endl<<endl;
cin>>n;
	for(i=0;i<n;i++){		
			cout<<"enter number"<<endl;
	cin>>num[i];
	}
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(num[i]<num[j]){
				man=num[i];
				num[i]=num[j];
				num[j]=man;
			}			
		}
	}
	cout<<"ascending "<<endl;
	for(i=0;i<n;i++){
	cout<<" "<<num[i]<<endl;;
	}
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(num[i]>num[j]){
				man=num[i];
				num[i]=num[j];
				num[j]=man;
			}			
		}
	}
	cout<<" descending"<<endl;
	for(i=0;i<n;i++){
		cout<<" "<<num[i]<<endl;
}
return 0;
}
Posted
Updated 1-Aug-21 22:37pm
v2
Comments
Richard MacCutchan 27-Oct-19 16:47pm    
That is because you have put the sorting code inline in main. You need to move the sort routines into their own functions.

Just write your sorting code in ad hoc functions. You should avoid C like arrays for such a task.
Try
C++
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

void sort_ascending(vector<int> & v);
void sort_descending(vector <int> & v);

int main()
{

  size_t n;
  cout << "enter n for the numbers you want to sort" << endl;
  cin >> n;
  cout << endl;

  vector<int> v(n);

  for (size_t i =0; i < n; ++i)
  {
    cout << "enter number " << (i+1) << " of " << n << endl;
    cin >> v[i];
  }
  cout << "ascending:\n";
  sort_ascending( v);
  for (auto x : v)
    cout << x << "\n";

  cout << "descending:\n";
  sort_descending(v);
  for (auto x : v)
    cout << x << "\n";
}

void sort_ascending( vector<int> & v)
{
  for (size_t i=0; i < v.size()-1; ++i)
    for (size_t j= i+1; j < v.size(); ++j)
      if ( v[i] > v[j] ) swap(v[i], v[j]);
}

void sort_descending( vector<int> & v)
{
  for (size_t i=0; i < v.size()-1; ++i)
    for (size_t j= i+1; j < v.size(); ++j)
      if ( v[i] < v[j] ) swap(v[i], v[j]);
}


That's a possible implementation of an exercise. On the other hand, if you are writing production code, then discard your own (bubble!) implementation of sort and use the one provided by the C++ standard library instead.
C++
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;

int main()
{
  size_t n;
  cout << "enter n for the numbers you want to sort" << endl;
  cin >> n;
  cout << endl;

  vector<int> v(n);

  for (size_t i =0; i < n; ++i)
  {
    cout << "enter number " << (i+1) << " of " << n << endl;
    cin >> v[i];
  }

  cout << "ascending:\n";
  sort(v.begin(), v.end());
  for (auto x : v)
    cout << x << "\n";

  cout << "descending:\n";
  sort(v.begin(), v.end(), std::greater<int>());
  for (auto x : v)
    cout << x << "\n"
}
 
Share this answer
 
A "user defined function" is just a function that you declare, just as you declare main i #n the code you show.
In fact, you ALMOST show the prototypes for two functions immediately above main - so all you have to do is give them valid names and move the code for "sorting in ascending order" to one function, and "sorting in descending order" in another. You can then call them from main just by using the name of the function followed by brackets (with any parameter values inside the brackets if needed):
C++
void MyFunction();    //Prototype - lets you use it in main before the definition

void main()
   {
   ...
   MyFunction();
   ...
   }
void MyFunction()
   {
   // your code here
   }
 
Share this answer
 
Comments
Richard MacCutchan 28-Oct-19 4:34am    
If you put the complete function above main in the source then there is no need for prototypes. One of the most annoying features of C/C++ (IMHO).
OriginalGriff 28-Oct-19 4:43am    
True - but since he has "almost prototypes" in there already, it makes sense to continue with them.

C compilers used to be slow enough without adding yet another pass just to pick up forward references :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900