Sort algorithm
-
Can anyone help me fix this. i am trying to sort the y's of my sprite from highest to lowest values and then print it out. this is what i have but my compiler wont compile it. can anyone kind enough to help me fix this to allow it work. anyways, thank you very very much. i really appreciate it. :-D #include using namespace std; struct SPRITE { int x; int y; }; // Maximum number of sprites const int N_SPRITES = 10; // Array of sprites SPRITE sprites[N_SPRITES]; bool Compare (const SPRITE &one, const SPRITE &two); /* Pre: Enter a Sprite value for one and two * Post: Compares all of both strings */ int main() { int value; for (int i=0; i> value; sprites[i].y = value; } int* begin = &sprites[0]; int* end = &sprites[9]; sort(begin, end, Compare); for (i=0; i
Knowing the error would help us, but I suspect it's that you use Compare before you define it. Move Compare to the top of the file, and if that doesn't work, tell us what the error is. It's also better form not to use all of namespace std, try using std::sort; and using std::cout; instead. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
Knowing the error would help us, but I suspect it's that you use Compare before you define it. Move Compare to the top of the file, and if that doesn't work, tell us what the error is. It's also better form not to use all of namespace std, try using std::sort; and using std::cout; instead. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
What header is sort under? thank you very much Christian :-D Thank you very much, John :-D Aloha from Hawaii :-)
Oh - your one include was iostream and your error was unable to find sort ? It's in algorithm. You'll still need to move Compare so it's defined before you call it, or forward declare it. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
Oh - your one include was iostream and your error was unable to find sort ? It's in algorithm. You'll still need to move Compare so it's defined before you call it, or forward declare it. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
Christian Graus wrote: Oh - your one include was iostream and your error was unable to find sort ? It's in algorithm. You'll still need to move Compare so it's defined before you call it, or forward declare it. i got both of that and i have included it like this: #include #include using std::sort; using std::cout; using std::cin; struct SPRITE { int x; int y; }; // Maximum number of sprites const int N_SPRITES = 10; // Array of sprites SPRITE sprites[N_SPRITES]; bool Compare (const SPRITE &one, const SPRITE &two) /* Pre: Enter value for one and two * Post: Compares all of both strings */ { return (one.y < two.y); } int main() { int value; for (int i=0; i> value; sprites[i].y = value; } sort(sprites[0], sprites[9], Compare); for (i=0; i
-
Christian Graus wrote: Oh - your one include was iostream and your error was unable to find sort ? It's in algorithm. You'll still need to move Compare so it's defined before you call it, or forward declare it. i got both of that and i have included it like this: #include #include using std::sort; using std::cout; using std::cin; struct SPRITE { int x; int y; }; // Maximum number of sprites const int N_SPRITES = 10; // Array of sprites SPRITE sprites[N_SPRITES]; bool Compare (const SPRITE &one, const SPRITE &two) /* Pre: Enter value for one and two * Post: Compares all of both strings */ { return (one.y < two.y); } int main() { int value; for (int i=0; i> value; sprites[i].y = value; } sort(sprites[0], sprites[9], Compare); for (i=0; i
Error number one is because you don't check the 'show message as is ( no HTML )' box below your post. Error number two is because you're not facing east when you program Error number three is because you're using an array instead of std::vector. Seriously, check the box so I can see all your code, tell me what the errors are, and then I might be able to help more. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
Error number one is because you don't check the 'show message as is ( no HTML )' box below your post. Error number two is because you're not facing east when you program Error number three is because you're using an array instead of std::vector. Seriously, check the box so I can see all your code, tell me what the errors are, and then I might be able to help more. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
sorry about that Christian. here is my code now: #include #include using std::sort; using std::cout; using std::cin; struct SPRITE { int x; int y; }; // Maximum number of sprites const int N_SPRITES = 10; bool Compare (const SPRITE &one, const SPRITE &two) /* Pre: Enter value for one and two * Post: Compares all of both strings */ { return (one.y < two.y); } int main() { // Array of sprites SPRITE sprites[N_SPRITES]; // Number value int value; for (int i=0; i> value; sprites[i].y = value; } sort(sprites[0], sprites[9], Compare); for (i=0; i &)' : could not deduce template argument for 'const struct std::iterator<_C,_Ty,_D> &' from 'struc t SPRITE' 3rd error: error C2780: 'void __cdecl std::_Sort_0(_RI,_RI,_Ty *)' : expects 3 arguments - 4 provided c:\program files\microsoft visual studio\vc98\include\algorithm(541) : see declaration of '_Sort_0' C:\DOCUMENTS AND SETTINGS\JOHN CRUZ.JCRUZ495\MY DOCUMENTS\MY PROJECTS\C++\Sort\main.cpp(46) : see reference to function template instantiation 'void __cdecl std::sort(struct SPRITE,struct SPRITE,bool (__cdecl *)(const struct SPRITE &,const s truct SPRITE &))' being compiled hey am really sorry for not providing what the errors were earlier. well anyways. I REALLY APPRECIATE ALL YOU GUYS HELP :-D Thank you very much, John :-D Aloha from Hawaii :-)
-
sorry about that Christian. here is my code now: #include #include using std::sort; using std::cout; using std::cin; struct SPRITE { int x; int y; }; // Maximum number of sprites const int N_SPRITES = 10; bool Compare (const SPRITE &one, const SPRITE &two) /* Pre: Enter value for one and two * Post: Compares all of both strings */ { return (one.y < two.y); } int main() { // Array of sprites SPRITE sprites[N_SPRITES]; // Number value int value; for (int i=0; i> value; sprites[i].y = value; } sort(sprites[0], sprites[9], Compare); for (i=0; i &)' : could not deduce template argument for 'const struct std::iterator<_C,_Ty,_D> &' from 'struc t SPRITE' 3rd error: error C2780: 'void __cdecl std::_Sort_0(_RI,_RI,_Ty *)' : expects 3 arguments - 4 provided c:\program files\microsoft visual studio\vc98\include\algorithm(541) : see declaration of '_Sort_0' C:\DOCUMENTS AND SETTINGS\JOHN CRUZ.JCRUZ495\MY DOCUMENTS\MY PROJECTS\C++\Sort\main.cpp(46) : see reference to function template instantiation 'void __cdecl std::sort(struct SPRITE,struct SPRITE,bool (__cdecl *)(const struct SPRITE &,const s truct SPRITE &))' being compiled hey am really sorry for not providing what the errors were earlier. well anyways. I REALLY APPRECIATE ALL YOU GUYS HELP :-D Thank you very much, John :-D Aloha from Hawaii :-)
Sorry - it was obvious all along. You need to pass in &sprites[0] and &sprites[9]. I'm not sure this will work - it expects begin and end, and end is one PAST the last item in the array, so sprites[9] may not be sorted - therefore I would pass in &sprites[0] + 10 as the second argument. It could also be cleaned up in other ways - you could use mem_fun_ref and have the sorting function be a member of your struct. Check out my article on functors for more info. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
Sorry - it was obvious all along. You need to pass in &sprites[0] and &sprites[9]. I'm not sure this will work - it expects begin and end, and end is one PAST the last item in the array, so sprites[9] may not be sorted - therefore I would pass in &sprites[0] + 10 as the second argument. It could also be cleaned up in other ways - you could use mem_fun_ref and have the sorting function be a member of your struct. Check out my article on functors for more info. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
Glad to help :0) Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
Error number one is because you don't check the 'show message as is ( no HTML )' box below your post. Error number two is because you're not facing east when you program Error number three is because you're using an array instead of std::vector. Seriously, check the box so I can see all your code, tell me what the errors are, and then I might be able to help more. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
Christian Graus wrote: Error number three is because you're using an array instead of std::vector lol! Signature space for rent. Apply Within.