Jump to content
  • Advertisement
Sign in to follow this  
boboS

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!