Join 137,420 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,965 people online right now. Registration is fast and FREE... Join Now!
void shuttle(int mydata[]) { int sort; unsigned short u=sizeof(mydata)-1, h, k, j, o, s; bool breakout; for (h=0;h<u;++h) { for (k=0;k<u;++k) cout << mydata[k] << " "; cout << endl; sort=mydata[h+1]; for (j=0;j<h+1;++j) { breakout=false; if (sort<mydata[j]) { breakout=true; o=j; } if (breakout==true) break; } if (breakout==false) { for (s=h;s>=o;--s) mydata[s+1]=mydata[s]; mydata[o]=sort; } } for (k=0;k<u;++k) cout << mydata[k] << " "; cout << endl; }
int main() { unsigned short u, h; while (1) { cout << "Enter number of items to shuttle sort" <<endl; cin >> u; if (u==0) continue; int mydata[u]; for (h=0;h<u-1;++h) { cout << "Enter item number " << h+1 << endl; cin >> mydata[h]; } shuttle(mydata); } return 0; }
This is what I want to happen:
CODE
Enter number of items to shuttle sort 5 Enter item number 1 3 Enter item number 2 10 Enter item number 3 5 Enter item number 4 1 Enter item number 5 4 3 10 5 1 4 3 10 5 1 4 3 5 10 1 4 1 3 5 10 4 1 3 4 5 10
What actually happened was:
CODE
Enter number of items to shuttle sort 5 Enter item number 1 3 Enter item number 2 10 Enter item number 3 5 Enter item number 4 1 3 10 5
Im very new to c++ and have been trying to find out why this isnt working, but its driving me crazy, if you can help me thanks.
The term "shuttle" sort is, unfortunately, horribly imprecise. It can refer to a selection sort, an insertion sort, or a bidirectional bubble sort. Maybe more. From your code, I'm guessing insertion sort.
You have a number of problems, but I'll stick to the C++ gotchas. The statement u=sizeof(mydata)-1 may be wishful thinking. You're much better off to just passing the size of the array along with the array itself.
Here's a little bit of code you should ultimately find useful.
Ok Ive fixed it now, it was encounting an error because I was giving a funtion a unsigned short instead of an int (it stopped giving me errors when i changed it anyway). Thanks for your help baavgai.
This post has been edited by TheMagnitude: 13 Jan, 2008 - 02:35 AM
This is still a sloppy sort, kind of a worst implementation of an insertion sort. Still, I got something like what you're offering to work:
CODE
void shuttle(int mydata[], int size) { int sort; unsigned short element; // you don't want to look at the last element it will be in order when done for (int h=0;h<size-1;++h) { showData(mydata, size); sort=mydata[h+1]; bool breakout=false; for (int j=0; j<h+1; ++j) { if (sort<mydata[j]) { breakout=true; element=j; break; } } // you need to move data around if breakout // otherwise, you're not doing anything if (breakout) { for (int s=h;s>=element;--s) mydata[s+1]=mydata[s]; mydata[element]=sort; } } showData(mydata, size); }
#include <iostream> using namespace std; void showData (int mydata[], int size) { for (int k=0;k<size;k++) cout << mydata[k] << " "; cout << endl; }
void shuttle(int mydata[], int size) { int element; int sort; cout << "Original data:" << endl; showData(mydata,size); cout << "Sorting:" << endl; for (int h=0;h<size-1;++h) { sort=mydata[h+1]; bool breakout=false; for (int j=0;(j<h+1 && breakout==false);++j) { if (sort<mydata[j]) { breakout=true; element=j; } } if (breakout==true) { for (int s=h;s>=element;--s) mydata[s+1]=mydata[s]; mydata[element]=sort; } showData(mydata, size); } showData(mydata, size); }
int main() { int u; while (1) { cout << "Enter number of items to shuttle sort" <<endl; cin >> u; if (u==0) continue; int mydata[u]; for (int h=0;h<u;++h) { cout << "Enter item number " << h+1 << endl; cin >> mydata[h]; } shuttle(mydata, u); } return 0; }
Also I have a question. in your code:
CODE
if (sort<mydata[j]) { breakout=true; element=j; break; }
Doesnt the break; statement just break out of the if statement but doesnt break out of the for statement?
Doesnt the break; statement just break out of the if statement but doesnt break out of the for statement?
The break statement exists to exit loops. Specifically, to avoid the flag system you're using. You don't need to break from a conditional block, more conditional logic can take care of that. Early loop exits are a special case, however; thus the break.
Also, since the break you're using is inside your if, and you set flag to true and then process on true, you actually don't need it at all.
Here's a slight variation of that final code.
CODE
void shuttle(int mydata[], int size) { cout << "Original data:" << endl; showData(mydata,size); cout << "Sorting:" << endl; for (int h=0;h<size-1;++h) { int sort=mydata[h+1]; for (int j=0; j<h+1;++j) { if (sort<mydata[j]) { for (int s=h;s>=j;--s) mydata[s+1]=mydata[s]; mydata[j]=sort; break; } } showData(mydata, size); } showData(mydata, size); }
Also I was wondering why I can't use the PM system?
No clue, never used it.
As for problems to play with; make your own! Those little algorithms implementations come from the bad old days of math professors being clueless as to what a computer can do. They're entertaining in there own way, but don't really do it for some folks.
Nothing will get you more inspired than having a project that you want to make. First tries can suck, but that's not the point, you'll always learn something.
On this site people post their homework all the time. Don't give them the answer, but try your hand at the homework. A lot of the problems are fairly entertaining. You can learn from any code, good or bad; you just learn different stuff.
Also I was wondering why I can't use the PM system?
No clue, never used it.
As for problems to play with; make your own! Those little algorithms implementations come from the bad old days of math professors being clueless as to what a computer can do. They're entertaining in there own way, but don't really do it for some folks.
Nothing will get you more inspired than having a project that you want to make. First tries can suck, but that's not the point, you'll always learn something.
On this site people post their homework all the time. Don't give them the answer, but try your hand at the homework. A lot of the problems are fairly entertaining. You can learn from any code, good or bad; you just learn different stuff.
Thanks for your advice.
What I'd like to do when I am pretty fluent in C++ is make a 2D physics engine. Im really looking forward to doing that I think thats a good project, may be a bit hard though but I always aim to high when programming
What Id like to know is where people post there projects in progress or completed for other people to download and comment on (im not on about code snippets and tutorials but compiled exes and whatnot)?
QUOTE(jjhaag @ 13 Jan, 2008 - 08:12 PM)
QUOTE(TheMagnitude @ 13 Jan, 2008 - 06:43 AM)
Also I was wondering why I can't use the PM system?
You need to have at least 10 posts to PM other users - it's a feature to prevent abuse and spam.