Thread: sorts prob

  1. #1
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    Question 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;
    			}	
    
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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.

  3. #3
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    tried

    i tried changing the array size[]...AND the n size...
    hmm, perhaps i should inveatigate Vectors?
    mouse

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 06:53 PM
  2. little prob with string?
    By ghostshadow189 in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2007, 02:02 PM
  3. Simple Sorts Confuse ME =/
    By otchster in forum C Programming
    Replies: 5
    Last Post: 12-03-2005, 02:02 PM
  4. sorts
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 10-17-2004, 07:26 PM
  5. sorting of sorts....hehe
    By newbie2C++ in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2001, 01:02 AM