Well does it work? Compile? Sort?
Well does it work? Compile? Sort?
Thought for the day:FLTK: "The most fun you can have with your clothes on.""Are you sure your sanity chip is fully screwed in sir?" (Kryten)
Stroustrup:
"If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"
No, it does not.
Something is seriously wrong with the loops. And its confusing me.. plus I need to know how to print the array once its sorted. right now im getting the unsorted numbers from the file and just bogus numbers from a cout i have after i call the function.
Code:#include <iostream> #include <fstream> using namespace::std; int bubblesort( int arrayf[], int currentitem, int arraysize, bool isswapped); int bubblesort( int arrayf[], int currentitem, int arraysize, bool isswapped) { int temp; if (currentitem ==arraysize-1){ //checking for end of array if (isswapped==true){ isswapped=false; bubblesort(arrayf,0,arraysize,isswapped); } if (isswapped==false){ bubblesort(arrayf,currentitem,arraysize,isswapped);//sorted } if (arrayf[currentitem]>arrayf[currentitem+1])//swapping if smaller { temp = arrayf[currentitem]; arrayf[currentitem] = arrayf[currentitem+1]; arrayf[currentitem+1] = temp; isswapped = true; bubblesort(arrayf,currentitem+1,arraysize,isswapped); } else { if(arrayf[currentitem]<arrayf[currentitem+1]) //isswapped=false; bubblesort(arrayf,currentitem+1,arraysize,isswapped); } } } int main() { int j = 15; int arr[j]; const int numValues = sizeof(arr) / sizeof(arr[0]); //opens file ifstream myfile ("integers.txt"); if (myfile.is_open()) { while ( myfile.good() ) {cout<<"unsorted"; for(j=0;j<=14;j++){ myfile>>arr[j]; cout << arr[j] << endl; } bool bSwapped = false; bubblesort(arr, 0, numValues, bSwapped); // start from the first item cout<<arr[j]; myfile.close(); getchar(); return 0; } }}
and also where the return statements would be , i was wondering if a return statement should be in the if where isswapped==false... but im not sure.
I am not going to do any more work on the sort for you, but Are you saying you are not allowed to use loops for the output either? If that is the case why are you allowed one to populate your array? Anyway, I you are not allowed to use loop to output result it is a simple matter of using my countdown example but count up instead and use the value as your index...starting from 0 instead of 1 as i mentioned earlier. So this would be a seperate call to 'RecursiveOutput()' with its own recall once you are done.
I have to add you have some real fundamental errors in there, you need to practice some basic stuff and gain understanding in general, quite apart from the task in hand
Last edited by rogster001; 03-28-2012 at 01:40 PM.
Thought for the day:FLTK: "The most fun you can have with your clothes on.""Are you sure your sanity chip is fully screwed in sir?" (Kryten)
Stroustrup:
"If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"