# Sort function

This topic is 4613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a newbie question: I want to make a sort function that have unlimited number of parameters to be given. For example I want to sort 5 numbers 3,4,5,19,2 (form another function) and should be: "sorting (5,3,4,5,19,2);" I've tryed to do it but It keeps giving me errors. So whats wrong with the nex code ?
#include <iostream>
#include <stdlib.h>

using namespace std;
int n,i,j,man;

int sorting(int n,int v[0],...) {
for (i=0;i<n;i++) {
for (j=i+1;j<n;j++) {
if (v>v[j]) {
man=v;
v=v[j];
v[j]=man;
}
}
}

for (i=0;i<n;i++) {
cout<<v;
cout<<endl;
}
system("PAUSE");
}

int main() {
sorting(4,1,7,3,6); // That should sort "(4," 4 numbers that are 1,7,3,6)
}
//And I dont want to use a sort function from internet because I do this for my own learning process
Thanks


##### Share on other sites
You're main function should be:

int main() {

int array[4] = {1, 7, 3, 6};
sorting(4,array); // That should sort "(4," 4 numbers that are 1,7,3,6)

}

##### Share on other sites
Thanks man. It worked. Also I've learned something new about arrays. :)

##### Share on other sites
You're using variable arguments lists, which have some specific syntax in C++. Have a look at this page for the proper syntax; the va_* stuff can give you a little bit more safety and eliminate the need for the count parameter.

However, I would recommend against using variable argument lists. It is easy to pass arguments to your function that are not numbers, which can cause all manner of horrible bugs. The "..." system is extremely unsafe and it is a bad habit to get into using it.

Instead, you can use something like a std::vector to hold your set of numbers, and have the sort() function use that:

void sorting(std::vector<int>& numbers){  // Do sorting here  // The parameter is a reference to a vector holding ints  // This means that when we modify the parameter, we're  // really modifying the original variable that we got  // passed from main.}int main(){  std::vector<int> numbers;  numbers.push_back(1);  numbers.push_back(7);  numbers.push_back(3);  numbers.push_back(6);  sorting(numbers);  // Print out the contents of numbers}

Then you can use the built-in .size() function on the vector to know exactly how many elements there are. You also know that every entry in the vector will be a number (an int, in this case) because the compiler won't allow you to add other types of data. Even better still, the vector will automatically take care of handling any memory shuffling that needs to be done.

As one final bonus, once you've learned some sorting methods on your own, you can simply switch over to using the std::sort function, and never have to worry about sorting again [smile]

##### Share on other sites
ok! Thanks ! I'll try that to for sure! :)

##### Share on other sites
I was the anonymous poster by the way. Anyway, you can do your main function like this aswell, which makes it easier to update:

int main()
{
int array[] = {1, 3, 8, 2}; //as an example
sorting(sizeof(array), array); //takes the size of the array as the first parameter
}

But it is better to use ApochPiQ's method.

##### Share on other sites
Yes, that for automaticaly detection on how much numbers are. :) Also after that I should try ApochPiQ way wich is a litle dificult for me to understand! Thanks again to all.

##### Share on other sites
Bu the sizeof() wont return me the number of indices of the arrays, right ?

##### Share on other sites
Oh yea, sorry my mistake, forget that last post.

##### Share on other sites
sizeof(array) / sizeof(int) will give you the number of elements in the array.

1. 1
2. 2
3. 3
4. 4
frob
20
5. 5
JoeJ
20

• 11
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632214
• Total Posts
3004858

×