# sorts prob

• 01-01-2003
mouse163
sorts prob
Hi & Happy New Year!!! I was wondering if anyone can tell me why my array sort works ONLY if the array is
less than 11? (making my n double digits 10).
the putput once the array goes greater than 10 gives me a long
negative number at the BEginning of the sort, and if i go MUCH higher than 10 say 20, i get proportionally more of the same negative number.
Hmm just want to know where my bug is.
thanks everyone!
Mouse
ps here is my code
Code:

``` #include <iostream.h> #include<fstream.h> void bubble_Sort( int arr[], int n ); ifstream infile("C:\\CODE\\IN.txt");     ofstream outfile("C:\\CODE\\of.txt");   void main() { if(!infile){         cerr << "Cannot open input file" << endl;         }     if(!outfile){         cerr << "Cannot open output file" << endl;         }       int arr[10];  for (int i =0; i< 9; i++)           infile >> arr[i];  bubble_Sort( arr, i-1);    for ( i =0; i< 9; i++)   cout<<"\n"<<arr[i]; } // Bubble Sort void bubble_Sort( int arr[], int n) {                     for (int i=0; i<= n; i++)                 for(int j=0; j<= n-i-1; j++)                         if ( arr[j] > arr[j+1] )                         {                                        int tmp = arr[j+1];                                 arr[j+1] = arr[j];                                 arr[j] = tmp;                         }        }```
• 01-01-2003
grib
int arr[10] in main() is only 10 int's long, if you try to input more it starts writing and working with undefined memory locations.

either make arr bigger or use std::vector<int> and add elements with push_back(). This works a lot like an array, except push_back() makes it get bigger and it always knows it's size.

pass it to buble_sort like this

bubble_sort(std::vector<int> &arr)

you no longer need the n, just use arr.size();

vectors can also sort themselves, but that would be cheating.
• 01-01-2003
mouse163
tried
i tried changing the array size[]...AND the n size...
hmm, perhaps i should inveatigate Vectors?
mouse
• 01-01-2003
Hammer