Code://old headers #include <iostream.h> #include <stdlib.h> #include <stdio.h>Code://new headers #include <iostream> #include <cstdlib> //use the std namespace using namspace std;
Code://old headers #include <iostream.h> #include <stdlib.h> #include <stdio.h>Code://new headers #include <iostream> #include <cstdlib> //use the std namespace using namspace std;
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
I said cstdlib.h can't be used...
And Cat, if there were multiple problems, why did you only tell me of the one? I fixed the Index thing, and it says that before the sort it is "5" and after it is "8, 8, 35, 92, 19" instead of "8, 12, 19, 35, 92." Also, if both of those swap functions work fine, why do they both generate this error:
"'swap' : local function definitions are illegal?"
Programmer's Law:
If your program doesn't work, look for the part that you didn't think was
important.
I only noticed the swap at first (which is a big problem).
The duplication is because the swap function is still broken.
They both DO work fine, but perhaps you put the function definition in the wrong place. This was my working code:
Code:#include <iostream> #include <cstdlib> #include <cstdio> using namespace std; inline void swap(int &a, int &b){ int temp = a; a = b; b = temp; } #define INDEX 5 void bubble_srt(int a[], int n); int main(void) { int array[INDEX] = {12, 8, 35, 92, 19}; //Altered the numbers... cout<<"Before sorting:"<< endl; for(int i=0; i<INDEX; i++) cout<< array[i]<< endl; bubble_srt(array, INDEX); cout<<"After the sort:"<< endl; for(int i=0; i<INDEX; i++) cout<< array[i]<< endl; } void bubble_srt(int a[], int n) { int i, j; for (i=0; i<n-2; i++) { for (j=0; j<n-1-i; j++){ if (a[j+1] < a[j]) swap(a[j],a[j+1]); } } }
Hey, that actually worked... Now I've gotta get it on my laptop @_@ Anyway, thanks.
Programmer's Law:
If your program doesn't work, look for the part that you didn't think was
important.
Ok, I haven't copied that completely out onto my laptop, but for some reason cstdlib and cstdio aren't on there, but they're on this computer. So, I don't think that will work on it. I'll give it a try now, though. I'll let you know how it works (or doesn't :-P)
Programmer's Law:
If your program doesn't work, look for the part that you didn't think was
important.
Just out of curiousity...why don't you just use the std::sort( ) function (from <algorithm>) to do this like I showed in a previous post?
Well, it's (very slightly) more memory efficient. I dunno, I just don't like decalring variables that I don't have to. I threw it in there because I thought it was kind of a cool tidbit...and I like being crafty with booleans. As far as speed effeciency, yeah there probably is no noticable difference, although I've never tested it.Is that really more efficent, though? It still requires three passes through an ALU, and none of the operations can happen in parallel.
I will try it, I was just using the first one thrown at me to get it to work. And, hey, it did. Its interesting though... I dropped the .h from the end of cstdlib and cstdio and it worked. Now I just have to figure out how to get it to work with words. Thanks for all the help guys.
Programmer's Law:
If your program doesn't work, look for the part that you didn't think was
important.
Standard C++ headers do not have .h on the end. If I wanted to use the C standard header stdlib.h, typically C++ headers drop the .h and add a 'c' to the front, thus <cstdlib>.
Indeed, none of the C++ standard headers include a .h; for example, #include <iostream> is standard, #include <iostream.h> is NOT standard, and not guaranteed to work.
Oh... There have been times where dropping the .h hasn't worked for me, so I tend to leave the .h there. Also, I've gotten the program to work for sorting numbers that the user types in, but sometimes when I use some 3 or 4 digit numbers it'll mess up. For example, I can put in 9, 7054, 7654, 745, and 5. The output will end up being:
9
5
745
7045
7654
Does anybody know why that might be? The code is the exact same as Cat's in his last post, with the exception of the addition of 5 new variables, a prompt to enter some numbers, and the numbers being replaced by the variables.
Programmer's Law:
If your program doesn't work, look for the part that you didn't think was
important.
That's the C++ solution to your problem. If you don't know what vectors are or how to use them, I strongly suggest learning.Code:#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> numbers; int temp; cout <<"Enter five numbers to sort: " <<endl; for( int iter = 1; iter <= 5; iter++ ) { cout <<"#" <<iter <<": "; cin >>temp; numbers.push_back( temp ); } sort( numbers.begin(), numbers.end() ); cout <<"\nSorted list: " <<endl; for( int index = 0; index < numbers.size(); index++ ) cout <<numbers[index] <<endl; }