#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
Sort function
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 ?
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)
}
int main() {
int array[4] = {1, 7, 3, 6};
sorting(4,array); // That should sort "(4," 4 numbers that are 1,7,3,6)
}
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:
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]
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]
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement